import { createContext, type ReactNode, useContext } from "react";
import { useSidePanelState } from "../hooks/use-side-panel-state.js";

type SidePanelContextValue = ReturnType<typeof useSidePanelState>;

const SidePanelContext = createContext<SidePanelContextValue | null>(null);

export function SidePanelProvider({ children }: { children: ReactNode }) {
	const value = useSidePanelState();
	return (
		<SidePanelContext.Provider value={value}>
			{children}
		</SidePanelContext.Provider>
	);
}

export function useSidePanelContext(): SidePanelContextValue {
	const ctx = useContext(SidePanelContext);
	if (!ctx) throw new Error("useSidePanelContext requires SidePanelProvider");
	return ctx;
}
