import { NodeSelection, Selection, TextSelection } from '@atlaskit/editor-prosemirror/state';
import { findParentNodeClosestToPos } from '@atlaskit/editor-prosemirror/utils';
import { isListItemNode } from '../utils';
import { numberNestedLists } from './numberNestedLists';
export var getListItemAttributes = function getListItemAttributes($pos) {
  // Get level for the correct indent of nesting
  var indentLevel = numberNestedLists($pos) - 1;
  var itemAtPos = findParentNodeClosestToPos($pos, isListItemNode);

  // Get the index of the current item relative to parent (parent is at item depth - 1)
  var itemIndex = $pos.index(itemAtPos ? itemAtPos.depth - 1 : undefined);
  return {
    indentLevel: indentLevel,
    itemIndex: itemIndex
  };
};
export var normalizeListItemsSelection = function normalizeListItemsSelection(_ref) {
  var selection = _ref.selection,
    doc = _ref.doc;
  if (selection.empty) {
    return selection;
  }
  var $from = selection.$from,
    $to = selection.$to;
  if (selection instanceof NodeSelection) {
    var _head = resolvePositionToStartOfListItem($from);
    return new TextSelection(_head, _head);
  }
  var head = resolvePositionToStartOfListItem($from);
  var anchor = resolvePositionToEndOfListItem($to);
  return new TextSelection(anchor, head);
};
var resolvePositionToStartOfListItem = function resolvePositionToStartOfListItem($pos) {
  var fromRange = $pos.blockRange($pos, isListItemNode);
  var fromPosition = fromRange && $pos.textOffset === 0 && fromRange.end - 1 === $pos.pos ? Selection.near($pos.doc.resolve(fromRange.end + 1), 1).$from : $pos;
  return fromPosition;
};
var resolvePositionToEndOfListItem = function resolvePositionToEndOfListItem($pos) {
  var toRange = $pos.blockRange($pos, isListItemNode);
  var toPosition = toRange && $pos.textOffset === 0 && toRange.start + 1 === $pos.pos ? Selection.near($pos.doc.resolve(toRange.start - 1), -1).$to : $pos;
  return toPosition;
};
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { numberNestedLists } from './numberNestedLists';