"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Link = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const icons_1 = require("@gravity-ui/icons");
const uikit_1 = require("@gravity-ui/uikit");
const classname_1 = require("../../../../../classname.js");
const TextInput_1 = require("../../../../../forms/TextInput.js");
const forms_1 = require("../../../../../i18n/forms/index.js");
const handlers_1 = require("../../../../../utils/handlers.js");
require("./TooltipView.css");
const b = (0, classname_1.cn)('link-tooltip-view');
exports.Link = (0, react_1.memo)(function Link({ href, anchorElement, placement, onOpenChange, onChange, onRemove, onUrlChange, autoFocus, onOpenInNewTab, }) {
    const [url, setUrl] = (0, react_1.useState)(href);
    const handleUrlUpdate = (v) => {
        setUrl(v);
        onUrlChange?.(v);
    };
    const handleSubmit = () => {
        onChange?.({ href: url });
    };
    const inputEnterKeyHandler = (0, handlers_1.enterKeyHandler)(handleSubmit);
    (0, react_1.useEffect)(() => {
        setUrl(href);
        onUrlChange?.(href);
    }, [href, onUrlChange]);
    return ((0, jsx_runtime_1.jsx)(uikit_1.Popup, { open: true, anchorElement: anchorElement, className: b('popup'), placement: placement, onOpenChange: onOpenChange, children: (0, jsx_runtime_1.jsxs)("div", { className: b(), children: [(0, jsx_runtime_1.jsx)(TextInput_1.TextInputFixed, { size: "l", hasClear: true, view: "clear", value: url, className: b('input', { empty: !url }), onUpdate: handleUrlUpdate, placeholder: (0, forms_1.i18n)('link-href-placeholder'), autoFocus: autoFocus, onKeyDown: inputEnterKeyHandler }), url && onRemove && ((0, jsx_runtime_1.jsx)(uikit_1.ActionTooltip, { title: (0, forms_1.i18n)('link_remove_help'), children: (0, jsx_runtime_1.jsx)(uikit_1.Button, { className: b('button'), view: "flat", size: "m", onClick: onRemove, children: (0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: icons_1.LinkSlash, size: 16 }) }) })), url && ((0, jsx_runtime_1.jsx)(uikit_1.ActionTooltip, { title: (0, forms_1.i18n)('link_open_help'), children: (0, jsx_runtime_1.jsx)(uikit_1.Button, { className: b('button'), view: "flat", size: "m", href: url, target: "_blank", rel: "noopener noreferrer", onClick: onOpenInNewTab, children: (0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: icons_1.ArrowUpRightFromSquare, size: 16 }) }) }))] }) }));
});
//# sourceMappingURL=TooltipView.js.map
