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 { TimelinePlayIcon } from "../../icons/index.js";
import { CanvasTooltip } from "../CanvasTooltip.js";
import { ExecutionStatusIcon } from "../ExecutionStatusIcon/index.js";
import { StageTaskIcon, StageTaskRetryDuration } from "./StageNode.styles.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 generateBadgeText = (taskExecution)=>{
    if (!taskExecution.badge) return;
    if (taskExecution.retryCount && taskExecution.retryCount > 1) return `${taskExecution.badge} x${taskExecution.retryCount}`;
    return taskExecution.badge;
};
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, isReadOnly })=>{
    const hasExecutionStatus = !!taskExecution?.status;
    const hasSecondRowContent = taskExecution && (taskExecution.duration || taskExecution.retryDuration || taskExecution.badge);
    const showPlayButtonSmall = onTaskPlay && hasExecutionStatus;
    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",
                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__*/ jsx("span", {
                                    className: "text-sm truncate",
                                    children: task.label
                                })
                            })
                        ]
                    }),
                    /*#__PURE__*/ jsxs(Row, {
                        align: "center",
                        gap: Spacing.SpacingXs,
                        style: {
                            flexShrink: 0
                        },
                        children: [
                            hasExecutionStatus && (taskExecution.message ? /*#__PURE__*/ jsx(CanvasTooltip, {
                                content: taskExecution.message,
                                placement: "top",
                                children: /*#__PURE__*/ jsx(Button, {
                                    variant: "ghost",
                                    size: "icon",
                                    className: "h-6 w-6",
                                    "aria-label": taskExecution.message,
                                    children: /*#__PURE__*/ jsx(ExecutionStatusIcon, {
                                        status: taskExecution.status
                                    })
                                })
                            }) : !isReadOnly && /*#__PURE__*/ jsx(ExecutionStatusIcon, {
                                status: taskExecution.status
                            })),
                            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: [
                            taskExecution?.duration && /*#__PURE__*/ jsx("span", {
                                className: "text-xs text-foreground-muted",
                                children: taskExecution.duration
                            }),
                            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: [
                            taskExecution?.badge && /*#__PURE__*/ jsx(Badge, {
                                variant: taskExecution.badgeStatus,
                                children: generateBadgeText(taskExecution) ?? ''
                            }),
                            showPlayButtonSmall && /*#__PURE__*/ jsx(TaskPlayButton, {
                                taskId: task.id,
                                onTaskPlay: onTaskPlay,
                                small: true
                            })
                        ]
                    })
                ]
            })
        ]
    });
});
export { TaskContent };
