import { jsx, Fragment } from 'react/jsx-runtime';
import { createContext, useContext, useState, useCallback, useMemo, useEffect } from 'react';

const SidebarContext = createContext(null);
const useSidebarContext = () => {
    const context = useContext(SidebarContext);
    // Return null if not in sidebar context (allows NavItem to work elsewhere)
    return context;
};
const AccordionNavContext = createContext(null);
const useAccordionNavContext = () => useContext(AccordionNavContext);
const AccordionNavProvider = ({ children, accordion = false }) => {
    const [openId, setOpenId] = useState(null);
    const register = useCallback((id) => {
        setOpenId(id);
    }, []);
    const unregister = useCallback((id) => {
        setOpenId(prev => (prev === id ? null : prev));
    }, []);
    const value = useMemo(() => ({ openId, register, unregister }), [openId, register, unregister]);
    if (!accordion)
        return jsx(Fragment, { children: children });
    return (jsx(AccordionNavContext.Provider, { value: value, children: children }));
};
const SidebarProvider = ({ children, defaultCollapsed = false, collapsed: controlledCollapsed, onCollapsedChange, mobileBreakpoint = 1024, mobileOpen: controlledMobileOpen, onMobileOpenChange, width = {}, side = 'left', variant = 'sidebar', }) => {
    const [internalCollapsed, setInternalCollapsed] = useState(defaultCollapsed);
    const [internalMobileOpen, setInternalMobileOpen] = useState(false);
    const [isMobile, setIsMobile] = useState(false);
    // Detect mobile breakpoint
    useEffect(() => {
        if (typeof window === 'undefined')
            return;
        const mql = window.matchMedia(`(max-width: ${mobileBreakpoint - 1}px)`);
        const handler = (e) => {
            setIsMobile(e.matches);
            if (!e.matches) {
                setInternalMobileOpen(false);
                onMobileOpenChange === null || onMobileOpenChange === void 0 ? void 0 : onMobileOpenChange(false);
            }
        };
        handler(mql);
        mql.addEventListener('change', handler);
        return () => mql.removeEventListener('change', handler);
    }, [mobileBreakpoint, onMobileOpenChange]);
    // Collapsed state
    const isCollapsedControlled = controlledCollapsed !== undefined;
    const collapsed = isCollapsedControlled ? controlledCollapsed : internalCollapsed;
    const setCollapsed = useCallback((newCollapsed) => {
        if (!isCollapsedControlled) {
            setInternalCollapsed(newCollapsed);
        }
        onCollapsedChange === null || onCollapsedChange === void 0 ? void 0 : onCollapsedChange(newCollapsed);
    }, [isCollapsedControlled, onCollapsedChange]);
    const toggleCollapsed = useCallback(() => {
        setCollapsed(!collapsed);
    }, [collapsed, setCollapsed]);
    // Mobile open state
    const isMobileOpenControlled = controlledMobileOpen !== undefined;
    const isMobileOpen = isMobileOpenControlled ? controlledMobileOpen : internalMobileOpen;
    const setMobileOpen = useCallback((open) => {
        if (!isMobileOpenControlled) {
            setInternalMobileOpen(open);
        }
        onMobileOpenChange === null || onMobileOpenChange === void 0 ? void 0 : onMobileOpenChange(open);
    }, [isMobileOpenControlled, onMobileOpenChange]);
    // Lock body scroll when mobile overlay is open
    useEffect(() => {
        if (isMobile && isMobileOpen) {
            document.body.style.overflow = 'hidden';
            return () => { document.body.style.overflow = ''; };
        }
    }, [isMobile, isMobileOpen]);
    const contextValue = useMemo(() => ({
        collapsed,
        setCollapsed,
        toggleCollapsed,
        width: {
            expanded: width.expanded || 280,
            collapsed: width.collapsed || 64,
        },
        side,
        variant,
        isMobileOpen,
        setMobileOpen,
        isMobile,
    }), [collapsed, setCollapsed, toggleCollapsed, width.expanded, width.collapsed, side, variant, isMobileOpen, setMobileOpen, isMobile]);
    return (jsx(SidebarContext.Provider, { value: contextValue, children: children }));
};

export { AccordionNavProvider, SidebarProvider, useAccordionNavContext, useSidebarContext };
