"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _nullthrows = _interopRequireDefault(require("nullthrows"));
var _prosemirrorState = require("prosemirror-state");
var _prosemirrorTransform = require("prosemirror-transform");
var _prosemirrorView = require("prosemirror-view");
var _prosemirrorModel = require("prosemirror-model");
var _insertTable = _interopRequireDefault(require("./insertTable"));
var _licitUiCommands = require("@modusoperandi/licit-ui-commands");
var _TableGridSizeEditor = _interopRequireWildcard(require("./ui/TableGridSizeEditor"));
var _licitDocAttrsStep = require("@modusoperandi/licit-doc-attrs-step");
var _NodeNames = require("./NodeNames");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
class TableInsertCommand extends _licitDocAttrsStep.UICommand {
  constructor() {
    super(...arguments);
    _defineProperty(this, "_popUp", null);
    _defineProperty(this, "shouldRespondToUIEvent", e => {
      return e.type === _licitDocAttrsStep.UICommand.EventType.MOUSEENTER;
    });
    _defineProperty(this, "isEnabled", state => {
      const tr = state;
      let bOK = false;
      const {
        selection
      } = tr;
      if (selection instanceof _prosemirrorState.TextSelection) {
        bOK = selection.from === selection.to;
        // [FS] IRAD-1065 2020-09-18
        // Disable create table menu if the selection is inside a table.
        if (bOK) {
          const $head = selection.$head;
          for (let d = $head.depth; d > 0; d--) {
            if ($head.node(d).type.spec.tableRole == 'row') {
              return false;
            }
          }
        }
        return bOK;
      }
      return bOK;
    });
    _defineProperty(this, "waitForUserInput", (state, dispatch, view, event) => {
      if (this._popUp) {
        return Promise.resolve(undefined);
      }
      const target = (0, _nullthrows.default)(event).currentTarget;
      if (!(target instanceof HTMLElement)) {
        return Promise.resolve(undefined);
      }
      const anchor = event ? event.currentTarget : null;
      return new Promise(resolve => {
        this._popUp = (0, _licitUiCommands.createPopUp)(_TableGridSizeEditor.default, null, {
          anchor,
          position: _licitUiCommands.atAnchorRight,
          onClose: val => {
            if (this._popUp) {
              this._popUp = null;
              resolve(val);
            }
          }
        });
      });
    });
    _defineProperty(this, "executeWithUserInput", (state, dispatch, view, inputs) => {
      if (dispatch) {
        const {
          selection,
          schema
        } = state;
        let {
          tr
        } = state;
        if (inputs) {
          const {
            rows,
            cols
          } = inputs;
          tr = tr.setSelection(selection);
          tr = (0, _insertTable.default)(tr, schema, rows, cols);
          tr = insertParagraph(state, tr);
        }
        dispatch(tr);
      }
      return false;
    });
  }
}

// [FS] 2021-04-01
// Add empty line after table drop
// To make easier to enter a line after table
function insertParagraph(state, tr) {
  const paragraph = state.schema.nodes[_NodeNames.PARAGRAPH];
  const textNode = state.schema.text(' ');
  const {
    from,
    to
  } = tr.selection;
  if (from !== to) {
    return tr;
  }
  const paragraphNode = paragraph.create({}, textNode, null);
  tr = tr.insert(from + tr.selection.$head.node(1).nodeSize - 4, _prosemirrorModel.Fragment.from(paragraphNode));
  return tr;
}
var _default = TableInsertCommand;
exports.default = _default;