"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _react = _interopRequireWildcard(require("react"));

var _box = _interopRequireDefault(require("../box"));

var _iconButton = _interopRequireDefault(require("../iconButton"));

var _popover = _interopRequireDefault(require("../popover"));

var _label = _interopRequireDefault(require("../label"));

var _input = _interopRequireDefault(require("../input"));

var _button = _interopRequireDefault(require("../button"));

var _tooltip = _interopRequireDefault(require("../tooltip"));

var _typography = require("../typography");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var TooltippedIconButton = (0, _tooltip.default)(_iconButton.default);

var LinkOptions = function LinkOptions(_ref) {
  var _ref$config = _ref.config,
      options = _ref$config.options,
      linkIcon = _ref$config.link.icon,
      defaultTargetOption = _ref$config.defaultTargetOption,
      _ref$currentState = _ref.currentState,
      link = _ref$currentState.link,
      selectionText = _ref$currentState.selectionText,
      onChange = _ref.onChange,
      translations = _ref.translations;

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      isPopoverShown = _useState2[0],
      setIsPopoverShown = _useState2[1];

  var iconButtonRef = (0, _react.useRef)();

  var _useState3 = (0, _react.useState)((link === null || link === void 0 ? void 0 : link.title) || selectionText),
      _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
      textValue = _useState4[0],
      setTextValue = _useState4[1];

  var _useState5 = (0, _react.useState)(link === null || link === void 0 ? void 0 : link.target),
      _useState6 = (0, _slicedToArray2.default)(_useState5, 2),
      linkValue = _useState6[0],
      setLinkValue = _useState6[1];

  (0, _react.useEffect)(function () {
    setTextValue((link === null || link === void 0 ? void 0 : link.title) || selectionText);
    setLinkValue(link === null || link === void 0 ? void 0 : link.target);
  }, [link === null || link === void 0 ? void 0 : link.target, selectionText]);

  var focusEditor = function focusEditor() {
    document.querySelector("[aria-label='rdw-editor']").focus();
  };

  var handleOpenPopoverClick = function handleOpenPopoverClick() {
    setIsPopoverShown(true);
  };

  var handleClosePopoverClick = function handleClosePopoverClick() {
    setIsPopoverShown(false);
    focusEditor();
  };

  var onTextChange = function onTextChange(_ref2) {
    var newText = _ref2.currentTarget.value;
    setTextValue(newText);
  };

  var onLinkChange = function onLinkChange(_ref3) {
    var newLink = _ref3.currentTarget.value;
    setLinkValue(newLink);
  };

  var handleAddLinkClick = /*#__PURE__*/function () {
    var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return onChange('link', textValue, linkValue, defaultTargetOption);

            case 2:
              _context.next = 4;
              return setIsPopoverShown(false);

            case 4:
              _context.next = 6;
              return focusEditor();

            case 6:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function handleAddLinkClick() {
      return _ref4.apply(this, arguments);
    };
  }();

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_box.default, {
    ref: iconButtonRef
  }, /*#__PURE__*/_react.default.createElement(TooltippedIconButton, {
    "data-wysiwyg": true,
    tooltip: /*#__PURE__*/_react.default.createElement(_typography.TextSmall, null, translations['components.controls.link.link']),
    tooltipSize: "small",
    tooltipShowDelay: 1000,
    icon: linkIcon,
    color: "black",
    key: "link",
    onClick: handleOpenPopoverClick,
    selected: !!link || isPopoverShown,
    marginRight: 2
  })), /*#__PURE__*/_react.default.createElement(_popover.default, {
    anchorEl: iconButtonRef === null || iconButtonRef === void 0 ? void 0 : iconButtonRef.current,
    active: isPopoverShown,
    backdrop: "transparent",
    onEscKeyDown: handleClosePopoverClick,
    onOverlayClick: handleClosePopoverClick,
    minWidth: 276,
    maxWidth: "100%",
    zIndex: 404
  }, /*#__PURE__*/_react.default.createElement(_box.default, {
    display: "flex",
    flexDirection: "column",
    padding: 4
  }, /*#__PURE__*/_react.default.createElement(_label.default, {
    htmlFor: "linkText",
    helpText: ""
  }, translations['components.controls.link.popover.text'], /*#__PURE__*/_react.default.createElement(_input.default, {
    id: "linkText",
    autoFocus: true,
    value: textValue || '',
    onChange: onTextChange,
    "data-wysiwyg": true
  })), /*#__PURE__*/_react.default.createElement(_label.default, {
    htmlFor: "link",
    helpText: "",
    marginBottom: 4
  }, translations['components.controls.link.popover.link'], /*#__PURE__*/_react.default.createElement(_input.default, {
    id: "link",
    value: linkValue || '',
    onChange: onLinkChange,
    placeholder: translations['components.controls.link.popover.placeholder'],
    "data-wysiwyg": true
  })), /*#__PURE__*/_react.default.createElement(_box.default, {
    display: "flex",
    justifyContent: "flex-end",
    alignItems: "center",
    marginTop: 4
  }, /*#__PURE__*/_react.default.createElement(_button.default, {
    level: "secondary",
    label: translations['components.controls.link.popover.cancel'],
    size: "small",
    marginRight: 3,
    onClick: handleClosePopoverClick,
    "data-wysiwyg": true
  }), /*#__PURE__*/_react.default.createElement(_button.default, {
    level: "primary",
    label: translations['components.controls.link.popover.addLink'],
    size: "small",
    disabled: !linkValue || !textValue,
    onClick: handleAddLinkClick,
    "data-wysiwyg": true
  })))));
};

var _default = LinkOptions;
exports.default = _default;