"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.handleShortcut = void 0;
var _slate = require("slate");
var _helpers = require("../helpers");
var _elementTypes = require("../../../constants/element-types");
var _core = require("../../../core");
/**
 * @param {Editor} editor
 * @param {String} text
 * @returns {Boolean} isPreventInsert
 */
const handleShortcut = (editor, text) => {
  if (text !== ' ') return false;
  const {
    selection
  } = editor;
  if (!_slate.Range.isCollapsed(selection)) return false;
  let [aboveNode, aboveNodePath] = _slate.Editor.above(editor);
  if (aboveNode.type !== _elementTypes.PARAGRAPH) return false;
  // Match ordered list shortcut
  const regOrderListShortcut = /^\s*[1]+\.\s*$/;
  const regUnorderedListShortcut = /^\s*\*+\s*$/;
  const {
    beforeText,
    range
  } = (0, _helpers.getBeforeText)(editor);
  const matchOrderListResult = beforeText.match(regOrderListShortcut);
  const matchUnorderedListResult = beforeText.match(regUnorderedListShortcut);
  const matchedOrderListText = matchOrderListResult && matchOrderListResult[0];
  const matchedUnorderedListText = matchUnorderedListResult && matchUnorderedListResult[0];
  if (matchUnorderedListResult) {
    // Unordered list shortcut
    if (matchedUnorderedListText !== '*') return false;
    if (matchUnorderedListResult.index !== 0) return false;
    // Delete shortcut key text
    _slate.Transforms.delete(editor, {
      at: range
    });
    (0, _helpers.setListType)(editor, _elementTypes.UNORDERED_LIST);
    (0, _core.focusEditor)(editor);
    return true;
  } else if (matchOrderListResult) {
    // Ordered list shortcut
    // If the match fails or the match is not at the beginning of the line, it is not a shortcut
    if (matchOrderListResult.index !== 0) return false;
    if (matchedOrderListText !== '1.') return false;
    const previousNodePath = (0, _core.getPreviousPath)(aboveNodePath);
    let previousNodeEntry = null;
    if (previousNodePath) {
      previousNodeEntry = _slate.Editor.node(editor, previousNodePath);
    }
    // If the previous node is not an ordered list and is start with `1.`,transforms to ordered list
    if (!previousNodePath || previousNodeEntry[0].type !== _elementTypes.ORDERED_LIST) {
      // Delete shortcut key text
      _slate.Transforms.delete(editor, {
        at: range
      });
      (0, _helpers.setListType)(editor, _elementTypes.ORDERED_LIST);
      (0, _core.focusEditor)(editor);
      return true;
    }
  }
  return false;
};
exports.handleShortcut = handleShortcut;