"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__, {
    BaseTextContainer: ()=>BaseTextContainer,
    NodeLabel: ()=>NodeLabel
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const external_constants_cjs_namespaceObject = require("../../constants.cjs");
const CssUtil_cjs_namespaceObject = require("../../utils/CssUtil.cjs");
const external_CanvasTooltip_cjs_namespaceObject = require("../CanvasTooltip.cjs");
const BaseTextContainer = ({ hasBottomHandles, shape, children })=>{
    if ('rectangle' === shape) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: "flex flex-1 min-w-0 flex-col items-start text-left",
        children: children
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: (0, CssUtil_cjs_namespaceObject.cx)('absolute left-1/2 w-[150%] flex flex-col z-10 transition-transform duration-200', hasBottomHandles ? 'items-start text-left translate-x-[20%] translate-y-1/2' : 'items-center text-center -translate-x-1/2 translate-y-full'),
        style: {
            bottom: hasBottomHandles ? external_constants_cjs_namespaceObject.NODE_TEXT_BOTTOM_OFFSET_WITH_HANDLES : external_constants_cjs_namespaceObject.NODE_TEXT_BOTTOM_OFFSET
        },
        children: children
    });
};
const ConditionalTooltip = ({ content, children })=>{
    if (!content) return children;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CanvasTooltip_cjs_namespaceObject.CanvasTooltip, {
        delay: true,
        placement: "top",
        content: content,
        smartTooltip: true,
        children: children
    });
};
const Header = ({ shape, backgroundColor, onDoubleClick, children, 'data-testid': dataTestId })=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        "data-testid": dataTestId,
        onDoubleClick: onDoubleClick,
        className: (0, CssUtil_cjs_namespaceObject.cx)('text-center text-sm leading-[18px] font-semibold text-foreground overflow-hidden', backgroundColor && 'px-1.5 py-0.5 rounded-sm', 'rectangle' === shape ? 'w-full text-left whitespace-nowrap text-ellipsis' : 'wrap-break-word line-clamp-3'),
        style: backgroundColor ? {
            backgroundColor
        } : void 0,
        children: children
    });
const SubHeader = ({ shape, onDoubleClick, children, 'data-testid': dataTestId })=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        "data-testid": dataTestId,
        onDoubleClick: onDoubleClick,
        className: (0, CssUtil_cjs_namespaceObject.cx)('text-center text-xs leading-[18px] text-foreground-muted wrap-break-word overflow-hidden', 'rectangle' === shape ? 'w-full text-left line-clamp-2' : 'line-clamp-5'),
        children: children
    });
const EditableLabel = /*#__PURE__*/ (0, external_react_namespaceObject.forwardRef)(({ shape, backgroundColor, variant, value, placeholder, rows, onChange, onKeyDown, onBlur, 'aria-label': ariaLabel }, ref)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("textarea", {
        ref: ref,
        value: value,
        placeholder: placeholder,
        rows: rows,
        "aria-label": ariaLabel,
        onChange: onChange,
        onKeyDown: onKeyDown,
        onBlur: onBlur,
        className: (0, CssUtil_cjs_namespaceObject.cx)('nodrag nowheel resize-none font-[inherit] text-foreground border-none rounded-sm outline-1 outline-dashed outline-border-de-emp max-w-full', 'subtext' === variant ? 'text-xs leading-[18px] font-normal mb-0' : 'text-sm leading-[18px] font-semibold mb-0.5', 'rectangle' === shape ? 'field-sizing-fixed w-full' : 'field-sizing-content text-center', backgroundColor && 'px-1.5 py-0.5'),
        style: backgroundColor ? {
            backgroundColor
        } : void 0
    }));
EditableLabel.displayName = 'EditableLabel';
const EmptyLabelPlaceholder = ({ onDoubleClick })=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
        type: "button",
        onDoubleClick: onDoubleClick,
        className: "nodrag nowheel text-sm leading-[18px] font-semibold text-foreground-muted bg-transparent border border-dashed border-border-de-emp rounded-sm cursor-pointer opacity-0 transition-opacity duration-200 min-w-5 min-h-5 hover:opacity-100",
        "aria-label": "Add node label",
        "data-testid": "empty-label-placeholder"
    });
