"use strict";
// Copyright 2017-2020 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("@polkadot/util");
class Combinator {
    constructor(fns, callback) {
        this._allHasFired = false;
        this._fired = [];
        this._fns = [];
        this._isActive = true;
        this._results = [];
        this._subscriptions = [];
        this._callback = callback;
        // eslint-disable-next-line @typescript-eslint/require-await
        this._subscriptions = fns.map((input, index) => __awaiter(this, void 0, void 0, function* () {
            const [fn, ...args] = Array.isArray(input)
                ? input
                : [input];
            this._fired.push(false);
            this._fns.push(fn);
            // Not quite 100% how to have a variable number at the front here
            return fn(...args, this.createCallback(index));
        }));
    }
    allHasFired() {
        if (!this._allHasFired) {
            this._allHasFired = this._fired.filter((hasFired) => !hasFired).length === 0;
        }
        return this._allHasFired;
    }
    createCallback(index) {
        return (value) => {
            this._fired[index] = true;
            this._results[index] = value;
            this.triggerUpdate();
        };
    }
    triggerUpdate() {
        if (!this._isActive || !util_1.isFunction(this._callback) || !this.allHasFired()) {
            return;
        }
        try {
            this._callback(this._results);
        }
        catch (error) {
            // swallow, we don't want the handler to trip us up
        }
    }
    unsubscribe() {
        if (!this._isActive) {
            return;
        }
        this._isActive = false;
        // eslint-disable-next-line @typescript-eslint/no-misused-promises
        this._subscriptions.forEach((subscription) => __awaiter(this, void 0, void 0, function* () {
            try {
                const unsubscribe = yield subscription;
                if (util_1.isFunction(unsubscribe)) {
                    unsubscribe();
                }
            }
            catch (error) {
                // ignore
            }
        }));
    }
}
exports.default = Combinator;
