import { createContext, type ReactNode, useContext } from "react";
import { useEncounter } from "../hooks/use-encounter.js";
import { useUndoRedoShortcuts } from "../hooks/use-undo-redo-shortcuts.js";

type EncounterContextValue = ReturnType<typeof useEncounter>;

const EncounterContext = createContext<EncounterContextValue | null>(null);

export function EncounterProvider({ children }: { children: ReactNode }) {
	const value = useEncounter();
	useUndoRedoShortcuts(value.undo, value.redo, value.canUndo, value.canRedo);
	return (
		<EncounterContext.Provider value={value}>
			{children}
		</EncounterContext.Provider>
	);
}

export function useEncounterContext(): EncounterContextValue {
	const ctx = useContext(EncounterContext);
	if (!ctx) throw new Error("useEncounterContext requires EncounterProvider");
	return ctx;
}
