import { createContext, type ReactNode, useContext } from "react";
import { useRulesEdition } from "../hooks/use-rules-edition.js";

type RulesEditionContextValue = ReturnType<typeof useRulesEdition>;

const RulesEditionContext = createContext<RulesEditionContextValue | null>(
	null,
);

export function RulesEditionProvider({ children }: { children: ReactNode }) {
	const value = useRulesEdition();
	return (
		<RulesEditionContext.Provider value={value}>
			{children}
		</RulesEditionContext.Provider>
	);
}

export function useRulesEditionContext(): RulesEditionContextValue {
	const ctx = useContext(RulesEditionContext);
	if (!ctx)
		throw new Error("useRulesEditionContext requires RulesEditionProvider");
	return ctx;
}
