"use strict";
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const util_1 = require("../util");
/**
 * @description Return a boolean array indicating whether the passed accounts had received heartbeats in the current session
 */
function receivedHeartbeats(api) {
    return util_1.memo(() => {
        var _a;
        return ((_a = api.query.imOnline) === null || _a === void 0 ? void 0 : _a.receivedHeartbeats) ? api.derive.staking.overview().pipe(operators_1.switchMap(({ currentIndex, validators }) => rxjs_1.combineLatest([
            rxjs_1.of(validators),
            api.query.imOnline.receivedHeartbeats.multi(validators.map((_address, index) => [currentIndex, index])),
            api.query.imOnline.authoredBlocks.multi(validators.map((address) => [currentIndex, address]))
        ])), operators_1.map(([validators, heartbeats, numBlocks]) => validators.reduce((result, validator, index) => (Object.assign(Object.assign({}, result), { [validator.toString()]: {
                blockCount: numBlocks[index],
                hasMessage: !heartbeats[index].isEmpty,
                isOnline: !heartbeats[index].isEmpty || numBlocks[index].gtn(0)
            } })), {})))
            : rxjs_1.of({});
    });
}
exports.receivedHeartbeats = receivedHeartbeats;
