import { Schema } from 'prosemirror-model';
import { TextSelection } from 'prosemirror-state';
import { Transform } from 'prosemirror-transform';
import { isListNode } from '@modusoperandi/licit-ui-commands';
import joinDown from './joinDown';
import joinUp from './joinUp';
export default function joinListNode(tr, schema, listNodePos) {
  if (!tr.doc || !tr.selection) {
    return tr;
  }
  var node = tr.doc.nodeAt(listNodePos);
  if (!isListNode(node)) {
    return tr;
  }
  var initialSelection = tr.selection;
  var listFromPos = listNodePos;
  var listToPos = listFromPos + node.nodeSize;
  var $fromPos = tr.doc.resolve(listFromPos);
  var $toPos = tr.doc.resolve(listToPos);
  var selectionOffset = 0;
  if ($toPos.nodeAfter && $toPos.nodeAfter.type === node.type && $toPos.nodeAfter.attrs.level === node.attrs.level) {
    tr = joinDown(tr);
  }
  if ($fromPos.nodeBefore && $fromPos.nodeBefore.type === node.type && $fromPos.nodeBefore.attrs.level === node.attrs.level) {
    selectionOffset -= 2;
    tr = joinUp(tr);
  }
  var selection = TextSelection.create(tr.doc, initialSelection.from + selectionOffset, initialSelection.to + selectionOffset);
  tr = tr.setSelection(selection);
  return tr;
}