import _createClass from "@babel/runtime/helpers/createClass";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import { Transaction } from '@atlaskit/editor-prosemirror/state';
/**
 * Convert a EditorCommand to a standard Prosemirror Command.
 * The preferred approach to dispatching a `EditorCommand` is via the
 * `core.actions.execute` on `pluginInjectionAPI`. In some cases
 * the type may require a Command until we refactor this out and this
 * function is suitable for those cases.
 *
 * @param command A plugin command (a function that modifies and returns a `Transaction`)
 * @returns Command
 */
export function editorCommandToPMCommand(command) {
  return function (_ref, dispatch) {
    var tr = _ref.tr;
    var newTr = command === null || command === void 0 ? void 0 : command({
      tr: tr
    });
    if (!newTr) {
      return false;
    }
    if (newTr instanceof PassiveTransaction) {
      return true;
    }
    dispatch === null || dispatch === void 0 || dispatch(newTr);
    return true;
  };
}

/**
 * PassiveTransaction is used only to indicate that
 * an `EditorCommand` should return `true` but should not dispatch.
 */
export var PassiveTransaction = /*#__PURE__*/function (_Transaction) {
  _inherits(PassiveTransaction, _Transaction);
  var _super = _createSuper(PassiveTransaction);
  // This is very cheeky but this should never be used outside its intended
  // purpose - it will likely crash the editor so we should get an early warning
  // signal
  function PassiveTransaction() {
    _classCallCheck(this, PassiveTransaction);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return _super.call(this, {});
  }
  return _createClass(PassiveTransaction);
}(Transaction);