"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.handleTab = void 0;
var _slate = require("slate");
var _isHotkey = _interopRequireDefault(require("is-hotkey"));
var _core = require("../../../core");
var _elementTypes = require("../../../constants/element-types");
var _transforms = require("../transforms");
const handleTab = (editor, event) => {
  const {
    selection,
    hasMovedSelection
  } = editor;
  if (!selection) return;
  if (hasMovedSelection) event.stopPropagation();
  const selectedList = (0, _core.findNode)(editor, {
    type: [_elementTypes.LIST_ITEM]
  });
  if (!selectedList) return;
  let workRange = editor.selection;
  if (!_slate.Range.isCollapsed(selection)) {
    let {
      anchor,
      focus
    } = selection;
    if (_slate.Range.isBackward(selection)) {
      ({
        anchor,
        focus
      } = {
        anchor: {
          ...selection.focus
        },
        focus: {
          ...selection.anchor
        }
      });
    }
    const unHungRange = _slate.Editor.unhangRange(editor, {
      anchor,
      focus
    });
    if (unHungRange) {
      workRange = unHungRange;
      _slate.Transforms.select(editor, unHungRange);
    }
  }
  const isIncrease = (0, _isHotkey.default)('shift+tab', event) ? false : true;
  if (workRange && selectedList) {
    event.preventDefault();
    (0, _transforms.moveListItems)(editor, {
      at: workRange,
      increase: isIncrease,
      enableResetOnShiftTab: true
    });
    return true;
  }
};
exports.handleTab = handleTab;