"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setBlockQuoteType = exports.isMenuDisabled = exports.getBlockQuoteType = void 0;
var _slate = require("slate");
var _slugid = _interopRequireDefault(require("slugid"));
var _elementTypes = require("../../constants/element-types");
var _core = require("../../core");
const isMenuDisabled = (editor, readonly) => {
  if (readonly) return true;
  if (editor.selection == null) return true;
  const [nodeEntry] = _slate.Editor.nodes(editor, {
    match: n => {
      const type = (0, _core.getNodeType)(n);

      // Only available for p and blockquote
      if (type === _elementTypes.PARAGRAPH) return true;
      if (type === _elementTypes.BLOCKQUOTE) return true;
      if (type === _elementTypes.UNORDERED_LIST) return true;
      if (type === _elementTypes.ORDERED_LIST) return true;
      if (type === _elementTypes.CHECK_LIST_ITEM) return true;
      if (type && type.startWith && type.startWith('header')) return true;
      if (type === _elementTypes.IMAGE) return true;
      return false;
    },
    universal: true,
    mode: 'highest' // Match top level
  });

  // Match to p blockquote, do not disable
  if (nodeEntry) {
    return false;
  }
  return true;
};
exports.isMenuDisabled = isMenuDisabled;
const getBlockQuoteType = editor => {
  const [match] = _slate.Editor.nodes(editor, {
    match: n => (0, _core.getNodeType)(n) === _elementTypes.BLOCKQUOTE,
    universal: true
  });
  if (!match) return _elementTypes.PARAGRAPH;
  const [n] = match;
  return (0, _core.getNodeType)(n);
};
exports.getBlockQuoteType = getBlockQuoteType;
const setBlockQuoteType = (editor, active) => {
  if (!active) {
    const blockquoteNode = {
      id: _slugid.default.nice(),
      type: _elementTypes.BLOCKQUOTE
    };
    _slate.Transforms.wrapNodes(editor, blockquoteNode, {
      mode: 'highest',
      match: n => _slate.Element.isElement(n) && _slate.Editor.isBlock(editor, n)
    });
  } else {
    _slate.Transforms.unwrapNodes(editor, {
      mode: 'highest',
      match: n => _slate.Element.isElement(n) && _slate.Editor.isBlock(editor, n)
    });
  }
  (0, _core.focusEditor)(editor);
};
exports.setBlockQuoteType = setBlockQuoteType;