"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _prosemirrorState = require("prosemirror-state");
var _prosemirrorTransform = require("prosemirror-transform");
var _prosemirrorView = require("prosemirror-view");
var _MarkNames = require("./MarkNames");
var _SelectionPlaceholderPlugin = require("./SelectionPlaceholderPlugin");
var _licitUiCommands = require("@modusoperandi/licit-ui-commands");
var _LinkURLEditor = _interopRequireDefault(require("./ui/LinkURLEditor"));
var _licitDocAttrsStep = require("@modusoperandi/licit-doc-attrs-step");
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 LinkSetURLCommand extends _licitDocAttrsStep.UICommand {
  constructor() {
    super(...arguments);
    _defineProperty(this, "_popUp", null);
    _defineProperty(this, "isEnabled", state => {
      if (!(state.selection instanceof _prosemirrorState.TextSelection)) {
        // Could be a NodeSelection or CellSelection.
        return false;
      }
      const markType = state.schema.marks[_MarkNames.MARK_LINK];
      if (!markType) {
        return false;
      }
      const {
        from,
        to
      } = state.selection;
      return from < to;
    });
    _defineProperty(this, "waitForUserInput", (state, dispatch, view, event) => {
      if (this._popUp) {
        return Promise.resolve(undefined);
      }
      if (dispatch) {
        dispatch((0, _SelectionPlaceholderPlugin.showSelectionPlaceholder)(state));
      }
      const {
        doc,
        schema,
        selection
      } = state;
      const markType = schema.marks[_MarkNames.MARK_LINK];
      if (!markType) {
        return Promise.resolve(undefined);
      }
      const {
        from,
        to
      } = selection;
      const result = (0, _licitUiCommands.findNodesWithSameMark)(doc, from, to, markType);
      const href = result ? result.mark.attrs.href : null;
      return new Promise(resolve => {
        this._popUp = (0, _licitUiCommands.createPopUp)(_LinkURLEditor.default, {
          href
        }, {
          modal: true,
          onClose: val => {
            if (this._popUp) {
              this._popUp = null;
              resolve(val);
            }
          }
        });
      });
    });
    _defineProperty(this, "executeWithUserInput", (state, dispatch, view, href) => {
      if (dispatch) {
        const {
          selection,
          schema
        } = state;
        let {
          tr
        } = state;
        tr = view ? (0, _SelectionPlaceholderPlugin.hideSelectionPlaceholder)(view.state) : tr;
        tr = tr.setSelection(selection);
        if (href !== undefined) {
          const markType = schema.marks[_MarkNames.MARK_LINK];
          const attrs = href ? {
            href
          } : null;
          tr = (0, _licitUiCommands.applyMark)(tr.setSelection(state.selection), schema, markType, attrs);
        }
        dispatch(tr);
      }
      view && view.focus();
      return true;
    });
  }
}
var _default = LinkSetURLCommand;
exports.default = _default;