"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__, {
    resolveToolbar: ()=>resolveToolbar
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const ToolbarActionContext_cjs_namespaceObject = require("../hooks/ToolbarActionContext.cjs");
const external_icon_registry_cjs_namespaceObject = require("./icon-registry.cjs");
const DEFAULT_ACTIONS_FOR_ALL_MODES = [
    {
        id: 'collapse',
        icon: 'chevrons-down-up',
        label: 'Toggle collapse',
        condition: {
            handles: [
                {
                    handleType: 'artifact',
                    type: 'source'
                }
            ]
        }
    }
];
const DEFAULT_MODE_TOOLBARS = {
    design: {
        actions: [
            {
                id: 'delete',
                icon: 'trash',
                label: 'Delete'
            },
            {
                id: 'duplicate',
                icon: 'copy',
                label: 'Duplicate'
            },
            {
                id: 'breakpoint',
                icon: 'circle',
                label: 'Toggle breakpoint'
            },
            ...DEFAULT_ACTIONS_FOR_ALL_MODES
        ]
    },
    debug: {
        actions: [
            {
                id: 'breakpoint',
                icon: 'circle',
                label: 'Toggle breakpoint'
            },
            ...DEFAULT_ACTIONS_FOR_ALL_MODES
        ]
    },
    evaluation: {
        actions: [
            ...DEFAULT_ACTIONS_FOR_ALL_MODES
        ]
    }
};
const toolbarRegistry = {
    getModeDefaults: (mode)=>DEFAULT_MODE_TOOLBARS[mode] ?? {
            actions: []
        }
};
function evaluateCondition(manifest, action, nodeType, context) {
    if (!action.condition) return true;
    const { condition } = action;
    if (condition.requiresPermissions && context.permissions) {
        const hasPermissions = condition.requiresPermissions.every((perm)=>context.permissions.includes(perm));
        if (!hasPermissions) return false;
    }
    if (condition.hideOnExecution && context.executionState) return false;
    if (condition.nodeTypes && !condition.nodeTypes.includes(nodeType)) return false;
    if (condition.handles?.length) {
        const manifestHandles = manifest.handleConfiguration.flatMap((group)=>group.handles);
        const hasMatchingHandle = condition.handles.some((requiredHandle)=>manifestHandles.some((manifestHandle)=>manifestHandle.handleType === requiredHandle.handleType && (void 0 === requiredHandle.type || manifestHandle.type === requiredHandle.type)));
        if (!hasMatchingHandle) return false;
    }
    return true;
}
function convertToNodeAction(action, mode, nodeData) {
    const IconComponent = (0, external_icon_registry_cjs_namespaceObject.getIcon)(action.icon);
    return {
        id: action.id,
        icon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(IconComponent, {
            w: 14,
            h: 14
        }),
        label: action.label,
        onAction: (nodeId)=>{
            const { onToolbarAction } = (0, ToolbarActionContext_cjs_namespaceObject.getToolbarActionStore)();
            onToolbarAction?.({
                actionId: action.id,
                nodeId,
                mode,
                nodeData
            });
        }
    };
}
function mergeToolbarConfigs(base, extension) {
    if (!base && !extension) return {
        actions: []
    };
    if (!base) return extension;
    if (!extension) return base;
    return {
        actions: [
            ...base.actions,
            ...extension.actions
        ],
        overflowActions: [
            ...base.overflowActions ?? [],
            ...extension.overflowActions ?? []
        ]
    };
}
function resolveToolbar(manifest, context, nodeData) {
    const { nodeType, toolbarExtensions: manifestToolbarExtensions, suppressDefaultToolbarActions } = manifest;
    const { mode } = (0, ToolbarActionContext_cjs_namespaceObject.getToolbarActionStore)();
    const rawDefaults = toolbarRegistry.getModeDefaults(mode);
    const suppressed = new Set(suppressDefaultToolbarActions?.[mode] ?? []);
    const modeDefaults = suppressed.size ? {
        actions: rawDefaults.actions.filter((a)=>!suppressed.has(a.id)),
        overflowActions: rawDefaults.overflowActions?.filter((a)=>!suppressed.has(a.id))
    } : rawDefaults;
    const nodeExtensions = manifestToolbarExtensions?.[mode];
    const merged = mergeToolbarConfigs(modeDefaults, nodeExtensions);
    const filteredActions = merged.actions.filter((action)=>evaluateCondition(manifest, action, nodeType, context)).map((action)=>convertToNodeAction(action, mode, nodeData));
    const filteredOverflow = (merged.overflowActions ?? []).filter((action)=>evaluateCondition(manifest, action, nodeType, context)).map((action)=>convertToNodeAction(action, mode, nodeData));
    if (0 === filteredActions.length && 0 === filteredOverflow.length) return;
    return {
        actions: filteredActions,
        overflowLabel: 'More options',
        overflowActions: filteredOverflow.length > 0 ? filteredOverflow : void 0
    };
}
exports.resolveToolbar = __webpack_exports__.resolveToolbar;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "resolveToolbar"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
