"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setListType = exports.isMenuDisabled = exports.getBeforeText = exports.getActiveListType = void 0;
var _slate = require("slate");
var _elementTypes = require("../../constants/element-types");
var _constant = require("./constant");
var _transforms = require("./transforms");
const isMenuDisabled = (editor, readonly) => {
  if (readonly || !editor.selection) return true;
  // Match disable node
  const [matchedDisabledNode] = _slate.Editor.nodes(editor, {
    match: node => {
      if (!_slate.Element.isElement(node)) return false;
      const isVoidOrBlock = _slate.Editor.isVoid(editor, node) && _slate.Editor.isBlock(editor, node);
      if (isVoidOrBlock) return true;
      const disabledNodeTypes = [_elementTypes.CODE_LINE, _elementTypes.CODE_BLOCK, _elementTypes.TABLE, _elementTypes.CHECK_LIST_ITEM];
      const isDisabledNode = disabledNodeTypes.includes(node.type);
      if (isDisabledNode) return true;
      return false;
    }
  });
  if (matchedDisabledNode) return true;
  return false;
};

/**
 * @returns {ORDERED_LIST | UNORDERED_LIST | undefined} Return the list type of the selected list
 */
exports.isMenuDisabled = isMenuDisabled;
const getActiveListType = editor => {
  const {
    selection
  } = editor;
  if (!selection) return;
  let selectedListNodeEntry;
  if (_slate.Range.isCollapsed(selection)) {
    const [nodeEntry] = _slate.Editor.nodes(editor, {
      match: node => _constant.LIST_TYPES.includes(node.type),
      mode: 'lowest'
    });
    selectedListNodeEntry = nodeEntry;
  } else {
    const {
      anchor,
      focus
    } = selection;
    const commonNodeEntry = _slate.Node.common(editor, anchor.path, focus.path);
    //  Select condition:
    // 1. Select in one list
    // 2. Select in one list item
    // 3. Select in one line
    if (_constant.LIST_TYPES.includes(commonNodeEntry[0].type)) {
      // Select in one list
      selectedListNodeEntry = commonNodeEntry;
    } else if (commonNodeEntry[0].type === _elementTypes.LIST_ITEM) {
      // Select in one list item
      selectedListNodeEntry = _slate.Editor.parent(editor, commonNodeEntry[1]);
    } else if (_slate.Text.isText(commonNodeEntry[0])) {
      // Select in one line
      const [nodeEntry] = _slate.Editor.nodes(editor, {
        at: commonNodeEntry[1],
        match: node => _constant.LIST_TYPES.includes(node.type),
        mode: 'lowest'
      });
      selectedListNodeEntry = nodeEntry;
    }
  }
  return selectedListNodeEntry && selectedListNodeEntry[0].type;
};
exports.getActiveListType = getActiveListType;
const setListType = (editor, type) => {
  (0, _transforms.transformsToList)(editor, type);
};
exports.setListType = setListType;
const getBeforeText = editor => {
  const {
    selection
  } = editor;
  if (selection == null) return {
    beforeText: '',
    range: null
  };
  const {
    anchor
  } = selection;
  // Find the near text node above the current text
  const [, aboveNodePath] = _slate.Editor.above(editor);
  const aboveNodeStartPoint = _slate.Editor.start(editor, aboveNodePath); // The starting position of the text node
  const range = {
    anchor,
    focus: aboveNodeStartPoint
  };
  const beforeText = _slate.Editor.string(editor, range) || '';
  return {
    beforeText,
    range
  };
};
exports.getBeforeText = getBeforeText;