import { createContext, type ReactNode, useContext } from "react";
import { usePlayerCharacters } from "../hooks/use-player-characters.js";

type PlayerCharactersContextValue = ReturnType<typeof usePlayerCharacters>;

const PlayerCharactersContext =
	createContext<PlayerCharactersContextValue | null>(null);

export function PlayerCharactersProvider({
	children,
}: {
	children: ReactNode;
}) {
	const value = usePlayerCharacters();
	return (
		<PlayerCharactersContext.Provider value={value}>
			{children}
		</PlayerCharactersContext.Provider>
	);
}

export function usePlayerCharactersContext(): PlayerCharactersContextValue {
	const ctx = useContext(PlayerCharactersContext);
	if (!ctx)
		throw new Error(
			"usePlayerCharactersContext requires PlayerCharactersProvider",
		);
	return ctx;
}
