import { createContext, useContext, type ReactNode } from 'react';
import type { InboxAdapter } from './adapter';

const InboxContext = createContext<InboxAdapter | null>(null);

export function InboxProvider({ adapter, children }: { adapter: InboxAdapter; children: ReactNode }) {
  return <InboxContext.Provider value={adapter}>{children}</InboxContext.Provider>;
}

/** Access the host-injected inbox adapter. Throws outside a provider — a missing provider is a
 *  wiring bug, and failing loudly beats a confusing null-deref three layers down. */
export function useInboxAdapter(): InboxAdapter {
  const adapter = useContext(InboxContext);
  if (!adapter) throw new Error('useInboxAdapter must be used within an <InboxProvider>');
  return adapter;
}
