/* eslint-disable react-compiler/react-compiler -- the transport branch in `useWindowTitle` is a deliberate rules-of-hooks exception; the compiler refuses files that disable it */
import {SDK_CHANNEL_NAME, SDK_NODE_NAME} from '@sanity/message-protocol'
import {isMessageBusEnvironment} from '@sanity/sdk/_internal'
import {useEffect, useState} from 'react'

import {useWindowConnection} from '../comlink/useWindowConnection'

interface ContextResource {
  type: string
  title?: string
  manifest?: {
    title?: string
  } | null
  activeDeployment?: {
    manifest?: {
      title?: string
    } | null
  } | null
}

interface ContextResponse {
  context: {
    resource: ContextResource
  }
}

function resolveAppTitle(resource: ContextResource): string | undefined {
  return resource.manifest?.title || resource.activeDeployment?.manifest?.title || resource.title
}

/**
 * Sets the browser's document title, automatically including the app's name
 * from the manifest.
 *
 * This follows the same convention as Sanity Studio workspaces, where the
 * workspace name is always present in the title:
 *
 * - With a view title: `<viewTitle> | <appTitle>`
 * - Without a view title: `<appTitle>`
 *
 * The Sanity dashboard appends `| Sanity` to produce the final browser tab title.
 *
 * Works in both Dashboard runtimes: it manages the document title over the Comlink connection, and
 * no-ops under the message bus, where the Dashboard host owns the title.
 *
 * @param viewTitle - An optional view-specific title to prepend to the app title.
 *
 * @example
 * ```tsx
 * import {useWindowTitle} from '@sanity/sdk-react/dashboard'
 *
 * function MoviesList() {
 *   useWindowTitle('Movies')
 *   return <div>...</div>
 * }
 *
 * // Browser tab: "Movies | My App | Sanity"
 * ```
 *
 * @example
 * ```tsx
 * // Call without arguments to show just the app title
 * function AppRoot() {
 *   useWindowTitle()
 *   return <Outlet />
 * }
 *
 * // Browser tab: "My App | Sanity"
 * ```
 *
 * @public
 * @category Dashboard
 */
export function useWindowTitle(viewTitle?: string): void {
  // The document title is set by the Dashboard host under the message bus, so this hook only
  // manages it in the Comlink runtime. The branch is stable: the transport is fixed for the page
  // lifetime, so the Comlink hooks below always run or never run.
  if (isMessageBusEnvironment()) return
  // eslint-disable-next-line react-hooks/rules-of-hooks -- transport is fixed for the page lifetime
  useComlinkWindowTitle(viewTitle)
}

function useComlinkWindowTitle(viewTitle?: string): void {
  const [appTitle, setAppTitle] = useState<string | null>(null)

  const {fetch} = useWindowConnection({
    name: SDK_NODE_NAME,
    connectTo: SDK_CHANNEL_NAME,
  })

  useEffect(() => {
    if (!fetch) return

    const controller = new AbortController()

    async function fetchAppTitle(signal: AbortSignal) {
      try {
        const data = await fetch<ContextResponse>('dashboard/v1/context', undefined, {signal})
        // Local development resources do not have a registered title or deployment manifest.
        // In that case, use the title rendered into the iframe HTML by the Sanity CLI.
        const title = resolveAppTitle(data.context.resource) || document.title
        if (title) {
          setAppTitle(title)
        }
      } catch (err: unknown) {
        if (err instanceof Error && err.name === 'AbortError') return
        // eslint-disable-next-line no-console
        console.error('Failed to fetch app title from dashboard context:', err)
      }
    }

    fetchAppTitle(controller.signal)

    return () => {
      controller.abort()
    }
  }, [fetch])

  useEffect(() => {
    if (!appTitle) return

    const previous = document.title
    document.title = viewTitle ? `${viewTitle} | ${appTitle}` : appTitle

    return () => {
      document.title = previous
    }
  }, [viewTitle, appTitle])
}
