import { __extends, __read, __spread, __values } from "tslib";
import { Hook } from "./hook";
var AsyncHook = /** @class */ (function (_super) {
    __extends(AsyncHook, _super);
    function AsyncHook() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    AsyncHook.prototype.lazyEmit = function (cb) {
        var listeners = this.getListeners();
        if (listeners.length === 0) {
            return Promise.resolve();
        }
        return this.emit.apply(this, cb());
    };
    AsyncHook.prototype.emit = function () {
        var e_1, _a;
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        var promiseChain = Promise.resolve();
        var _loop_1 = function (listener) {
            promiseChain = promiseChain.then(function () { return listener.callback.apply(listener, __spread(args)); });
        };
        try {
            for (var _b = __values(this.getListeners()), _c = _b.next(); !_c.done; _c = _b.next()) {
                var listener = _c.value;
                _loop_1(listener);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return promiseChain;
    };
    return AsyncHook;
}(Hook));
export { AsyncHook };
