import { Plugin } from 'prosemirror-state';
import { columnResizing } from 'prosemirror-tables';
import { EditorView } from 'prosemirror-view';
var TABLE_HANDLE_WIDTH = 10;
var TABLE_CELL_MINWIDTH = 25;
var TABLE_VIEW = undefined;
var TABLE_LAST_COLUMN_RESIZABLE = false;
function lookUpTableWrapper(event) {
  var target = event.target;
  if (!(target !== null && target !== void 0 && target.closest)) {
    return null;
  }
  return target.closest('.tableWrapper');
}
function dispatchMouseEvent(type, clientX) {
  requestAnimationFrame(function () {
    var event = new MouseEvent(type, {
      view: window,
      bubbles: true,
      cancelable: true,
      clientX: clientX
    });
    window.dispatchEvent(event);
  });
}
function calculateMaxClientX(event, targetTable) {
  var clientX = event.clientX;
  var _targetTable$getBound = targetTable.getBoundingClientRect(),
    left = _targetTable$getBound.left,
    width = _targetTable$getBound.width;
  var offsetX = clientX - left;
  var colgroup = targetTable.querySelector('colgroup');
  var colsCount = colgroup ? colgroup.querySelectorAll('col').length : 0;
  var cx = width - offsetX - colsCount * TABLE_CELL_MINWIDTH;
  return Math.round(clientX + Math.max(0, cx));
}
export default function createTableResizingPlugin() {
  var maxClientX = 0;

  // https://github.com/ProseMirror/prosemirror-tables/blob/master/src/columnresizing.js
  var plugin = columnResizing(TABLE_HANDLE_WIDTH, TABLE_CELL_MINWIDTH, TABLE_VIEW, TABLE_LAST_COLUMN_RESIZABLE);
  var _captureMouse = function captureMouse(event) {
    if (event.clientX > maxClientX) {
      // Current mouse event will make table too wide. Stop it and
      // fires a simulated event instead.
      event.stopImmediatePropagation();
      dispatchMouseEvent(event.type, maxClientX);
    } else if (event.type === 'mouseup') {
      window.removeEventListener('mousemove', _captureMouse, true);
      window.removeEventListener('mouseup', _captureMouse, true);
    }
  };
  var _mousedown = plugin.props.handleDOMEvents.mousedown;

  // This is a workaround to constraint the mousemove to prevent
  // the table become too wide.
  Object.assign(plugin.props.handleDOMEvents, {
    mousedown: function mousedown(view, event) {
      var targetTable = lookUpTableWrapper(event);
      maxClientX = targetTable ? calculateMaxClientX(event, targetTable) : Number.MAX_VALUE;
      window.addEventListener('mousemove', _captureMouse, true);
      window.addEventListener('mouseup', _captureMouse, true);
      _mousedown(view, event);
      return false;
    }
  });
  return plugin;
}