import { useGlobal } from './useGlobal';

export const usePanelChannel = <T>(channel: string, initialData: T) => {
  return useGlobal<T>(['PANEL_CHANNEL', channel], initialData);
};

/**
 * Declare a channel once — its name, its payload type and its initial value — and get the hook that reads
 * and writes it.
 *
 * Calling `usePanelChannel('files:refresh-signal', 0)` at each site instead has two holes, both silent.
 * A typo in the name does not error: it yields a *different*, empty channel pinned to its initial value,
 * so the publisher publishes into nowhere and the subscriber waits forever. And `T` comes from each
 * caller, so a publisher and a subscriber can simply disagree about the payload and nothing checks — the
 * four `files:refresh-signal` sites were `number` by convention only.
 *
 * One definition fixes both: there is one spelling and one type, and every consumer gets them by import.
 */
export const defineChannel =
  <T>(channel: string, initialData: T) =>
  () =>
    usePanelChannel<T>(channel, initialData);
