import { createContext, type ReactNode, useContext } from "react";
import { useInitiativeRolls } from "../hooks/use-initiative-rolls.js";

type InitiativeRollsContextValue = ReturnType<typeof useInitiativeRolls>;

const InitiativeRollsContext =
	createContext<InitiativeRollsContextValue | null>(null);

export function InitiativeRollsProvider({ children }: { children: ReactNode }) {
	const value = useInitiativeRolls();
	return (
		<InitiativeRollsContext.Provider value={value}>
			{children}
		</InitiativeRollsContext.Provider>
	);
}

export function useInitiativeRollsContext(): InitiativeRollsContextValue {
	const ctx = useContext(InitiativeRollsContext);
	if (!ctx)
		throw new Error(
			"useInitiativeRollsContext requires InitiativeRollsProvider",
		);
	return ctx;
}
