function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import uuid from './ui/uuid.js';
import { Fragment, Schema } from 'prosemirror-model';
import { TextSelection } from 'prosemirror-state';
import { Transform, canSplit } from 'prosemirror-transform';
import { LIST_ITEM, ORDERED_LIST, BULLET_LIST, PARAGRAPH } from './NodeNames.js';
import { findParentNodeOfType } from 'prosemirror-utils';

// Splits a list item by the current cursor's position.
// Some examples:
//
// - split before item's text:
//   - before:
//     1. <cursor>AA
//     2. BB
//     3. CC
//   - after:
//     1. <cursor>
//     2. AA
//     3. BB
//     4. CC
//
// - split between item's text:
//   - before:
//     1. AA
//     2. B<cursor>B
//     3. CC
//   - after:
//     1. AA
//     2. B
//     3. B
//     4. CC
//
// - split after item's text:
//   - before:
//     1. AA
//     2. BB<cursor>
//     3. CC
//   - after:
//     1. AA
//     2. BB
//     3. <cursor>
//     4. CC
//
// - split at item with empty content:
//   - before:
//     1. AA
//     2. <cursor>
//     3. CC
//   - after:
//     1. AA
//     <cursor>
//     2. BB
//     3. CC
//
export default function splitListItem(tr, schema) {
  var nodeType = schema.nodes[LIST_ITEM];
  if (!nodeType) {
    return tr;
  }
  var _tr = tr,
    selection = _tr.selection;
  if (!selection) {
    return tr;
  }
  var $from = selection.$from,
    $to = selection.$to,
    node = selection.node;
  if (node && node.isBlock || $from.depth < 2 || !$from.sameParent($to)) {
    return tr;
  }
  var grandParent = $from.node(-1);
  if (grandParent.type !== nodeType) {
    return tr;
  }
  if ($from.parent.content.size == 0) {
    // In an empty list item.
    return splitEmptyListItem(tr, schema);
  }
  var nextType = $to.pos == $from.end() ? grandParent.contentMatchAt($from.indexAfter(-1)).defaultType : null;
  tr = tr["delete"]($from.pos, $to.pos);
  var types = nextType && [null, {
    type: nextType
  }];
  if (!canSplit(tr.doc, $from.pos, 2, types)) {
    return tr;
  }
  return tr.split($from.pos, 2, types);
}

// Splits an item with empty content:
//   - before:
//     1. AA
//     2. <cursor>
//     3. CC
//   - after:
//     1. AA
//     <cursor>
//     2. BB
//     3. CC
function splitEmptyListItem(tr, schema) {
  var listItemType = schema.nodes[LIST_ITEM];
  var orderedListType = schema.nodes[ORDERED_LIST];
  var bulletListType = schema.nodes[BULLET_LIST];
  var paragraphType = schema.nodes[PARAGRAPH];
  if (!listItemType || !paragraphType) {
    // Schema does not support the nodes expected.
    return tr;
  }
  var listItemFound = findParentNodeOfType(listItemType)(tr.selection);
  if (!listItemFound || listItemFound.node.textContent !== '') {
    // Cursor is not inside an empty list item.
    return tr;
  }
  var listFound = orderedListType && findParentNodeOfType(orderedListType)(tr.selection) || bulletListType && findParentNodeOfType(bulletListType)(tr.selection);
  if (!listFound) {
    // Cursor isn't inside an list.
    return tr;
  }
  var $listItemPos = tr.doc.resolve(listItemFound.pos);
  var listItemIndex = $listItemPos.index($listItemPos.depth);
  var listFoundNode = listFound.node;
  if (listFoundNode.childCount < 3 || listItemIndex < 1 || listItemIndex >= listFoundNode.childCount - 1) {
    // - The list must have at least three list items
    // - The cursor must be after the first list item and before the last list
    //   item.
    // If both conditions don't match, bails out, which will remove the empty
    // item.
    return tr;
  }

  // Find the name of the current list to split. If the name isn't available,
  // assigns a new name.
  var name = listFoundNode.attrs.name;
  if (!name) {
    name = uuid();
    tr = tr.setNodeMarkup(listFound.pos, listFoundNode.type, _objectSpread(_objectSpread({}, listFoundNode.attrs), {}, {
      name: name
    }), listFoundNode.marks);
  }

  // We'll split the list into two lists.
  // the first list contains the items before the cursor, and the second
  // list contains the items after the cursor and the second list will "follow"
  // the first list by sharing the same counter variable.
  var sliceFrom = listItemFound.pos + listItemFound.node.nodeSize;
  var sliceTo = listFound.pos + listFound.node.nodeSize - 1;
  var slicedItems = tr.doc.slice(sliceFrom, sliceTo, false);
  var deleteFrom = listItemFound.pos;
  var deleteTo = listFound.pos + listFound.node.nodeSize;
  tr = tr["delete"](deleteFrom, deleteTo);
  var sourceListNode = listFound.node;
  var listAttrs = _objectSpread({}, sourceListNode.attrs);
  if (orderedListType === sourceListNode.type) {
    listAttrs.counterReset = 'none';
    listAttrs.following = name;
  }
  var insertFrom = deleteFrom + 1;
  var listNode = sourceListNode.type.create(listAttrs, slicedItems.content);
  tr = tr.insert(insertFrom, listNode);
  var paragraph = paragraphType.create({}, Fragment.empty);
  tr = tr.insert(insertFrom, paragraph);
  tr = tr.setSelection(TextSelection.create(tr.doc, insertFrom));
  return tr;
}