import { jsx } from "react/jsx-runtime";
import { getToolbarActionStore } from "../hooks/ToolbarActionContext.js";
import { getIcon } from "./icon-registry.js";
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 = getIcon(action.icon);
    return {
        id: action.id,
        icon: /*#__PURE__*/ jsx(IconComponent, {
            w: 14,
            h: 14
        }),
        label: action.label,
        onAction: (nodeId)=>{
            const { onToolbarAction } = 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 } = 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
    };
}
export { resolveToolbar };
