"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    useToolbarState: ()=>useToolbarState
});
const external_react_namespaceObject = require("react");
const useToolbarState = ({ config, expanded, nodeId, hidden })=>{
    const [isDropdownOpen, setIsDropdownOpen] = (0, external_react_namespaceObject.useState)(false);
    const dropdownRef = (0, external_react_namespaceObject.useRef)(null);
    const buttonRef = (0, external_react_namespaceObject.useRef)(null);
    const actionsWithState = (0, external_react_namespaceObject.useMemo)(()=>config.actions.map((action)=>{
            if ('separator' === action.id) return action;
            const item = action;
            return {
                ...item,
                onClick: ()=>{
                    try {
                        item.onAction(nodeId);
                    } catch (error) {
                        console.error(`Error executing action ${item.id}:`, error);
                    }
                }
            };
        }), [
        config.actions,
        nodeId
    ]);
    const pinnedActions = (0, external_react_namespaceObject.useMemo)(()=>actionsWithState.filter((action)=>'separator' !== action.id && !!action.isPinned), [
        actionsWithState
    ]);
    const overflowActionsWithState = (0, external_react_namespaceObject.useMemo)(()=>{
        if (!config.overflowActions) return [];
        return config.overflowActions.map((action)=>{
            if ('separator' === action.id) return action;
            const item = action;
            return {
                ...item,
                onClick: ()=>{
                    try {
                        item.onAction(nodeId);
                    } catch (error) {
                        console.error(`Error executing action ${item.id}:`, error);
                    }
                }
            };
        });
    }, [
        config.overflowActions,
        nodeId
    ]);
    const displayState = (0, external_react_namespaceObject.useMemo)(()=>{
        if (hidden) return 'hidden';
        if (expanded) return 'expanded';
        return pinnedActions.length > 0 ? 'pinned' : 'hidden';
    }, [
        hidden,
        expanded,
        pinnedActions.length
    ]);
    const actionsToDisplay = (0, external_react_namespaceObject.useMemo)(()=>{
        if ('hidden' === displayState) return [];
        if ('expanded' === displayState) return actionsWithState;
        return pinnedActions;
    }, [
        displayState,
        actionsWithState,
        pinnedActions
    ]);
    const shouldShowOverflow = 'expanded' === displayState && overflowActionsWithState.length > 0;
    (0, external_react_namespaceObject.useEffect)(()=>{
        const handleClickOutside = (event)=>{
            if (dropdownRef.current && !dropdownRef.current.contains(event.target) && buttonRef.current && !buttonRef.current.contains(event.target)) setIsDropdownOpen(false);
        };
        if (isDropdownOpen) document.addEventListener('mousedown', handleClickOutside);
        return ()=>document.removeEventListener('mousedown', handleClickOutside);
    }, [
        isDropdownOpen
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (!shouldShowOverflow && isDropdownOpen) setIsDropdownOpen(false);
    }, [
        shouldShowOverflow,
        isDropdownOpen
    ]);
    const toggleDropdown = (0, external_react_namespaceObject.useCallback)((e)=>{
        e.stopPropagation();
        e.preventDefault();
        setIsDropdownOpen((prev)=>!prev);
    }, []);
    return {
        isDropdownOpen,
        setIsDropdownOpen,
        dropdownRef,
        buttonRef,
        shouldShowOverflow,
        displayState,
        actionsToDisplay,
        overflowActionsToDisplay: overflowActionsWithState,
        separatorOrientation: config.position && 'top' !== config.position && 'bottom' !== config.position ? 'horizontal' : 'vertical',
        toggleDropdown
    };
};
exports.useToolbarState = __webpack_exports__.useToolbarState;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "useToolbarState"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
