import { useEffect } from 'react';
import { useAtomValue } from 'jotai';
import { customWindowChromeAtom } from '../state/customWindowChrome';
import { invokeTauri, isTauri } from './useTauri';

/**
 * P5-47 — drive the native window frame from the `customWindowChromeAtom`.
 *
 * On mount and whenever the atom changes, pushes the value onto the native
 * `set_custom_chrome` command: `enabled = true` strips the OS decorations so the
 * web `<TitleBar/>` can take over, `enabled = false` restores the native frame.
 * No-op in the browser (`isTauri()` guard), so it's safe to call unconditionally
 * from the app shell.
 */
export function useTauriWindowChrome(): void {
  const enabled = useAtomValue(customWindowChromeAtom);

  useEffect(() => {
    if (!isTauri()) return;
    invokeTauri('set_custom_chrome', { enabled });
  }, [enabled]);
}
