"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.wrapTaskListIntoListAbove = exports.moveTargetIntoList = void 0;
var _model = require("@atlaskit/editor-prosemirror/model");
var _transform = require("@atlaskit/editor-prosemirror/transform");
var moveTargetIntoList = exports.moveTargetIntoList = function moveTargetIntoList(_ref) {
  var _$target$nodeAfter;
  var insertPosition = _ref.insertPosition,
    $target = _ref.$target;
  // take the text content of the paragraph and insert after the paragraph up until before the the cut
  var from = insertPosition;
  var to = $target.pos + (((_$target$nodeAfter = $target.nodeAfter) === null || _$target$nodeAfter === void 0 ? void 0 : _$target$nodeAfter.nodeSize) || 0); //$cut.pos + $cut.nodeAfter.nodeSize;
  var gapFrom = $target.posAtIndex(0, $target.depth + 1); // start pos of the child
  var gapTo = $target.doc.resolve(gapFrom).end(); // end pos of the paragraph

  if (gapTo - gapFrom === 0) {
    return new _transform.ReplaceStep(from, to, $target.doc.slice(insertPosition, $target.pos));
  }
  var step = new _transform.ReplaceAroundStep(from, to, gapFrom, gapTo, $target.doc.slice(insertPosition, $target.pos), 0, true);
  return step;
};
var wrapTaskListIntoListAbove = exports.wrapTaskListIntoListAbove = function wrapTaskListIntoListAbove(tr, taskListStart, previousListStart) {
  var $taskListStart = tr.doc.resolve(taskListStart);

  /* Safecheck: if not passed a taskList node, return */
  if ($taskListStart.node().type.name !== 'taskList') {
    return;
  }
  var $previousListStart = tr.doc.resolve(previousListStart);
  var taskList = tr.doc.slice($taskListStart.pos, $taskListStart.after());
  var frag = _model.Fragment.from(taskList.content);

  /*
    Delete the existing taskList at the same level and insert inside above list provided
    1. To delete the tasklist, we need wrapping positions before and after it to completely delete it.
      $taskListStart.after() would give us closing position enclosed by node itself using it would remove only the contents of taskList
      so we add 1 to it to get the position after the taskList node and completly remove it.
    2. Inserting the taskList at the end() position safe inserts the taskList to next available position which would lead to deleting and inserting taskList at same place.
      Reason: $previousListStart.end() gives us end position of listItem but we can add content only inside listItem not at same level
      so we subtract 1 to get the position inside the list item and insert the TaskList same level as paragraph/content inside listItem.
  */
  tr.delete($taskListStart.before(), $taskListStart.after() + 1);
  tr.insert($previousListStart.end() - 1, frag);
};