'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useState, useCallback, useEffect } from 'react';
const AGENT_ASSIST_STORAGE_KEY = 'devdoc-agent-assist-open';
const MobileContext = /*#__PURE__*/ createContext(null);
// Helper to get initial state from localStorage (default to true)
function getInitialAgentAssistState() {
    if (typeof window === 'undefined') return true;
    const stored = localStorage.getItem(AGENT_ASSIST_STORAGE_KEY);
    // Default to true (open) if not set
    return stored === null ? true : stored === 'true';
}
export function MobileProvider({ children }) {
    const [isLeftSidebarOpen, setIsLeftSidebarOpen] = useState(false);
    const [isRightSidebarOpen, setIsRightSidebarOpen] = useState(false);
    const [isMobile, setIsMobile] = useState(false);
    const [isHydrated, setIsHydrated] = useState(false);
    // Initialize from localStorage after hydration (only on desktop)
    useEffect(()=>{
        const isMobileView = window.innerWidth < 1024;
        if (!isMobileView) {
            const savedState = getInitialAgentAssistState();
            setIsRightSidebarOpen(savedState);
        }
        setIsHydrated(true);
    }, []);
    // Detect mobile on mount and window resize
    useEffect(()=>{
        const checkMobile = ()=>{
            setIsMobile(window.innerWidth < 1024); // lg breakpoint
        };
        // Initial check
        checkMobile();
        // Listen for resize
        window.addEventListener('resize', checkMobile);
        return ()=>window.removeEventListener('resize', checkMobile);
    }, []);
    // Close left sidebar when switching to desktop (left sidebar is mobile-only)
    // Note: Right sidebar can stay open on desktop as it's now a toggleable panel
    useEffect(()=>{
        if (!isMobile) {
            setIsLeftSidebarOpen(false);
        }
    }, [
        isMobile
    ]);
    const toggleLeftSidebar = useCallback(()=>{
        setIsLeftSidebarOpen((prev)=>!prev);
        // Close right sidebar when opening left
        setIsRightSidebarOpen(false);
    }, []);
    const toggleRightSidebar = useCallback(()=>{
        setIsRightSidebarOpen((prev)=>{
            const newState = !prev;
            // Persist to localStorage (only on desktop)
            if (typeof window !== 'undefined' && window.innerWidth >= 1024) {
                localStorage.setItem(AGENT_ASSIST_STORAGE_KEY, String(newState));
            }
            return newState;
        });
        // Close left sidebar when opening right
        setIsLeftSidebarOpen(false);
    }, []);
    const openLeftSidebar = useCallback(()=>{
        setIsLeftSidebarOpen(true);
        setIsRightSidebarOpen(false);
    }, []);
    const closeLeftSidebar = useCallback(()=>{
        setIsLeftSidebarOpen(false);
    }, []);
    const openRightSidebar = useCallback(()=>{
        setIsRightSidebarOpen(true);
        setIsLeftSidebarOpen(false);
        // Persist to localStorage (only on desktop)
        if (typeof window !== 'undefined' && window.innerWidth >= 1024) {
            localStorage.setItem(AGENT_ASSIST_STORAGE_KEY, 'true');
        }
    }, []);
    const closeRightSidebar = useCallback(()=>{
        setIsRightSidebarOpen(false);
        // Persist to localStorage (only on desktop)
        if (typeof window !== 'undefined' && window.innerWidth >= 1024) {
            localStorage.setItem(AGENT_ASSIST_STORAGE_KEY, 'false');
        }
    }, []);
    const closeAllSidebars = useCallback(()=>{
        setIsLeftSidebarOpen(false);
        setIsRightSidebarOpen(false);
        // Persist to localStorage (only on desktop)
        if (typeof window !== 'undefined' && window.innerWidth >= 1024) {
            localStorage.setItem(AGENT_ASSIST_STORAGE_KEY, 'false');
        }
    }, []);
    return /*#__PURE__*/ _jsx(MobileContext.Provider, {
        value: {
            isLeftSidebarOpen,
            isRightSidebarOpen,
            toggleLeftSidebar,
            toggleRightSidebar,
            openLeftSidebar,
            closeLeftSidebar,
            openRightSidebar,
            closeRightSidebar,
            closeAllSidebars,
            isMobile
        },
        children: children
    });
}
export function useMobile() {
    const context = useContext(MobileContext);
    if (!context) {
        throw new Error('useMobile must be used within a MobileProvider');
    }
    return context;
}
