function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

import { resolveMaybePromiseMethod } from '@teamix/utils';
export function eventHandler(action, actionContext, onTrigger) {
  var _action$trigger = action.trigger,
      trigger = _action$trigger === void 0 ? 'onClick' : _action$trigger,
      preventDefault = action.preventDefault,
      stopPropagation = action.stopPropagation,
      beforeAction = action.beforeAction;
  return _defineProperty({}, "".concat(trigger), function () {
    var _2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(e) {
      var _action$onTrigger;

      var beforeActionContext;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (preventDefault) {
                e.preventDefault();
              }

              if (stopPropagation) {
                e.stopPropagation();
              }

              if (!beforeAction) {
                _context.next = 14;
                break;
              }

              _context.prev = 3;
              _context.next = 6;
              return resolveMaybePromiseMethod(beforeAction, actionContext);

            case 6:
              beforeActionContext = _context.sent;
              _context.next = 12;
              break;

            case 9:
              _context.prev = 9;
              _context.t0 = _context["catch"](3);
              beforeActionContext = false;

            case 12:
              if (!(beforeActionContext === false)) {
                _context.next = 14;
                break;
              }

              return _context.abrupt("return");

            case 14:
              onTrigger(Object.assign({}, beforeActionContext, actionContext), e);
              (_action$onTrigger = action.onTrigger) === null || _action$onTrigger === void 0 ? void 0 : _action$onTrigger.call(action);

            case 16:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[3, 9]]);
    }));

    function _(_x) {
      return _2.apply(this, arguments);
    }

    return _;
  }());
}
export default eventHandler;