"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__, {
    StageNodeHeader: ()=>StageNodeHeader
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const apollo_core_namespaceObject = require("@uipath/apollo-core");
const index_cjs_namespaceObject = require("../../layouts/index.cjs");
const apollo_wind_namespaceObject = require("@uipath/apollo-wind");
const external_react_namespaceObject = require("react");
const external_icons_index_cjs_namespaceObject = require("../../icons/index.cjs");
const icon_registry_cjs_namespaceObject = require("../../utils/icon-registry.cjs");
const external_CanvasTooltip_cjs_namespaceObject = require("../CanvasTooltip.cjs");
const external_ExecutionStatusIcon_index_cjs_namespaceObject = require("../ExecutionStatusIcon/index.cjs");
const ExecutionStatusIcon_cjs_namespaceObject = require("../ExecutionStatusIcon/ExecutionStatusIcon.cjs");
const external_StageNode_styles_cjs_namespaceObject = require("./StageNode.styles.cjs");
const external_StageNode_types_cjs_namespaceObject = require("./StageNode.types.cjs");
const CHIP_ICONS = {
    [external_StageNode_types_cjs_namespaceObject.StageHeaderChipType.Entry]: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_icons_index_cjs_namespaceObject.EntryConditionIcon, {
        w: apollo_core_namespaceObject.Icon.IconXs,
        h: apollo_core_namespaceObject.Icon.IconXs
    }),
    [external_StageNode_types_cjs_namespaceObject.StageHeaderChipType.Exit]: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_icons_index_cjs_namespaceObject.ExitConditionIcon, {
        w: apollo_core_namespaceObject.Icon.IconXs,
        h: apollo_core_namespaceObject.Icon.IconXs
    }),
    [external_StageNode_types_cjs_namespaceObject.StageHeaderChipType.ReturnToOrigin]: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_icons_index_cjs_namespaceObject.ReturnToOriginIcon, {
        w: apollo_core_namespaceObject.Icon.IconXs,
        h: apollo_core_namespaceObject.Icon.IconXs
    }),
    [external_StageNode_types_cjs_namespaceObject.StageHeaderChipType.CaseExit]: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
        icon: "x",
        size: 16
    }),
    [external_StageNode_types_cjs_namespaceObject.StageHeaderChipType.CaseCompletion]: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
        icon: "check",
        size: 16
    })
};
const StageNodeHeaderInner = ({ props, isReadOnly, isException, status, handleTaskAddClick })=>{
    const { id, stageDetails, execution, addTaskLabel = 'Add task', onTaskAdd, onAddTaskFromToolbox, onStageTitleChange, loadingTaskIds } = props;
    const isAddTaskDisabled = (loadingTaskIds?.size ?? 0) > 0;
    const icon = stageDetails?.icon;
    const statusLabel = execution?.stageStatus?.label;
    const stageDuration = execution?.stageStatus?.duration;
    const isStageTitleEditable = !!onStageTitleChange && !isReadOnly;
    const [isStageTitleEditing, setIsStageTitleEditing] = (0, external_react_namespaceObject.useState)(false);
    const stageTitleRef = (0, external_react_namespaceObject.useRef)(null);
    const [label, setLabel] = (0, external_react_namespaceObject.useState)(props.stageDetails.label);
    (0, external_react_namespaceObject.useEffect)(()=>{
        setLabel(props.stageDetails.label);
    }, [
        props.stageDetails.label
    ]);
    const handleStageTitleChange = (0, external_react_namespaceObject.useCallback)((e)=>{
        setIsStageTitleEditing(true);
        setLabel(e.target.value);
    }, []);
    const handleStageTitleClickToSave = (0, external_react_namespaceObject.useCallback)((e)=>{
        if (isStageTitleEditing && !stageTitleRef.current?.contains(e.target)) {
            setIsStageTitleEditing(false);
            if (onStageTitleChange) {
                if ('' === label.trim()) setLabel('Untitled Stage');
                onStageTitleChange(label);
            }
        }
    }, [
        isStageTitleEditing,
        onStageTitleChange,
        label
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (isStageTitleEditing) document.addEventListener('click', handleStageTitleClickToSave);
        return ()=>{
            document.removeEventListener('click', handleStageTitleClickToSave);
        };
    }, [
        handleStageTitleClickToSave,
        isStageTitleEditing
    ]);
    const handleStageTitleBlurToSave = (0, external_react_namespaceObject.useCallback)(()=>{
        if (isStageTitleEditing) {
            setIsStageTitleEditing(false);
            if (onStageTitleChange) {
                if ('' === label.trim()) setLabel('Untitled Stage');
                onStageTitleChange(label);
            }
        }
    }, [
        isStageTitleEditing,
        onStageTitleChange,
        label
    ]);
    const handleStageTitleKeyDown = (0, external_react_namespaceObject.useCallback)((e)=>{
        if ('Enter' === e.key) {
            setIsStageTitleEditing(false);
            if (onStageTitleChange) onStageTitleChange(label);
        }
        if ('Escape' !== e.key) e.stopPropagation();
    }, [
        onStageTitleChange,
        label
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_StageNode_styles_cjs_namespaceObject.StageHeader, {
        isException: isException,
        "data-testid": `stage-header-${id}`,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(index_cjs_namespaceObject.Row, {
                gap: apollo_core_namespaceObject.Spacing.SpacingMicro,
                align: "center",
                flex: 1,
                minW: 0,
                children: [
                    icon,
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(index_cjs_namespaceObject.Column, {
                        py: 2,
                        flex: 1,
                        minW: 0,
                        children: [
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                                className: (0, apollo_wind_namespaceObject.cn)('text-sm', !isStageTitleEditing && 'font-bold'),
                                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CanvasTooltip_cjs_namespaceObject.CanvasTooltip, {
                                    content: label,
                                    placement: "top",
                                    delay: true,
                                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNode_styles_cjs_namespaceObject.StageTitleContainer, {
                                        isEditing: isStageTitleEditing,
                                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNode_styles_cjs_namespaceObject.StageTitleInput, {
                                            name: "Stage Title",
                                            isStageTitleEditable: isStageTitleEditable,
                                            value: label,
                                            ref: stageTitleRef,
                                            isEditing: isStageTitleEditing,
                                            ...onStageTitleChange && {
                                                onFocus: ()=>setIsStageTitleEditing(true),
                                                onInput: handleStageTitleChange,
                                                onKeyDown: handleStageTitleKeyDown,
                                                onBlur: handleStageTitleBlurToSave
                                            },
                                            readOnly: !isStageTitleEditable
                                        })
                                    })
                                })
                            }),
                            stageDuration && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                                className: "text-xs text-foreground-muted",
                                children: stageDuration
                            }),
                            stageDetails.headerChips && stageDetails.headerChips.length > 0 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNode_styles_cjs_namespaceObject.StageHeaderChipsRow, {
                                children: stageDetails.headerChips.map((chip)=>{
                                    const button = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_StageNode_styles_cjs_namespaceObject.StageChip, {
                                        type: "button",
                                        "aria-label": 'string' == typeof chip.tooltip ? chip.tooltip : chip.type,
                                        onClick: (e)=>{
                                            e.stopPropagation();
                                            chip.onClick?.();
                                        },
                                        children: [
                                            CHIP_ICONS[chip.type],
                                            void 0 !== chip.count && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                                                className: "text-xs",
                                                children: chip.count
                                            })
                                        ]
                                    }, chip.type);
                                    if (chip.tooltip) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CanvasTooltip_cjs_namespaceObject.CanvasTooltip, {
                                        placement: "bottom",
                                        content: chip.tooltip,
                                        children: button
                                    }, chip.type);
                                    return button;
                                })
                            })
                        ]
                    })
                ]
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(index_cjs_namespaceObject.Row, {
                gap: apollo_core_namespaceObject.Spacing.SpacingMicro,
                align: "start",
                py: apollo_core_namespaceObject.Padding.PadS,
                children: [
                    status && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CanvasTooltip_cjs_namespaceObject.CanvasTooltip, {
                        content: statusLabel,
                        placement: "top",
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(apollo_wind_namespaceObject.Button, {
                            variant: "ghost",
                            size: "icon",
                            className: "h-6 w-6",
                            "aria-label": statusLabel,
                            children: 'NotExecuted' === status ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
                                icon: "hourglass",
                                size: 20,
                                color: (0, ExecutionStatusIcon_cjs_namespaceObject.getExecutionStatusColor)('NotExecuted')
                            }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_ExecutionStatusIcon_index_cjs_namespaceObject.ExecutionStatusIcon, {
                                status: status,
                                size: 20
                            })
                        })
                    }),
                    (onTaskAdd || onAddTaskFromToolbox) && !isReadOnly && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CanvasTooltip_cjs_namespaceObject.CanvasTooltip, {
                        content: addTaskLabel,
                        placement: "top",
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(apollo_wind_namespaceObject.Button, {
                            variant: "ghost",
                            size: "icon",
                            className: "h-6 w-6",
                            onClick: handleTaskAddClick,
                            "aria-label": addTaskLabel,
                            disabled: isAddTaskDisabled,
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
                                icon: "plus",
                                size: 20
                            })
                        })
                    })
                ]
            })
        ]
    });
};
const StageNodeHeader = /*#__PURE__*/ (0, external_react_namespaceObject.memo)(StageNodeHeaderInner);
exports.StageNodeHeader = __webpack_exports__.StageNodeHeader;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "StageNodeHeader"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
