import { createContext as createContext$1 } from "../utils/react.js";
import { useCallback, useEffect, useMemo, useReducer, useRef, useState } from "react";
import { jsx } from "react/jsx-runtime";

//#region src/sidemenuV2/context.tsx
const submenuReducer = (state, action) => {
	let newState;
	switch (action.type) {
		case "SET_SUBMENU":
			if (state[action.itemId] === action.isOpen) return state;
			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;
	}
	return newState;
};
const [SidemenuProvider, useSidemenuContext] = createContext$1({ isOptional: true });
const SidemenuStateProvider = ({ children, isExpandedInitial = true, onExpandedChange, variant, size, isDark }) => {
	const [isExpanded, setIsExpanded] = useState(isExpandedInitial);
	const isInternalChangeRef = useRef(false);
	useEffect(() => {
		if (!isInternalChangeRef.current) setIsExpanded(isExpandedInitial);
		isInternalChangeRef.current = false;
	}, [isExpandedInitial]);
	const [submenuStates, dispatchSubmenu] = useReducer(submenuReducer, {});
	const setSubmenuState = useCallback((itemId, isOpen) => {
		dispatchSubmenu({
			type: "SET_SUBMENU",
			itemId,
			isOpen
		});
	}, []);
	const toggleSubmenu = useCallback((itemId) => {
		dispatchSubmenu({
			type: "TOGGLE_SUBMENU",
			itemId
		});
	}, []);
	const handleSetIsExpanded = useCallback((newIsExpanded) => {
		isInternalChangeRef.current = true;
		setIsExpanded(newIsExpanded);
		onExpandedChange?.(newIsExpanded);
	}, [onExpandedChange]);
	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" });
	}, []);
	return /* @__PURE__ */ jsx(SidemenuProvider, {
		value: 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
		]),
		children
	});
};

//#endregion
export { SidemenuStateProvider, useSidemenuContext };
globalThis.__vuiVersion__ = "4.0.0"
//# sourceMappingURL=context.js.map