"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 _eventBus = _interopRequireDefault(require("../../../utils/event-bus"));
var _eventTypes = require("../../../constants/event-types");
var _constants = require("../../constants");
var _linkModal = _interopRequireDefault(require("../../plugins/link/menu/link-modal"));
var _formulaModal = _interopRequireDefault(require("../../plugins/formula/menu/formula-modal"));
const InsertElementDialog = _ref => {
  let {
    editor
  } = _ref;
  const [dialogType, setDialogType] = (0, _react.useState)('');
  const [isOpenLinkModal, setIsOpenLinkModal] = (0, _react.useState)(false);
  const [formula, setFormula] = (0, _react.useState)('');
  const [linkInfo, setLinkInfo] = (0, _react.useState)({
    linkTitle: '',
    linkUrl: ''
  });
  const toggleDialog = (0, _react.useCallback)(_ref2 => {
    let {
      type
    } = _ref2;
    setDialogType(type);
    setIsOpenLinkModal(true);
    if (type === _constants.ELementTypes.LINK) {
      if (editor.selection) {
        const selectedText = _slate.Editor.string(editor, editor.selection);
        setLinkInfo((0, _objectSpread2.default)((0, _objectSpread2.default)({}, linkInfo), {}, {
          linkTitle: selectedText
        }));
      }
    }
  }, [editor, linkInfo]);
  const handleOpenLinkModal = (0, _react.useCallback)(formulaElement => {
    if (formulaElement) {
      const {
        formula
      } = formulaElement.data || {};
      setFormula(formula);
      setIsOpenLinkModal(true);
    }
  }, []);
  (0, _react.useEffect)(() => {
    const eventBus = _eventBus.default.getInstance();
    const toggleDialogSubscribe = eventBus.subscribe(_eventTypes.INTERNAL_EVENTS.INSERT_ELEMENT, toggleDialog);
    const unsubscribe = eventBus.subscribe(_eventTypes.INTERNAL_EVENTS.ON_OPEN_FORMULA_DIALOG, handleOpenLinkModal);
    return () => {
      toggleDialogSubscribe();
      unsubscribe();
    };
  }, [toggleDialog, handleOpenLinkModal]);
  const onCloseModal = (0, _react.useCallback)(() => {
    setIsOpenLinkModal(false);
    if (dialogType === _constants.ELementTypes.LINK) {
      setLinkInfo({
        linkTitle: '',
        linkUrl: ''
      });
    }
    if (dialogType === _constants.ELementTypes.FORMULA) {
      setFormula('');
    }
  }, [dialogType]);
  if (_constants.ELementTypes.LINK === dialogType) {
    return isOpenLinkModal && /*#__PURE__*/_react.default.createElement(_linkModal.default, {
      onCloseModal: onCloseModal,
      editor: editor,
      linkTitle: linkInfo.linkTitle,
      linkUrl: linkInfo.linkUrl
    });
  } else if (_constants.ELementTypes.FORMULA === dialogType) {
    return isOpenLinkModal && /*#__PURE__*/_react.default.createElement(_formulaModal.default, {
      onCloseModal: onCloseModal,
      editor: editor,
      formula: formula
    });
  } else {
    return null;
  }
};
var _default = exports.default = InsertElementDialog;