import * as React from "react";

import { useAppNavigation } from "@/app/navigation/useAppNavigation";
import { listenForNavigationDeepLinks } from "@/shared/deep-link";

/**
 * Subscribe to `buzz://message` deep links emitted by the Tauri backend
 * and route them through the app's navigation helpers.
 *
 * Lives in a hook (not inline in `AppShell`) so it can be unit-tested
 * without the entire shell, and so the shell file stays under its line cap.
 *
 * Mirrors the cold-start race handling of the `connect` listener in
 * `App.tsx`: late-arriving payloads from a fresh launch are picked up the
 * first time the listener mounts. Routing matches the in-app buzz://
 * handler in `markdown.tsx`: always `goChannel` with `messageId` and let
 * the channel route's existing scroll-into-view + getEventById backfill
 * resolve the target (works for both stream replies and forum threads).
 */
export function useMessageDeepLinks(enabled = true) {
  const { goChannel } = useAppNavigation();

  React.useEffect(() => {
    if (!enabled) return;

    let cancelled = false;
    const unlistenPromise = listenForNavigationDeepLinks(
      async (payload) => {
        if (cancelled) return false;
        await goChannel(payload.channelId);
        return true;
      },
      async (payload) => {
        if (cancelled) return false;
        await goChannel(payload.channelId, {
          messageId: payload.messageId,
          threadRootId: payload.threadRootId,
        });
        return true;
      },
    );
    return () => {
      cancelled = true;
      void unlistenPromise.then((unlisten) => unlisten());
    };
  }, [enabled, goChannel]);
}
