import { createContext, type ReactNode, useContext } from "react";
import { useBestiary } from "../hooks/use-bestiary.js";

export type { SearchResult } from "../hooks/use-bestiary.js";

type BestiaryContextValue = ReturnType<typeof useBestiary>;

const BestiaryContext = createContext<BestiaryContextValue | null>(null);

export function BestiaryProvider({ children }: { children: ReactNode }) {
	const value = useBestiary();
	return (
		<BestiaryContext.Provider value={value}>
			{children}
		</BestiaryContext.Provider>
	);
}

export function useBestiaryContext(): BestiaryContextValue {
	const ctx = useContext(BestiaryContext);
	if (!ctx) throw new Error("useBestiaryContext requires BestiaryProvider");
	return ctx;
}
