import { jsx, jsxs } from "react/jsx-runtime";
import { Padding, Spacing } from "@uipath/apollo-core";
import { Column, Row } from "../../layouts/index.js";
import { Badge, Button, Spinner } from "@uipath/apollo-wind";
import debounce from "debounce";
import { memo, useCallback, useMemo, useState } from "react";
import { useSafeLingui } from "../../../i18n/index.js";
import { TimelinePlayIcon } from "../../icons/index.js";
import { CanvasTooltip } from "../CanvasTooltip.js";
import { ExecutionStatusIcon } from "../ExecutionStatusIcon/index.js";
import { StageTaskIcon, StageTaskRetryDuration } from "./StageNode.styles.js";
import { StageTaskEntryConditionIcon } from "./StageTaskEntryConditionIcon.js";
import { useExecutionStatusLabel } from "./useExecutionStatusLabel.js";
const ProcessCanvasIcon = ()=>/*#__PURE__*/ jsxs("svg", {
        viewBox: "0 0 24 24",
        fill: "none",
        stroke: "currentColor",
        strokeWidth: "1.5",
        children: [
            /*#__PURE__*/ jsx("rect", {
                x: "3",
                y: "3",
                width: "7",
                height: "7",
                rx: "1"
            }),
            /*#__PURE__*/ jsx("rect", {
                x: "14",
                y: "3",
                width: "7",
                height: "7",
                rx: "1"
            }),
            /*#__PURE__*/ jsx("rect", {
                x: "3",
                y: "14",
                width: "7",
                height: "7",
                rx: "1"
            }),
            /*#__PURE__*/ jsx("rect", {
                x: "14",
                y: "14",
                width: "7",
                height: "7",
                rx: "1"
            })
        ]
    });
