"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var chai_1 = require("chai");
var promise_utils_1 = require("../../src/promise-utils");
var EventsMatcher = /** @class */ (function () {
    function EventsMatcher(options) {
        this.options = options;
        this.events = [];
    }
    EventsMatcher.prototype.track = function (emitter) {
        var _this = this;
        var eventNames = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            eventNames[_i - 1] = arguments[_i];
        }
        eventNames.forEach(function (eventName) { return emitter.on(eventName, function (event) {
            chai_1.expect(event.type, "type of event dispatched as " + eventName).to.eql(eventName);
            _this.events.push(event);
        }); });
    };
    EventsMatcher.prototype.expect = function (events) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var _this = this;
            var _a, interval, timeout, retries;
            return tslib_1.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.options, interval = _a.interval, timeout = _a.timeout, retries = _a.retries;
                        if (!events.length) return [3 /*break*/, 2];
                        return [4 /*yield*/, promise_utils_1.retryPromise(function () { return _this.checkEvents(events); }, { retries: retries, interval: interval, timeout: timeout })];
                    case 1:
                        _b.sent();
                        return [3 /*break*/, 3];
                    case 2:
                        chai_1.expect(this.events).to.eql([]);
                        _b.label = 3;
                    case 3: return [4 /*yield*/, promise_utils_1.delayedPromise(this.options.noExtraEventsGrace)];
                    case 4:
                        _b.sent();
                        chai_1.expect(this.events, 'no further events after matching').to.eql([]);
                        return [2 /*return*/];
                }
            });
        });
    };
    EventsMatcher.prototype.checkEvents = function (events) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            return tslib_1.__generator(this, function (_a) {
                chai_1.expect(this.events).to.containSubset(events);
                this.events = [];
                return [2 /*return*/];
            });
        });
    };
    return EventsMatcher;
}());
exports.EventsMatcher = EventsMatcher;
//# sourceMappingURL=events-matcher.js.map