import { createContext, useCallback, useContext, useState, type ReactNode } from "react"; const STORAGE_KEY = "nexus:chat-panel-open"; interface ChatPanelContextValue { chatOpen: boolean; activeConversationId: string | null; setChatOpen: (open: boolean) => void; toggleChat: () => void; setActiveConversationId: (id: string | null) => void; } const ChatPanelContext = createContext(null); function readPreference(): boolean { try { const raw = localStorage.getItem(STORAGE_KEY); return raw === "true"; } catch { return false; } } function writePreference(open: boolean) { try { localStorage.setItem(STORAGE_KEY, String(open)); } catch { /* ignore */ } } export function ChatPanelProvider({ children }: { children: ReactNode }) { const [chatOpen, setChatOpenState] = useState(readPreference); const [activeConversationId, setActiveConversationId] = useState(null); const setChatOpen = useCallback((open: boolean) => { setChatOpenState(open); writePreference(open); }, []); const toggleChat = useCallback(() => { setChatOpenState((prev) => { const next = !prev; writePreference(next); return next; }); }, []); return ( {children} ); } export function useChatPanel() { const ctx = useContext(ChatPanelContext); if (!ctx) throw new Error("useChatPanel must be used within ChatPanelProvider"); return ctx; }