"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setHeaderType = exports.isMenuDisabled = exports.getHeaderType = void 0;
var _slate = require("slate");
var _queries = require("../../core/queries");
var _constants = require("../../constants");
var _core = require("../../core");
const isMenuDisabled = function (editor) {
  let readonly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  if (readonly) return true;
  if (!editor.selection) return true;
  const [match] = _slate.Editor.nodes(editor, {
    match(node, path) {
      let type = (0, _queries.getNodeType)(node);
      if (!type && (0, _queries.isTextNode)(node) && node.id) {
        const parentNode = (0, _queries.getParentNode)(node, node.id);
        type = (0, _queries.getNodeType)(parentNode);
      }
      if (type === _constants.ELementTypes.PARAGRAPH) return true;
      if (type && type.startsWith(_constants.ELementTypes.HEADER)) return true;
      return false;
    },
    universal: true,
    mode: 'highest'
  });
  return !match;
};
exports.isMenuDisabled = isMenuDisabled;
const getHeaderType = editor => {
  const [match] = _slate.Editor.nodes(editor, {
    match: node => {
      if (_slate.Element.isElement(node) && _slate.Editor.isBlock(editor, node)) {
        const type = (0, _queries.getNodeType)(node);
        if (type && type.startsWith(_constants.ELementTypes.HEADER)) return true;
      }
      return false;
    },
    universal: true
  });
  if (!match) return;
  const [matchedNode] = match;
  return (0, _queries.getNodeType)(matchedNode);
};
exports.getHeaderType = getHeaderType;
const setHeaderType = (editor, type) => {
  if (!type) return;
  _slate.Transforms.setNodes(editor, {
    type
  });
  (0, _core.focusEditor)(editor);
};
exports.setHeaderType = setHeaderType;