"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _isHotkey = _interopRequireDefault(require("is-hotkey"));
var _helpers = require("../helpers");
var _onTabHandle = require("./on-tab-handle");
var _insertBreakList = require("./insert-break-list");
var _insertFragmentList = require("./insert-fragment-list");
var _normalizeList = require("./normalize-list");
var _constant = require("../constant");
var _shortcut = require("./shortcut");
var _queries = require("../queries");
var _core = require("../../../core");
var _transforms = require("../transforms");
var _elementTypes = require("../../../constants/element-types");
const withList = editor => {
  const {
    insertBreak,
    onHotKeyDown,
    deleteBackward,
    insertText
  } = editor;
  const newEditor = editor;
  newEditor.insertBreak = () => {
    if ((0, _insertBreakList.insertBreakList)(editor)) return;
    insertBreak();
    return;
  };
  newEditor.deleteBackward = unit => {
    const {
      selection
    } = newEditor;
    if (selection === null) {
      deleteBackward(unit);
      return;
    }
    const res = (0, _queries.getListItemEntry)(editor, {});
    if (res && (0, _core.isSelectionAtBlockStart)(editor, {
      match: node => node.type === _elementTypes.LIST_ITEM
    })) {
      const {
        list,
        listItem
      } = res;
      if ((0, _core.isFirstNode)(list[0], listItem[0]) && !(0, _queries.isListNested)(editor, list[1])) {
        (0, _transforms.unwrapList)(editor);
        return;
      }
    }
    // nothing todo
    deleteBackward(unit);
  };
  newEditor.insertText = text => {
    const isPreventInsert = (0, _shortcut.handleShortcut)(newEditor, text);
    if (isPreventInsert) return;
    return insertText(text);
  };
  newEditor.onHotKeyDown = event => {
    const activeListType = (0, _helpers.getActiveListType)(editor);
    const isListActive = _constant.LIST_TYPES.includes(activeListType);
    if (isListActive) {
      if ((0, _isHotkey.default)(['tab', 'shift+tab'], event)) {
        if ((0, _onTabHandle.handleTab)(newEditor, event)) return true;
      }
      if ((0, _isHotkey.default)('shift+enter', event)) {
        event.preventDefault();
        return true;
      }
    }
    return onHotKeyDown && onHotKeyDown(event);
  };
  newEditor.insertFragment = (0, _insertFragmentList.insertFragmentList)(newEditor);
  newEditor.normalizeNode = (0, _normalizeList.normalizeList)(newEditor);
  return newEditor;
};
var _default = exports.default = withList;