import { getCollapsedShape } from "./collapse.js";
function resolveDisplay(manifestDisplay, context) {
    if (!manifestDisplay) return {
        icon: '',
        shape: 'square',
        label: context?.display?.label || 'Unknown Node'
    };
    const isCollapsed = context?.isCollapsed ?? false;
    const shape = context?.display?.shape ?? manifestDisplay.shape;
    const collapsedShape = 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,
        icon: context?.display?.icon ?? manifestDisplay.icon ?? '',
        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;
}
export { replaceTemplateVars, resolveDisplay, resolveHandles, resolveVisibility };
