"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__, {
    replaceTemplateVars: ()=>replaceTemplateVars,
    resolveDisplay: ()=>resolveDisplay,
    resolveVisibility: ()=>resolveVisibility,
    resolveHandles: ()=>resolveHandles
});
const external_collapse_cjs_namespaceObject = require("./collapse.cjs");
function resolveDisplay(manifestDisplay, context) {
    if (!manifestDisplay) return {
        icon: 'circle-question-mark',
        shape: 'square',
        label: context?.display?.label || 'Unknown Node'
    };
    const isCollapsed = context?.isCollapsed ?? false;
    const shape = context?.display?.shape ?? manifestDisplay.shape;
    const collapsedShape = (0, external_collapse_cjs_namespaceObject.getCollapsedShape)(shape);
    const expandedShape = manifestDisplay.shape;
    const resolvedLabel = context?.display?.canvasLabel ?? context?.display?.label ?? manifestDisplay.canvasLabel ?? manifestDisplay.label;
    return {
        ...manifestDisplay,
        ...context?.display,
        label: resolvedLabel,
        canvasLabel: context?.display?.canvasLabel ?? manifestDisplay.canvasLabel,
        shape: isCollapsed ? collapsedShape : expandedShape
    };
}
function resolveVisibility(visible, context) {
    if (void 0 === visible) return true;
    if ('boolean' == typeof visible) return visible;
    if ('string' == typeof visible) {
        const value = getPropertyByPath(context, visible);
        return Boolean(value);
    }
    return true;
}
function replaceTemplateVars(template, vars) {
    return template.replace(/\{(\w+(?:\.\w+)*)\}/g, (match, path)=>{
        const value = getPropertyByPath(vars, path);
        return null != value ? String(value) : match;
    });
}
function resolveHandles(handleGroups, context) {
    const isCollapsed = context?.isCollapsed ?? false;
    return handleGroups.map((group)=>{
        const handles = group.handles.flatMap((handle)=>{
            const isArtifactHandle = 'artifact' === handle.handleType;
            const handleBaseVisible = resolveVisibility(handle.visible, context);
            const handleVisible = isCollapsed && isArtifactHandle ? false : handleBaseVisible;
            if (handle.repeat) {
                const array = getPropertyByPath(context, handle.repeat);
                if (!Array.isArray(array)) {
                    console.warn(`Repeat expression "${handle.repeat}" did not resolve to an array. Skipping handle.`);
                    return [];
                }
                const itemVar = handle.itemVar || 'item';
                const indexVar = handle.indexVar || 'index';
                return array.map((item, index)=>{
                    const vars = {
                        ...context,
                        [itemVar]: item,
                        [indexVar]: index
                    };
                    return {
                        ...handle,
                        id: replaceTemplateVars(handle.id, vars),
                        label: handle.label ? replaceTemplateVars(handle.label, vars) : void 0,
                        visible: handleVisible,
                        repeat: void 0,
                        itemVar: void 0,
                        indexVar: void 0
                    };
                });
            }
            return {
                ...handle,
                id: replaceTemplateVars(handle.id, context),
                label: handle.label ? replaceTemplateVars(handle.label, context) : void 0,
                visible: handleVisible
            };
        });
        const hasVisibleHandles = handles.some((h)=>h.visible);
        const groupBaseVisible = group.visible;
        const groupVisible = isCollapsed && !hasVisibleHandles ? false : groupBaseVisible;
        return {
            ...group,
            visible: groupVisible,
            handles
        };
    });
}
function getPropertyByPath(obj, path) {
    const keys = path.split('.');
    let current = obj;
    for (const key of keys){
        if (null == current) return;
        if ('object' != typeof current || !(key in current)) return;
        current = current[key];
    }
    return current;
}
exports.replaceTemplateVars = __webpack_exports__.replaceTemplateVars;
exports.resolveDisplay = __webpack_exports__.resolveDisplay;
exports.resolveHandles = __webpack_exports__.resolveHandles;
exports.resolveVisibility = __webpack_exports__.resolveVisibility;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "replaceTemplateVars",
    "resolveDisplay",
    "resolveHandles",
    "resolveVisibility"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
