import { jsx as _jsx } from "react/jsx-runtime";
import { useMemo, useState, useCallback, useReducer } from 'react';
import { createContext } from '../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 = { ...state, [action.itemId]: action.isOpen };
            break;
        }
        case 'TOGGLE_SUBMENU': {
            const currentValue = state[action.itemId] || false;
            newState = { ...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] = createContext({ isOptional: true });
// Enhanced provider that manages state
export const SidemenuStateProvider = ({ children, isExpandedInitial = true, onExpandedChange, variant, size, isDark, }) => {
    // Pure uncontrolled state without persistence - context only
    const [isExpanded, setIsExpanded] = useState(isExpandedInitial);
    // Initialize submenu states as empty - persisted in context only
    const [submenuStates, dispatchSubmenu] = useReducer(submenuReducer, {});
    // Submenu state management functions using reducer dispatch - memoized
    const setSubmenuState = useCallback((itemId, isOpen) => {
        dispatchSubmenu({ type: 'SET_SUBMENU', itemId, isOpen });
    }, []);
    const toggleSubmenu = useCallback((itemId) => {
        dispatchSubmenu({ type: 'TOGGLE_SUBMENU', itemId });
    }, []);
    // Enhanced setIsExpanded that handles callbacks - memoized
    const handleSetIsExpanded = useCallback((newIsExpanded) => {
        setIsExpanded(newIsExpanded);
        onExpandedChange?.(newIsExpanded);
    }, [onExpandedChange]);
    // Memoized reset functions for external use
    const resetAllStates = useCallback(() => {
        dispatchSubmenu({ type: 'RESET_ALL' });
        const defaultExpanded = isExpandedInitial;
        setIsExpanded(defaultExpanded);
        onExpandedChange?.(defaultExpanded);
    }, [isExpandedInitial, onExpandedChange]);
    const resetExpansionOnly = useCallback(() => {
        const defaultExpanded = isExpandedInitial;
        setIsExpanded(defaultExpanded);
        onExpandedChange?.(defaultExpanded);
    }, [isExpandedInitial, onExpandedChange]);
    const resetSubmenuOnly = useCallback(() => {
        dispatchSubmenu({ type: 'RESET_ALL' });
    }, []);
    // Memoized context value with all state management functions
    const contextValue = 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 _jsx(SidemenuProvider, { value: contextValue, children: children });
};
export { SidemenuProvider, useSidemenuContext };
//# sourceMappingURL=context.js.map