"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__, {
    StageTitleInput: ()=>StageTitleInput
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const apollo_wind_namespaceObject = require("@uipath/apollo-wind");
const external_react_namespaceObject = require("react");
const external_CanvasTooltip_cjs_namespaceObject = require("../CanvasTooltip.cjs");
const external_useStageNodeLabels_cjs_namespaceObject = require("./useStageNodeLabels.cjs");
const StageTitleInput = ({ stageId, label: labelProp, onChange, className })=>{
    const labels = (0, external_useStageNodeLabels_cjs_namespaceObject.useStageNodeLabels)();
    const isEditable = !!onChange;
    const [isEditing, setIsEditing] = (0, external_react_namespaceObject.useState)(false);
    const inputRef = (0, external_react_namespaceObject.useRef)(null);
    const [label, setLabel] = (0, external_react_namespaceObject.useState)(labelProp);
    (0, external_react_namespaceObject.useEffect)(()=>{
        setLabel(labelProp);
    }, [
        labelProp
    ]);
    (0, external_react_namespaceObject.useLayoutEffect)(()=>{
        if (isEditing && inputRef.current) {
            const input = inputRef.current;
            input.focus();
            const caret = input.value.length;
            input.setSelectionRange(caret, caret);
        }
    }, [
        isEditing
    ]);
    const handleChange = (0, external_react_namespaceObject.useCallback)((e)=>{
        setLabel(e.target.value);
    }, []);
    const saveStageTitle = (0, external_react_namespaceObject.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 = (0, external_react_namespaceObject.useCallback)(()=>{
        if (isEditing) saveStageTitle();
    }, [
        isEditing,
        saveStageTitle
    ]);
    const handleKeyDown = (0, external_react_namespaceObject.useCallback)((e)=>{
        if ('Enter' === e.key) saveStageTitle();
        if ('Escape' !== e.key) e.stopPropagation();
    }, [
        saveStageTitle
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: (0, apollo_wind_namespaceObject.cn)('py-0.5 text-sm', !isEditing && 'font-bold', className),
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CanvasTooltip_cjs_namespaceObject.CanvasTooltip, {
            content: label,
            placement: "top",
            hide: isEditing,
            delay: true,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                className: (0, apollo_wind_namespaceObject.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__*/ (0, jsx_runtime_namespaceObject.jsx)("input", {
                    ref: inputRef,
                    name: `stage-title-${stageId}`,
                    "aria-label": `Stage title: ${label}`,
                    autoComplete: "off",
                    value: label,
                    onInput: handleChange,
                    onKeyDown: handleKeyDown,
                    onBlur: handleBlur,
                    className: (0, apollo_wind_namespaceObject.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__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
                    type: "button",
                    className: (0, apollo_wind_namespaceObject.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__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                        className: "truncate",
                        children: label
                    })
                }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                    className: "inline-block max-w-full truncate rounded-sm py-1",
                    children: label
                })
            })
        })
    });
};
exports.StageTitleInput = __webpack_exports__.StageTitleInput;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "StageTitleInput"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
