'use client';
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SidebarProvider = exports.useSidebar = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importDefault(require("react"));
const storage_1 = require("../../../lib/storage");
const SidebarContext = react_1.default.createContext(undefined);
const useSidebar = () => {
    const context = react_1.default.useContext(SidebarContext);
    if (!context) {
        throw new Error('useSidebar must be used within a SidebarProvider');
    }
    return context;
};
exports.useSidebar = useSidebar;
const SidebarProvider = ({ children }) => {
    const [hydrated, setHydrated] = react_1.default.useState(false);
    const [collapsed, setCollapsed] = react_1.default.useState(false);
    const [items, _setItems] = react_1.default.useState({});
    const toggleSidebar = () => setCollapsed((collapsed) => !collapsed);
    const getItemCollapsed = (key) => {
        return items[key] || false;
    };
    const setItemCollapsed = (key, value) => {
        _setItems((items) => ({ ...items, [key]: value }));
    };
    // Read from localStorage after hydration to avoid SSR mismatch
    react_1.default.useEffect(() => {
        setCollapsed((0, storage_1.getStorage)('sidebar-collapsed', false) === 'true');
        _setItems((0, storage_1.getStorageObject)('sidebar-items', {}));
        setHydrated(true);
    }, []);
    // Only persist to localStorage after hydration to avoid overwriting stored values
    react_1.default.useEffect(() => {
        if (!hydrated) {
            return;
        }
        localStorage.setItem('sidebar-collapsed', JSON.stringify(collapsed));
    }, [collapsed, hydrated]);
    react_1.default.useEffect(() => {
        if (!hydrated) {
            return;
        }
        localStorage.setItem('sidebar-items', JSON.stringify(items));
    }, [items, hydrated]);
    return ((0, jsx_runtime_1.jsx)(SidebarContext.Provider, { value: {
            isCollapsed: collapsed,
            items,
            setItems: _setItems,
            getItemCollapsed,
            setItemCollapsed,
            toggleSidebar
        }, children: children }));
};
exports.SidebarProvider = SidebarProvider;
