"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index_esm = require("../../../min-dash/dist/index.esm.js");
var DEFAULT_PRIORITY = 1e3;
function CommandInterceptor(eventBus) {
  this._eventBus = eventBus;
}
CommandInterceptor.$inject = ["eventBus"];
function unwrapEvent(fn, that) {
  return function(event) {
    return fn.call(that || null, event.context, event.command, event);
  };
}
CommandInterceptor.prototype.on = function(events, hook, priority, handlerFn, unwrap, that) {
  if (index_esm.isFunction(hook) || index_esm.isNumber(hook)) {
    that = unwrap;
    unwrap = handlerFn;
    handlerFn = priority;
    priority = hook;
    hook = null;
  }
  if (index_esm.isFunction(priority)) {
    that = unwrap;
    unwrap = handlerFn;
    handlerFn = priority;
    priority = DEFAULT_PRIORITY;
  }
  if (index_esm.isObject(unwrap)) {
    that = unwrap;
    unwrap = false;
  }
  if (!index_esm.isFunction(handlerFn)) {
    throw new Error("handlerFn must be a function");
  }
  if (!index_esm.isArray(events)) {
    events = [events];
  }
  var eventBus = this._eventBus;
  index_esm.forEach(events, function(event) {
    var fullEvent = ["commandStack", event, hook].filter(function(e) {
      return e;
    }).join(".");
    eventBus.on(fullEvent, priority, unwrap ? unwrapEvent(handlerFn, that) : handlerFn, that);
  });
};
var hooks = [
  "canExecute",
  "preExecute",
  "preExecuted",
  "execute",
  "executed",
  "postExecute",
  "postExecuted",
  "revert",
  "reverted"
];
index_esm.forEach(hooks, function(hook) {
  CommandInterceptor.prototype[hook] = function(events, priority, handlerFn, unwrap, that) {
    if (index_esm.isFunction(events) || index_esm.isNumber(events)) {
      that = unwrap;
      unwrap = handlerFn;
      handlerFn = priority;
      priority = events;
      events = null;
    }
    this.on(events, hook, priority, handlerFn, unwrap, that);
  };
});
exports.default = CommandInterceptor;
