import { jsx, jsxs } from "react/jsx-runtime";
import { Icon, Padding, Spacing } from "@uipath/apollo-core";
import { Row } from "../../layouts/index.js";
import { Button } from "@uipath/apollo-wind";
import { memo } from "react";
import { EntryConditionIcon, ExitConditionIcon, ReturnToOriginIcon } from "../../icons/index.js";
import { CanvasIcon } from "../../utils/icon-registry.js";
import { CanvasTooltip } from "../CanvasTooltip.js";
import { ExecutionStatusIcon } from "../ExecutionStatusIcon/index.js";
import { getExecutionStatusColor } from "../ExecutionStatusIcon/ExecutionStatusIcon.js";
import { StageChip, StageHeader } from "./StageNode.styles.js";
import { StageHeaderChipType } from "./StageNode.types.js";
import { StageTitleInput } from "./StageTitleInput.js";
import { useStageNodeLabels } from "./useStageNodeLabels.js";
const SLA_ICON_CONFIG = {
    warning: {
        icon: 'triangle-alert',
        iconColor: 'var(--canvas-warning-icon)'
    },
    error: {
        icon: 'circle-alert',
        iconColor: 'var(--canvas-error-icon)'
    }
};
const CHIP_ICONS = {
    [StageHeaderChipType.Entry]: /*#__PURE__*/ jsx(EntryConditionIcon, {
        w: Icon.IconXs,
        h: Icon.IconXs
    }),
    [StageHeaderChipType.Exit]: /*#__PURE__*/ jsx(ExitConditionIcon, {
        w: Icon.IconXs,
        h: Icon.IconXs
    }),
    [StageHeaderChipType.ReturnToOrigin]: /*#__PURE__*/ jsx(ReturnToOriginIcon, {
        w: Icon.IconXs,
        h: Icon.IconXs
    }),
    [StageHeaderChipType.CaseExit]: /*#__PURE__*/ jsx(CanvasIcon, {
        icon: "x",
        size: 16
    }),
    [StageHeaderChipType.CaseCompletion]: /*#__PURE__*/ jsx(CanvasIcon, {
        icon: "check",
        size: 16
    })
};
const StageNodeHeaderInner = ({ props, isReadOnly, isException, status, handleTaskAddClick })=>{
    const labels = useStageNodeLabels();
    const { id, stageDetails, execution, 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 slaText = execution?.stageStatus?.slaText;
    const slaIcon = execution?.stageStatus?.slaIcon;
    const slaIndicator = slaIcon ? SLA_ICON_CONFIG[slaIcon] : void 0;
    return /*#__PURE__*/ jsxs(StageHeader, {
        isException: isException,
        "data-testid": `stage-header-${id}`,
        children: [
            /*#__PURE__*/ jsxs("div", {
                className: "flex items-start justify-between gap-1",
                children: [
                    /*#__PURE__*/ jsxs(Row, {
                        gap: Spacing.SpacingMicro,
                        align: "center",
                        flex: 1,
                        minW: 0,
                        children: [
                            icon,
                            /*#__PURE__*/ jsx(StageTitleInput, {
                                stageId: id,
                                label: stageDetails.label,
                                onChange: isReadOnly ? void 0 : onStageTitleChange,
                                className: "flex-1 min-w-0"
                            })
                        ]
                    }),
                    /*#__PURE__*/ jsxs(Row, {
                        gap: Spacing.SpacingMicro,
                        align: "start",
                        py: Padding.PadS,
                        children: [
                            status && /*#__PURE__*/ jsx(CanvasTooltip, {
                                content: statusLabel,
                                placement: "top",
                                children: /*#__PURE__*/ jsx(Button, {
                                    variant: "ghost",
                                    size: "icon",
                                    className: "h-6 w-6",
                                    "aria-label": statusLabel,
                                    children: 'NotExecuted' === status ? /*#__PURE__*/ jsx(CanvasIcon, {
                                        icon: "hourglass",
                                        size: 20,
                                        color: getExecutionStatusColor('NotExecuted')
                                    }) : /*#__PURE__*/ jsx(ExecutionStatusIcon, {
                                        status: status,
                                        size: 20
                                    })
                                })
                            }),
                            (onTaskAdd || onAddTaskFromToolbox) && !isReadOnly && /*#__PURE__*/ jsx(CanvasTooltip, {
                                content: labels.addTask,
                                placement: "top",
                                children: /*#__PURE__*/ jsx(Button, {
                                    variant: "ghost",
                                    size: "icon",
                                    className: "h-6 w-6",
                                    onClick: handleTaskAddClick,
                                    "aria-label": labels.addTask,
                                    disabled: isAddTaskDisabled,
                                    children: /*#__PURE__*/ jsx(CanvasIcon, {
                                        icon: "plus",
                                        size: 20
                                    })
                                })
                            })
                        ]
                    })
                ]
            }),
            (slaText || stageDetails.headerChips && stageDetails.headerChips.length > 0) && /*#__PURE__*/ jsxs("div", {
                className: "mt-1 flex flex-wrap items-center justify-between gap-2",
                children: [
                    slaText && /*#__PURE__*/ jsxs("span", {
                        className: "inline-flex items-center gap-1 text-xs text-foreground-muted",
                        "data-testid": `stage-sla-${id}`,
                        "data-sla-icon": slaIcon,
                        children: [
                            slaIndicator && /*#__PURE__*/ jsx(CanvasIcon, {
                                icon: slaIndicator.icon,
                                size: 16,
                                color: slaIndicator.iconColor
                            }),
                            slaText
                        ]
                    }),
                    stageDetails.headerChips && stageDetails.headerChips.length > 0 && /*#__PURE__*/ jsx("div", {
                        className: "flex flex-wrap items-center gap-1",
                        children: stageDetails.headerChips.map((chip)=>{
                            const button = /*#__PURE__*/ jsxs(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__*/ jsx("span", {
                                        className: "text-xs",
                                        children: chip.count
                                    })
                                ]
                            }, chip.type);
                            if (chip.tooltip) return /*#__PURE__*/ jsx(CanvasTooltip, {
                                placement: "bottom",
                                content: chip.tooltip,
                                children: button
                            }, chip.type);
                            return button;
                        })
                    })
                ]
            }),
            stageDuration && /*#__PURE__*/ jsx("span", {
                className: "mt-1 text-xs text-foreground-muted",
                children: stageDuration
            })
        ]
    });
};
const StageNodeHeader = /*#__PURE__*/ memo(StageNodeHeaderInner);
export { StageNodeHeader };
