"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _dropdown = require("../../../dropdown");
var _input = require("../../../input");
var _button = require("../../../button");
var _useInputValidation = require("../../../hooks/use-input-validation");
var _textEditorConstants = require("../../text-editor-constants");
var _textEditorService = _interopRequireDefault(require("../../text-editor-service"));
var _textEditorContext = _interopRequireDefault(require("../../text-editor-context"));
var _editorButton = _interopRequireDefault(require("./editor-button"));
var _textEditorHooks = require("../../text-editor-hooks");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function LinkButton({
  icon,
  tooltip,
  urlIsRequiredMessage,
  enterValidUrlMessage,
  saveButtonText,
  validationErrorWarningMessage,
  ariaLabel
}) {
  const defaultTranslations = (0, _textEditorHooks.useTextEditorDefaultTranslations)();
  const {
    slate,
    slateReact
  } = (0, _react.useContext)(_textEditorContext.default);
  const textEditorService = new _textEditorService.default(slate, slateReact);
  const editor = slateReact.useSlate();
  const [url, setUrl] = (0, _react.useState)('');
  const isLinkActive = textEditorService.isBlockActive(editor, _textEditorConstants.BLOCK_TYPES.LINK);
  const urlProps = (0, _useInputValidation.useInputValidation)([url, setUrl], {
    validations: [(0, _useInputValidation.required)(urlIsRequiredMessage ?? defaultTranslations.urlIsRequiredMessage), (0, _useInputValidation.isUrl)(enterValidUrlMessage ?? defaultTranslations.enterValidUrlMessage)]
  }, {
    validationErrorWarningMessage: validationErrorWarningMessage ?? defaultTranslations.validationErrorWarningMessage
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdown.Dropdown, {
    placement: _dropdown.DROPDOWN_PLACEMENT_TYPES.BOTTOM_START,
    renderAnchor: ({
      ref,
      onClick
    }) => /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      ref: ref,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_editorButton.default, {
        testId: "link-button",
        className: (0, _classnames.default)({
          active: isLinkActive
        }),
        tooltipText: tooltip ?? defaultTranslations.tooltipTextLink,
        icon: icon,
        onClick: e => {
          setUrl('');
          onClick(e);
        }
      })
    }),
    children: ({
      closeDropdown
    }) => /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "custom-link-dropdown",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "link-input",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_input.Input, {
          id: "link-input",
          fieldTestId: "link-input",
          placeholder: 'https://www.datarobot.com/',
          isHorizontalLayout: true,
          "aria-label": ariaLabel,
          ...urlProps
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.Button, {
        type: _button.BUTTON_TYPES.SUBMIT,
        className: "save-link-button",
        testId: "save-button",
        onClick: () => {
          // If Url is valid
          if (urlProps.validity === null) {
            textEditorService.focus(editor);
            textEditorService.insertLink(editor, url);
            closeDropdown();
          }
        },
        children: saveButtonText ?? defaultTranslations.saveButtonText
      })]
    })
  });
}
var _default = exports.default = LinkButton;