const NodeLabelInternal = ({ label = '', subLabel = '', labelTooltip, labelBackgroundColor, shape, hasBottomHandles, selected, dragging, centerAdornment, readonly, onChange })=>{
    const [isEditing, setIsEditing] = (0, external_react_namespaceObject.useState)(false);
    const [localLabel, setLocalLabel] = (0, external_react_namespaceObject.useState)('');
    const [localSubLabel, setLocalSubLabel] = (0, external_react_namespaceObject.useState)('');
    const [focusTarget, setFocusTarget] = (0, external_react_namespaceObject.useState)(null);
    const labelInputRef = (0, external_react_namespaceObject.useRef)(null);
    const subLabelInputRef = (0, external_react_namespaceObject.useRef)(null);
    const handleSave = (0, external_react_namespaceObject.useCallback)(()=>{
        setIsEditing(false);
        setFocusTarget(null);
        if (localLabel !== label || localSubLabel !== subLabel) onChange?.({
            label: localLabel,
            subLabel: localSubLabel
        });
    }, [
        localLabel,
        localSubLabel,
        label,
        subLabel,
        onChange
    ]);
    const startEditing = (0, external_react_namespaceObject.useCallback)((targetRef)=>{
        setIsEditing(true);
        setLocalLabel(label);
        setLocalSubLabel(subLabel);
        setFocusTarget(targetRef);
    }, [
        label,
        subLabel
    ]);
    const handleDoubleClick = (0, external_react_namespaceObject.useCallback)((targetRef)=>(e)=>{
            e.stopPropagation();
            startEditing(targetRef);
        }, [
        startEditing
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (isEditing && focusTarget?.current) {
            focusTarget.current.focus();
            focusTarget.current.select();
        }
    }, [
        isEditing,
        focusTarget
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (isEditing && (!selected || dragging || readonly)) handleSave();
    }, [
        selected,
        dragging,
        isEditing,
        readonly,
        handleSave
    ]);
    const handleCancel = (0, external_react_namespaceObject.useCallback)(()=>{
        setIsEditing(false);
        setFocusTarget(null);
        setLocalLabel(label);
        setLocalSubLabel(subLabel);
    }, [
        label,
        subLabel
    ]);
    const handleKeyDown = (0, external_react_namespaceObject.useCallback)((e)=>{
        if ('Enter' === e.key) {
            e.preventDefault();
            e.stopPropagation();
            handleSave();
        } else if ('Escape' === e.key) {
            e.preventDefault();
            e.stopPropagation();
            handleCancel();
        }
    }, [
        handleSave,
        handleCancel
    ]);
    const handleBlur = (0, external_react_namespaceObject.useCallback)((e)=>{
        const relatedTarget = e.relatedTarget;
        if (relatedTarget === labelInputRef.current || relatedTarget === subLabelInputRef.current) return;
        handleSave();
    }, [
        handleSave
    ]);
    if (!label && !subLabel && !isEditing) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(BaseTextContainer, {
        hasBottomHandles: hasBottomHandles,
        shape: shape,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(EmptyLabelPlaceholder, {
                onDoubleClick: readonly ? void 0 : handleDoubleClick(labelInputRef)
            }),
            centerAdornment
        ]
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(BaseTextContainer, {
        hasBottomHandles: hasBottomHandles,
        shape: shape,
        children: [
            isEditing ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(EditableLabel, {
                        ref: labelInputRef,
                        value: localLabel,
                        onChange: (e)=>setLocalLabel(e.target.value),
                        onKeyDown: handleKeyDown,
                        onBlur: handleBlur,
                        shape: shape,
                        variant: "normal",
                        backgroundColor: labelBackgroundColor,
                        placeholder: "Name",
                        rows: 'rectangle' === shape ? 1 : void 0,
                        "aria-label": "Edit node name"
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(EditableLabel, {
                        ref: subLabelInputRef,
                        value: localSubLabel,
                        onChange: (e)=>setLocalSubLabel(e.target.value),
                        onKeyDown: handleKeyDown,
                        onBlur: handleBlur,
                        shape: shape,
                        variant: "subtext",
                        placeholder: "Description",
                        rows: 'rectangle' === shape ? 2 : void 0,
                        "aria-label": "Edit node description"
                    })
                ]
            }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(ConditionalTooltip, {
                content: labelTooltip,
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Header, {
                        shape: shape,
                        backgroundColor: labelBackgroundColor,
                        onDoubleClick: readonly ? void 0 : handleDoubleClick(labelInputRef),
                        "data-testid": "node-label",
                        children: label
                    }),
                    subLabel && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(SubHeader, {
                        shape: shape,
                        onDoubleClick: readonly ? void 0 : handleDoubleClick(subLabelInputRef),
                        "data-testid": "node-sublabel",
                        children: subLabel
                    })
                ]
            }),
            centerAdornment
        ]
    });
};
const NodeLabel = /*#__PURE__*/ (0, external_react_namespaceObject.memo)(NodeLabelInternal);
exports.BaseTextContainer = __webpack_exports__.BaseTextContainer;
exports.NodeLabel = __webpack_exports__.NodeLabel;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "BaseTextContainer",
    "NodeLabel"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
