"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.updateFormula = exports.isMenuDisabled = exports.isFormulaType = exports.isFormulaActive = exports.insertFormula = void 0;
var _slate = require("slate");
var _slugid = _interopRequireDefault(require("slugid"));
var _queries = require("../../core/queries");
var _focusEditor = require("../../core/transforms/focus-editor");
var _constants = require("../../constants");
var _elementTypes = require("../../constants/element-types");
var _core = require("../../core");
const isMenuDisabled = function (editor) {
  let readonly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  if (readonly) return true;
  const {
    selection
  } = editor;
  if (!selection) return false;
  const selectedElems = (0, _queries.getSelectedElems)(editor);
  // Check if the selected element is illegal
  const isSelectedIllegalElement = selectedElems.some(elem => {
    const {
      type
    } = elem;
    if (editor.isVoid(elem)) return true;
    const unSupportTypes = [_constants.ELementTypes.CODE_BLOCK, _constants.ELementTypes.CODE_LINE, _constants.ELementTypes.TABLE, _constants.ELementTypes.TABLE_CELL, _constants.ELementTypes.TABLE_ROW, _constants.ELementTypes.HEADER1, _constants.ELementTypes.HEADER2, _constants.ELementTypes.HEADER3, _constants.ELementTypes.HEADER4, _constants.ELementTypes.HEADER5, _constants.ELementTypes.HEADER6, _constants.ELementTypes.LIST_ITEM, _constants.ELementTypes.UNORDERED_LIST, _constants.ELementTypes.ORDERED_LIST];
    if (unSupportTypes.includes(type)) return true;
    return false;
  });
  if (isSelectedIllegalElement) return true;
  return false;
};
exports.isMenuDisabled = isMenuDisabled;
const isFormulaActive = editor => {
  return isFormulaType(editor);
};
exports.isFormulaActive = isFormulaActive;
const isFormulaType = editor => {
  const [match] = _slate.Editor.nodes(editor, {
    match: n => (0, _queries.getNodeType)(n) === _constants.ELementTypes.FORMULA,
    universal: true
  });
  return !!match;
};
exports.isFormulaType = isFormulaType;
const insertFormula = (editor, data) => {
  const formula = {
    id: _slugid.default.nice(),
    type: _elementTypes.FORMULA,
    data: {
      formula: data.formula
    },
    children: [(0, _core.generateDefaultText)()]
  };
  _slate.Transforms.insertNodes(editor, formula, {
    at: data.at,
    void: true
  });
  (0, _focusEditor.focusEditor)(editor);
};
exports.insertFormula = insertFormula;
const updateFormula = (editor, data) => {
  const {
    formula,
    at
  } = data;
  _slate.Transforms.setNodes(editor, {
    data: {
      formula
    }
  }, {
    at: at,
    void: true
  });
  (0, _focusEditor.focusEditor)(editor);
};
exports.updateFormula = updateFormula;