import { createContext, type ReactNode, useContext } from "react";
import type {
	BestiaryCachePort,
	BestiaryIndexPort,
	EncounterPersistence,
	Pf2eBestiaryIndexPort,
	PlayerCharacterPersistence,
	UndoRedoPersistence,
} from "../adapters/ports.js";

export interface Adapters {
	encounterPersistence: EncounterPersistence;
	undoRedoPersistence: UndoRedoPersistence;
	playerCharacterPersistence: PlayerCharacterPersistence;
	bestiaryCache: BestiaryCachePort;
	bestiaryIndex: BestiaryIndexPort;
	pf2eBestiaryIndex: Pf2eBestiaryIndexPort;
}

const AdapterContext = createContext<Adapters | null>(null);

export function AdapterProvider({
	adapters,
	children,
}: {
	adapters: Adapters;
	children: ReactNode;
}) {
	return (
		<AdapterContext.Provider value={adapters}>
			{children}
		</AdapterContext.Provider>
	);
}

export function useAdapters(): Adapters {
	const ctx = useContext(AdapterContext);
	if (!ctx) throw new Error("useAdapters requires AdapterProvider");
	return ctx;
}
