"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/objectSpread2"));
var _react = _interopRequireWildcard(require("react"));
var _slate = require("slate");
var _menuItem = _interopRequireDefault(require("../../../commons/menu/menu-item"));
var _menusConfig = require("../../../constants/menus-config");
var _elementTypes = require("../../../constants/element-types");
var _helper = require("../helper");
var _eventBus = _interopRequireDefault(require("../../../../utils/event-bus"));
var _linkModal = _interopRequireDefault(require("./link-modal"));
var _eventTypes = require("../../../../constants/event-types");
const menuConfig = _menusConfig.MENUS_CONFIG_MAP[_elementTypes.LINK];
const LinkMenu = _ref => {
  let {
    isRichEditor,
    className,
    readonly,
    editor
  } = _ref;
  const [isOpenLinkModal, setIsOpenLinkModal] = (0, _react.useState)(false);
  const [linkInfo, setLinkInfo] = (0, _react.useState)({
    linkTitle: '',
    linkUrl: ''
  });
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const isLinkActive = (0, _react.useMemo)(() => (0, _helper.isLinkType)(editor), [editor.selection]);
  (0, _react.useEffect)(() => {
    const eventBus = _eventBus.default.getInstance();
    const unsubscribe = eventBus.subscribe(_eventTypes.INTERNAL_EVENTS.ON_OPEN_LINK_MODAL, handleOpenLinkModal);
    return () => unsubscribe();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  (0, _react.useEffect)(() => {
    if ((0, _helper.isLinkType)(editor)) {
      const newTitle = editor.selection && _slate.Editor.string(editor, editor.selection);
      newTitle && setLinkInfo((0, _objectSpread2.default)((0, _objectSpread2.default)({}, linkInfo), {}, {
        linkTitle: newTitle
      }));
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [editor.selection]);
  const handleOpenLinkModal = (0, _react.useCallback)(linkInfo => {
    Reflect.ownKeys.length && setLinkInfo(linkInfo);
    setIsOpenLinkModal(true);
  }, [setIsOpenLinkModal, setLinkInfo]);
  const onMouseDown = (0, _react.useCallback)(event => {
    event.preventDefault();
    event.stopPropagation();
    if (isLinkActive) {
      isLinkActive && (0, _helper.unWrapLinkNode)(editor);
      return;
    }
    if (editor.selection) {
      const selectedText = _slate.Editor.string(editor, editor.selection);
      setLinkInfo((0, _objectSpread2.default)((0, _objectSpread2.default)({}, linkInfo), {}, {
        linkTitle: selectedText
      }));
    }
    setIsOpenLinkModal(true);
    document.getElementById("seafile_".concat(_elementTypes.LINK)).blur();
  }, [editor, isLinkActive, linkInfo]);
  const onCloseModal = (0, _react.useCallback)(() => {
    setIsOpenLinkModal(false);
    setLinkInfo({
      linkTitle: '',
      linkUrl: ''
    });
  }, []);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_menuItem.default, Object.assign({
    isRichEditor: isRichEditor,
    className: className,
    disabled: (0, _helper.isMenuDisabled)(editor, readonly),
    isActive: isLinkActive,
    onMouseDown: onMouseDown
  }, menuConfig)), isOpenLinkModal && /*#__PURE__*/_react.default.createElement(_linkModal.default, {
    onCloseModal: onCloseModal,
    editor: editor,
    linkTitle: linkInfo.linkTitle,
    linkUrl: linkInfo.linkUrl
  }));
};
var _default = exports.default = LinkMenu;