"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.insertListItem = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/objectSpread2"));
var _slate = require("slate");
var _core = require("../../../core");
var _elementTypes = require("../../../constants/element-types");
const insertListItem = editor => {
  const licEntry = (0, _core.getAboveNode)(editor, {
    match: {
      type: _elementTypes.PARAGRAPH
    }
  });
  if (!licEntry) return false;
  const [, paragraphPath] = licEntry;
  const listItemEntry = _slate.Editor.parent(editor, paragraphPath);
  if (!listItemEntry) return false;
  const [listItemNode, listItemPath] = listItemEntry;
  if (listItemNode.type !== _elementTypes.LIST_ITEM) return false;
  let success = false;
  _slate.Editor.withoutNormalizing(editor, () => {
    var _editor$selection;
    if (!_slate.Range.isCollapsed(editor.selection)) {
      // Delete selected text
      _slate.Transforms.delete(editor, {
        at: editor.selection
      });
    }
    const _isStartPoint = (0, _core.isStartPoint)(editor, (_editor$selection = editor.selection) === null || _editor$selection === void 0 ? void 0 : _editor$selection.focus, paragraphPath);
    const isEndPoint = (0, _core.isBlockTextEmptyAfterSelection)(editor);
    const nextParagraphPath = _slate.Path.next(paragraphPath);
    const nextListItemPath = _slate.Path.next(listItemPath);
    if (_isStartPoint) {
      // List item has content, cursor at start
      const licItem = (0, _core.generateElement)(_elementTypes.PARAGRAPH);
      _slate.Transforms.insertNodes(editor, licItem, {
        at: listItemPath
      });
      const listItem = (0, _core.generateElement)(_elementTypes.LIST_ITEM, {
        childrenOrText: []
      });
      _slate.Transforms.wrapNodes(editor, listItem, {
        at: listItemPath
      });
      success = true;
      return;
    }
    if (!isEndPoint) {
      // List item has content, cursor at middle
      _slate.Transforms.splitNodes(editor);
      const listItem = (0, _core.generateElement)(_elementTypes.LIST_ITEM, {
        childrenOrText: []
      });
      _slate.Transforms.wrapNodes(editor, listItem, {
        at: nextParagraphPath
      });
      _slate.Transforms.moveNodes(editor, {
        at: nextParagraphPath,
        to: nextListItemPath
      });
      _slate.Transforms.select(editor, nextListItemPath);
      _slate.Transforms.collapse(editor, {
        edge: 'start'
      });
      success = true;
    } else {
      var _Editor$marks;
      // List item has content, cursor at end
      const marks = (_Editor$marks = _slate.Editor.marks(editor)) === null || _Editor$marks === void 0 ? void 0 : _Editor$marks.key;
      const licItem = (0, _core.generateElement)(_elementTypes.PARAGRAPH);
      _slate.Transforms.insertNodes(editor, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, licItem), marks), {
        at: nextListItemPath
      });
      const listItem = (0, _core.generateElement)(_elementTypes.LIST_ITEM, {
        childrenOrText: []
      });
      _slate.Transforms.wrapNodes(editor, listItem, {
        at: nextListItemPath
      });
      _slate.Transforms.select(editor, nextListItemPath);
      success = true;
    }
    if (listItemNode.children.length > 1) {
      _slate.Transforms.moveNodes(editor, {
        at: nextParagraphPath,
        to: nextListItemPath.concat(1)
      });
      success = true;
    }
  });
  return success;
};
exports.insertListItem = insertListItem;