import { jsx } from "react/jsx-runtime";
import { cn } from "@uipath/apollo-wind";
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
import { CanvasTooltip } from "../CanvasTooltip.js";
import { useStageNodeLabels } from "./useStageNodeLabels.js";
const StageTitleInput = ({ stageId, label: labelProp, onChange, className })=>{
    const labels = useStageNodeLabels();
    const isEditable = !!onChange;
    const [isEditing, setIsEditing] = useState(false);
    const inputRef = useRef(null);
    const [label, setLabel] = useState(labelProp);
    useEffect(()=>{
        setLabel(labelProp);
    }, [
        labelProp
    ]);
    useLayoutEffect(()=>{
        if (isEditing && inputRef.current) {
            const input = inputRef.current;
            input.focus();
            const caret = input.value.length;
            input.setSelectionRange(caret, caret);
        }
    }, [
        isEditing
    ]);
    const handleChange = useCallback((e)=>{
        setLabel(e.target.value);
    }, []);
    const saveStageTitle = useCallback(()=>{
        setIsEditing(false);
        if (!onChange) return;
        const finalLabel = '' === label.trim() ? labels.untitledStage : label;
        if (finalLabel !== label) setLabel(finalLabel);
        onChange(finalLabel);
    }, [
        onChange,
        label,
        labels.untitledStage
    ]);
    const handleBlur = useCallback(()=>{
        if (isEditing) saveStageTitle();
    }, [
        isEditing,
        saveStageTitle
    ]);
    const handleKeyDown = useCallback((e)=>{
        if ('Enter' === e.key) saveStageTitle();
        if ('Escape' !== e.key) e.stopPropagation();
    }, [
        saveStageTitle
    ]);
    return /*#__PURE__*/ jsx("div", {
        className: cn('py-0.5 text-sm', !isEditing && 'font-bold', className),
        children: /*#__PURE__*/ jsx(CanvasTooltip, {
            content: label,
            placement: "top",
            hide: isEditing,
            delay: true,
            children: /*#__PURE__*/ jsx("div", {
                className: cn('flex min-h-7 w-full items-center rounded', isEditing && 'outline outline-1 -outline-offset-1 outline-[var(--canvas-border-de-emp)]'),
                children: isEditing ? /*#__PURE__*/ jsx("input", {
                    ref: inputRef,
                    name: `stage-title-${stageId}`,
                    "aria-label": `Stage title: ${label}`,
                    autoComplete: "off",
                    value: label,
                    onInput: handleChange,
                    onKeyDown: handleKeyDown,
                    onBlur: handleBlur,
                    className: cn('nodrag w-full min-w-[100px] py-1', 'cursor-text rounded-sm border-none bg-transparent text-sm text-ellipsis', 'focus:outline-none', 'hover:bg-[var(--canvas-background-secondary)]')
                }) : isEditable ? /*#__PURE__*/ jsx("button", {
                    type: "button",
                    className: cn('nodrag flex w-full min-w-0 items-center self-stretch rounded-sm', 'appearance-none border-none bg-transparent p-0 text-left text-sm font-bold', 'cursor-text hover:bg-[var(--canvas-background-secondary)]'),
                    onClick: ()=>setIsEditing(true),
                    children: /*#__PURE__*/ jsx("span", {
                        className: "truncate",
                        children: label
                    })
                }) : /*#__PURE__*/ jsx("span", {
                    className: "inline-block max-w-full truncate rounded-sm py-1",
                    children: label
                })
            })
        })
    });
};
export { StageTitleInput };