const TaskPlayButton = /*#__PURE__*/ memo(({ taskId, onTaskPlay, small })=>{
    const [playLoading, setPlayLoading] = useState(false);
    const debouncedTaskPlay = useMemo(()=>debounce(async (id)=>{
            setPlayLoading(true);
            try {
                await onTaskPlay(id);
            } catch  {} finally{
                setPlayLoading(false);
            }
        }, 500, {
            immediate: true
        }), [
        onTaskPlay
    ]);
    const handlePlayClick = useCallback((e)=>{
        e.stopPropagation();
        e.preventDefault();
        debouncedTaskPlay(taskId);
    }, [
        debouncedTaskPlay,
        taskId
    ]);
    const iconSize = small ? 18 : 22;
    const buttonSize = small ? '20px' : Spacing.SpacingL;
    return /*#__PURE__*/ jsx(CanvasTooltip, {
        content: "Trigger task",
        placement: "top",
        children: /*#__PURE__*/ jsx(Button, {
            variant: "ghost",
            size: "icon",
            "data-testid": `stage-task-play-${taskId}`,
            onClick: handlePlayClick,
            onMouseDown: (e)=>e.stopPropagation(),
            onKeyDown: (e)=>e.stopPropagation(),
            className: "task-menu-icon-button",
            style: {
                color: 'var(--canvas-icon-default)',
                minWidth: 'unset',
                width: buttonSize,
                height: buttonSize,
                padding: 0,
                ...small && {
                    marginRight: '-2px'
                }
            },
            children: playLoading ? /*#__PURE__*/ jsx(Spinner, {
                size: "sm",
                style: {
                    width: iconSize - 2,
                    height: iconSize - 2
                }
            }) : /*#__PURE__*/ jsx(TimelinePlayIcon, {
                w: iconSize,
                h: iconSize
            })
        })
    });
});
const TaskContent = /*#__PURE__*/ memo(({ task, taskExecution, isDragging, onTaskPlay })=>{
    const { _ } = useSafeLingui();
    const getStatusName = useExecutionStatusLabel();
    const hasExecutionStatus = !!taskExecution?.status;
    const badgeText = useMemo(()=>{
        if (!taskExecution?.badge) return;
        if (taskExecution.retryCount && taskExecution.retryCount > 1) {
            if ('InProgress' === taskExecution.status) return _({
                id: 'stage-node.task-badge.running-again',
                message: 'Running again'
            });
            return _({
                id: 'stage-node.task-badge.ran-n-times',
                message: '{count, plural, one {Ran # time} other {Ran # times}}',
                values: {
                    count: taskExecution.retryCount
                }
            });
        }
        return taskExecution.badge;
    }, [
        taskExecution?.badge,
        taskExecution?.retryCount,
        taskExecution?.status,
        _
    ]);
    const hasSecondRowContent = taskExecution && (taskExecution.duration || taskExecution.retryDuration || badgeText);
    const showPlayButtonSmall = onTaskPlay && hasExecutionStatus;
    const taskStatusFallbackName = hasExecutionStatus ? getStatusName(taskExecution?.status) : '';
    const taskStatusTooltip = taskExecution?.message || taskStatusFallbackName;
    const durationLabel = useMemo(()=>taskExecution?.duration ? /*#__PURE__*/ jsx("span", {
            className: "text-xs text-foreground-muted",
            children: taskExecution.duration
        }) : null, [
        taskExecution?.duration
    ]);
    return /*#__PURE__*/ jsxs(Column, {
        flex: 1,
        style: {
            overflow: 'hidden',
            textOverflow: 'ellipsis',
            whiteSpace: 'nowrap'
        },
        gap: Padding.PadXs,
        children: [
            /*#__PURE__*/ jsxs(Row, {
                align: "center",
                justify: "space-between",
                gap: Spacing.SpacingXs,
                children: [
                    /*#__PURE__*/ jsxs(Row, {
                        gap: Spacing.SpacingXs,
                        align: "center",
                        style: {
                            overflow: 'hidden',
                            textOverflow: 'ellipsis',
                            whiteSpace: 'nowrap'
                        },
                        children: [
                            /*#__PURE__*/ jsx(StageTaskIcon, {
                                children: task.icon ?? /*#__PURE__*/ jsx(ProcessCanvasIcon, {})
                            }),
                            /*#__PURE__*/ jsx(CanvasTooltip, {
                                content: task.label,
                                placement: "top",
                                smartTooltip: true,
                                ...isDragging && {
                                    isOpen: false
                                },
                                children: /*#__PURE__*/ jsxs(Row, {
                                    gap: '2px',
                                    align: "center",
                                    style: {
                                        overflow: 'hidden',
                                        textOverflow: 'ellipsis',
                                        whiteSpace: 'nowrap'
                                    },
                                    children: [
                                        /*#__PURE__*/ jsx("span", {
                                            className: "text-sm truncate",
                                            style: {
                                                minWidth: 0
                                            },
                                            children: task.label
                                        }),
                                        task.isRequired && /*#__PURE__*/ jsx("span", {
                                            className: "text-sm",
                                            style: {
                                                flexShrink: 0
                                            },
                                            children: '*'
                                        })
                                    ]
                                })
                            })
                        ]
                    }),
                    /*#__PURE__*/ jsxs(Row, {
                        align: "center",
                        gap: Spacing.SpacingXs,
                        style: {
                            flexShrink: 0
                        },
                        children: [
                            hasExecutionStatus && /*#__PURE__*/ jsx(CanvasTooltip, {
                                content: taskStatusTooltip,
                                placement: "top",
                                children: /*#__PURE__*/ jsx(Button, {
                                    variant: "ghost",
                                    size: "icon",
                                    className: "h-4 w-4",
                                    "aria-label": taskStatusTooltip,
                                    children: /*#__PURE__*/ jsx(ExecutionStatusIcon, {
                                        status: taskExecution.status
                                    })
                                })
                            }),
                            !hasSecondRowContent && /*#__PURE__*/ jsx(StageTaskEntryConditionIcon, {
                                task: task,
                                small: !!hasExecutionStatus
                            }),
                            showPlayButtonSmall && !hasSecondRowContent && /*#__PURE__*/ jsx(TaskPlayButton, {
                                taskId: task.id,
                                onTaskPlay: onTaskPlay,
                                small: true
                            }),
                            onTaskPlay && !hasExecutionStatus && /*#__PURE__*/ jsx(TaskPlayButton, {
                                taskId: task.id,
                                onTaskPlay: onTaskPlay
                            })
                        ]
                    })
                ]
            }),
            taskExecution && hasSecondRowContent && /*#__PURE__*/ jsxs(Row, {
                align: "center",
                justify: "space-between",
                children: [
                    /*#__PURE__*/ jsxs(Row, {
                        gap: '2px',
                        children: [
                            durationLabel && (taskExecution?.durationTooltip ? /*#__PURE__*/ jsx(CanvasTooltip, {
                                content: taskExecution.durationTooltip,
                                placement: "top",
                                hide: isDragging,
                                children: durationLabel
                            }) : durationLabel),
                            taskExecution?.retryDuration && /*#__PURE__*/ jsx(StageTaskRetryDuration, {
                                status: taskExecution.badgeStatus ?? 'warning',
                                children: /*#__PURE__*/ jsx("span", {
                                    className: "text-xs",
                                    children: `(+${taskExecution.retryDuration})`
                                })
                            })
                        ]
                    }),
                    /*#__PURE__*/ jsxs(Row, {
                        align: "center",
                        gap: Spacing.SpacingXs,
                        children: [
                            badgeText && /*#__PURE__*/ jsx(Badge, {
                                variant: taskExecution.badgeStatus,
                                children: badgeText
                            }),
                            /*#__PURE__*/ jsx(StageTaskEntryConditionIcon, {
                                task: task,
                                small: true
                            }),
                            showPlayButtonSmall && /*#__PURE__*/ jsx(TaskPlayButton, {
                                taskId: task.id,
                                onTaskPlay: onTaskPlay,
                                small: true
                            })
                        ]
                    })
                ]
            })
        ]
    });
});
export { TaskContent };
