"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSidemenuContext = exports.SidemenuProvider = exports.SidemenuStateProvider = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const utils_1 = require("../utils");
const submenuReducer = (state, action) => {
    let newState;
    switch (action.type) {
        case 'SET_SUBMENU': {
            if (state[action.itemId] === action.isOpen) {
                return state; // Return same reference if no change
            }
            newState = Object.assign(Object.assign({}, state), { [action.itemId]: action.isOpen });
            break;
        }
        case 'TOGGLE_SUBMENU': {
            const currentValue = state[action.itemId] || false;
            newState = Object.assign(Object.assign({}, state), { [action.itemId]: !currentValue });
            break;
        }
        case 'RESTORE_STATE': {
            newState = action.state;
            break;
        }
        case 'RESET_ALL': {
            newState = {};
            break;
        }
        default:
            return state;
    }
    // Submenu states are now persisted in context only
    return newState;
};
const [SidemenuProvider, useSidemenuContext] = (0, utils_1.createContext)({ isOptional: true });
exports.SidemenuProvider = SidemenuProvider;
exports.useSidemenuContext = useSidemenuContext;
// Enhanced provider that manages state
const SidemenuStateProvider = ({ children, isExpandedInitial = true, onExpandedChange, variant, size, isDark, }) => {
    // Pure uncontrolled state without persistence - context only
    const [isExpanded, setIsExpanded] = (0, react_1.useState)(isExpandedInitial);
    // Initialize submenu states as empty - persisted in context only
    const [submenuStates, dispatchSubmenu] = (0, react_1.useReducer)(submenuReducer, {});
    // Submenu state management functions using reducer dispatch - memoized
    const setSubmenuState = (0, react_1.useCallback)((itemId, isOpen) => {
        dispatchSubmenu({ type: 'SET_SUBMENU', itemId, isOpen });
    }, []);
    const toggleSubmenu = (0, react_1.useCallback)((itemId) => {
        dispatchSubmenu({ type: 'TOGGLE_SUBMENU', itemId });
    }, []);
    // Enhanced setIsExpanded that handles callbacks - memoized
    const handleSetIsExpanded = (0, react_1.useCallback)((newIsExpanded) => {
        setIsExpanded(newIsExpanded);
        onExpandedChange === null || onExpandedChange === void 0 ? void 0 : onExpandedChange(newIsExpanded);
    }, [onExpandedChange]);
    // Memoized reset functions for external use
    const resetAllStates = (0, react_1.useCallback)(() => {
        dispatchSubmenu({ type: 'RESET_ALL' });
        const defaultExpanded = isExpandedInitial;
        setIsExpanded(defaultExpanded);
        onExpandedChange === null || onExpandedChange === void 0 ? void 0 : onExpandedChange(defaultExpanded);
    }, [isExpandedInitial, onExpandedChange]);
    const resetExpansionOnly = (0, react_1.useCallback)(() => {
        const defaultExpanded = isExpandedInitial;
        setIsExpanded(defaultExpanded);
        onExpandedChange === null || onExpandedChange === void 0 ? void 0 : onExpandedChange(defaultExpanded);
    }, [isExpandedInitial, onExpandedChange]);
    const resetSubmenuOnly = (0, react_1.useCallback)(() => {
        dispatchSubmenu({ type: 'RESET_ALL' });
    }, []);
    // Memoized context value with all state management functions
    const contextValue = (0, react_1.useMemo)(() => ({
        size,
        variant,
        isDark,
        isMenuExpanded: isExpanded,
        submenuStates,
        setSubmenuState,
        toggleSubmenu,
        setIsExpanded: handleSetIsExpanded,
        onExpandedChange,
        resetAllStates,
        resetExpansionOnly,
        resetSubmenuOnly,
    }), [
        size,
        variant,
        isDark,
        isExpanded,
        submenuStates,
        setSubmenuState,
        toggleSubmenu,
        handleSetIsExpanded,
        onExpandedChange,
        resetAllStates,
        resetExpansionOnly,
        resetSubmenuOnly,
    ]);
    return (0, jsx_runtime_1.jsx)(SidemenuProvider, { value: contextValue, children: children });
};
exports.SidemenuStateProvider = SidemenuStateProvider;
//# sourceMappingURL=context.js.map