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 const getListItemAttributes = $pos => {
  // Get level for the correct indent of nesting
  const indentLevel = numberNestedLists($pos) - 1;
  const itemAtPos = findParentNodeClosestToPos($pos, isListItemNode);

  // Get the index of the current item relative to parent (parent is at item depth - 1)
  const itemIndex = $pos.index(itemAtPos ? itemAtPos.depth - 1 : undefined);
  return {
    indentLevel,
    itemIndex
  };
};
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export const normalizeListItemsSelection = ({
  selection,
  doc
}) => {
  if (selection.empty) {
    return selection;
  }
  const {
    $from,
    $to
  } = selection;
  if (selection instanceof NodeSelection) {
    const head = resolvePositionToStartOfListItem($from);
    return new TextSelection(head, head);
  }
  const head = resolvePositionToStartOfListItem($from);
  const anchor = resolvePositionToEndOfListItem($to);
  return new TextSelection(anchor, head);
};
const resolvePositionToStartOfListItem = $pos => {
  const fromRange = $pos.blockRange($pos, isListItemNode);
  const fromPosition = fromRange && $pos.textOffset === 0 && fromRange.end - 1 === $pos.pos ? Selection.near($pos.doc.resolve(fromRange.end + 1), 1).$from : $pos;
  return fromPosition;
};
const resolvePositionToEndOfListItem = $pos => {
  const toRange = $pos.blockRange($pos, isListItemNode);
  const 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';