import { createContext } from 'preact';
import { useContext } from 'preact/hooks';
import { useRecommendations } from '../hooks/useRecommendations.js';
import type { ComponentChildren } from 'preact';

type Value = ReturnType<typeof useRecommendations>;

const Ctx = createContext<Value | null>(null);

export function RecommendationsProvider({ children }: { children: ComponentChildren }) {
  return <Ctx.Provider value={useRecommendations()}>{children}</Ctx.Provider>;
}

export function useRecommendationsContext(): Value {
  const ctx = useContext(Ctx);
  if (!ctx) throw new Error('Must be inside RecommendationsProvider');
  return ctx;
}
