"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _slateEditTable = _interopRequireDefault(require("slate-edit-table"));

var _utils = require("slate-edit-table/dist/utils");

var _handlers = require("slate-edit-table/dist/handlers");

var _options = _interopRequireDefault(require("slate-edit-table/dist/options"));

var _changes = require("slate-edit-table/dist/changes");

function onBackspace(event, change, editor, opts) {
  var _startCell$nodes;

  var value = change.value;
  var startBlock = value.startBlock,
      endBlock = value.endBlock,
      selection = value.selection,
      document = value.document;
  var startCell = document.getClosest(startBlock.key, opts.isCell);
  var endCell = document.getClosest(endBlock.key, opts.isCell);
  var startBlockIndex = startCell === null || startCell === void 0 ? void 0 : (_startCell$nodes = startCell.nodes) === null || _startCell$nodes === void 0 ? void 0 : _startCell$nodes.findIndex(function (block) {
    return block.key == startBlock.key;
  }); // If a cursor is collapsed at the start of the first block, do nothing

  if (startBlockIndex === 0 && selection.isAtStartOf(startBlock)) {
    if (startBlock.isVoid) {
      // Delete the block normally if it is a void block
      return undefined;
    }

    event.preventDefault();
    return change;
  } // If "normal" deletion, we continue


  if (startCell === endCell) {
    return undefined;
  } // If cursor is between multiple blocks,
  // we clear the content of the cells.


  event.preventDefault();
  var blocks = value.blocks; // Get all cells that contains the selection

  var cells = blocks.map(function (node) {
    return node.type === opts.typeCell ? node : document.getClosest(node.key, function (a) {
      return a.type === opts.typeCell;
    });
  }).toSet(); // If the cursor is at the very end of the first cell, ignore it.
  // If the cursor is at the very start of the last cell, ignore it.
  // This behavior is to compensate hanging selection behaviors:
  // https://github.com/ianstormtaylor/slate/pull/1605

  var ignoreFirstCell = value.selection.collapseToStart().isAtEndOf(cells.first());
  var ignoreLastCell = value.selection.collapseToEnd().isAtStartOf(cells.last());
  var cellsToClear = cells;

  if (ignoreFirstCell) {
    cellsToClear = cellsToClear.rest();
  }

  if (ignoreLastCell) {
    cellsToClear = cellsToClear.butLast();
  } // Clear all the selection


  cellsToClear.forEach(function (cell) {
    return (0, _changes.clearCell)(opts, change, cell);
  }); // Update the selection properly, and avoid reset of selection

  var updatedStartCell = change.value.document.getDescendant(cellsToClear.first().key);
  return change.collapseToStartOf(updatedStartCell);
}

var KEY_ENTER = 'Enter';
var KEY_TAB = 'Tab';
var KEY_BACKSPACE = 'Backspace';
var KEY_DOWN = 'ArrowDown';
var KEY_UP = 'ArrowUp';
/**
 * User is pressing a key in the editor
 */

function onKeyDown(opts, event, change, editor) {
  // Only handle events in cells
  if (!(0, _utils.isSelectionInTable)(opts, change.value)) {
    return undefined;
  } // Build arguments list


  var args = [event, change, editor, opts];

  switch (event.key) {
    case KEY_ENTER:
      if (event.metaKey && opts.exitBlockType) {
        return _handlers.onModEnter.apply(void 0, args);
      }

      return _handlers.onEnter.apply(void 0, args);

    case KEY_TAB:
      return _handlers.onTab.apply(void 0, args);

    case KEY_BACKSPACE:
      return onBackspace.apply(void 0, args);

    case KEY_DOWN:
    case KEY_UP:
      return _handlers.onUpDown.apply(void 0, args);

    default:
      return undefined;
  }
}

var _default = function _default(opts) {
  var core = (0, _slateEditTable["default"])(opts);
  var tableOpts = new _options["default"](opts);
  core.onKeyDown = onKeyDown.bind(null, tableOpts);
  return core;
};

exports["default"] = _default;
//# sourceMappingURL=CustomTablePlugin.js.map