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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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 nullthrows from 'nullthrows';
import { Fragment, Schema } from 'prosemirror-model';
import { EditorState } from 'prosemirror-state';
import { TextSelection } from 'prosemirror-state';
import { Transform } from 'prosemirror-transform';
import { findParentNodeOfType } from 'prosemirror-utils';
import { EditorView } from 'prosemirror-view';
import * as React from 'react';
import { HEADING, LIST_ITEM, PARAGRAPH } from './NodeNames.js';
import nodeAt from './nodeAt.js';
import { UICommand } from '@modusoperandi/licit-doc-attrs-step';
function mergeListItemUp(tr, schema) {
  // This merge a list item to is previous list item of the selection is at the
  // beginning of the list item.
  var _tr = tr,
    selection = _tr.selection;
  if (!selection) {
    return tr;
  }
  var nodeType = schema.nodes[LIST_ITEM];
  if (!nodeType) {
    return tr;
  }
  var from = selection.from,
    empty = selection.empty;
  if (!empty) {
    // Selection is collapsed.
    return tr;
  }
  var result = findParentNodeOfType(nodeType)(selection);
  if (!result) {
    return tr;
  }
  var pos = result.pos,
    node = result.node;
  if (from !== pos + 2) {
    // Selection is not at the begining of the list item.
    return tr;
  }
  var $pos = tr.doc.resolve(pos);
  var prevNode = $pos.nodeBefore;
  if (!prevNode || prevNode.type !== nodeType) {
    return tr;
  }
  if (node.childCount !== 1) {
    // list item should only have one child (paragraph).
    return tr;
  }
  var paragraphNode = node.firstChild;
  var textNode = schema.text(' ');

  // Delete the list item
  tr = tr["delete"](pos - 2, pos + node.nodeSize);
  // Append extra space character to its previous list item.
  tr = tr.insert(pos - 2, Fragment.from(textNode));
  // Move the content to its previous list item.
  tr = tr.insert(pos - 1, Fragment.from(paragraphNode.content));
  tr = tr.setSelection(TextSelection.create(tr.doc, pos - 1, pos - 1));
  return tr;
}
function mergeListItemDown(tr, schema) {
  // This merge a list item to is next list item of the selection is at the
  // beginning of the list item.
  var _tr2 = tr,
    selection = _tr2.selection;
  if (!selection) {
    return tr;
  }
  var listItem = schema.nodes[LIST_ITEM];
  if (!listItem) {
    return tr;
  }
  var from = selection.from,
    empty = selection.empty;
  if (!empty) {
    // Selection is collapsed.
    return tr;
  }
  var result = findParentNodeOfType(listItem)(selection);
  if (!result) {
    return tr;
  }
  var pos = result.pos,
    node = result.node;
  if (from !== pos + node.content.size) {
    // Selection is not at the begining of the list item.
    return tr;
  }
  var $pos = tr.doc.resolve(pos);
  var list = $pos.parent.type;
  var listResult = findParentNodeOfType(list)(selection);
  if (!listResult) {
    return tr;
  }
  var nextFrom = pos + node.nodeSize;
  var nextNode = nodeAt(tr.doc, nextFrom);
  var deleteFrom = nextFrom;
  if (listResult.start + listResult.node.content.size === nextFrom) {
    // It's at the end of the last list item. It shall bring the content of the
    // block after the list.
    nextNode = nodeAt(tr.doc, nextFrom + 1);
    deleteFrom += 1;
  }
  if (!nextNode) {
    return tr;
  }
  var nextContent;
  switch (nextNode.type) {
    case listItem:
      // List item should only have one child (paragraph).
      var paragraphNode = nullthrows(nextNode.firstChild);
      nextContent = Fragment.from(paragraphNode.content);
      break;
    case schema.nodes[HEADING]:
    case schema.nodes[PARAGRAPH]:
      // Will bring in the content of the next block.
      nextContent = Fragment.from(nextNode.content);
      break;
  }
  if (!nextContent) {
    return tr;
  }
  var textNode = schema.text(' ');
  // Delete the next node.
  tr = tr["delete"](deleteFrom, deleteFrom + nextNode.nodeSize);
  // Append extra space character to its previous list item.
  tr = tr.insert(nextFrom - 2, nextContent);
  // Move the content to the list item.
  tr = tr.insert(nextFrom - 2, Fragment.from(textNode));
  tr = tr.setSelection(TextSelection.create(tr.doc, nextFrom - 2, nextFrom - 2));
  return tr;
}
var ListItemMergeCommand = /*#__PURE__*/function (_UICommand) {
  function ListItemMergeCommand(_direction) {
    var _this;
    _classCallCheck(this, ListItemMergeCommand);
    _this = _callSuper(this, ListItemMergeCommand);
    _defineProperty(_this, "_direction", '');
    _defineProperty(_this, "isActive", function (state) {
      return false;
    });
    _defineProperty(_this, "execute", function (state, dispatch, view) {
      var selection = state.selection,
        schema = state.schema;
      var tr = state.tr;
      var direction = _this._direction;
      if (direction === 'down') {
        tr = mergeListItemDown(tr.setSelection(selection), schema);
      } else if (direction === 'up') {
        tr = mergeListItemUp(tr.setSelection(selection), schema);
      }
      if (tr.docChanged) {
        dispatch && dispatch(tr);
        return true;
      } else {
        return false;
      }
    });
    _defineProperty(_this, "waitForUserInput", function (_state, _dispatch, _view, _event) {
      return Promise.resolve(undefined);
    });
    _defineProperty(_this, "executeWithUserInput", function (_state, _dispatch, _view, _inputs) {
      return false;
    });
    _this._direction = _direction;
    return _this;
  }
  _inherits(ListItemMergeCommand, _UICommand);
  return _createClass(ListItemMergeCommand, [{
    key: "cancel",
    value: function cancel() {
      return null;
    }
  }]);
}(UICommand);
export default ListItemMergeCommand;