"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");
function proposals(api, section) {
    return () => api.query[section]
        ? api.query[section].proposals().pipe(operators_1.switchMap((hashes) => hashes.length
            ? rxjs_1.combineLatest([
                rxjs_1.of(hashes),
                api.query[section].proposalOf.multi(hashes),
                api.query[section].voting.multi(hashes)
            ])
            : rxjs_1.of([[], [], []])), operators_1.map(([hashes, proposals, votes]) => proposals
            .map((proposalOpt, index) => proposalOpt.isSome
            ? {
                hash: hashes[index],
                proposal: proposalOpt.unwrap(),
                votes: votes[index].unwrapOr(null)
            }
            : null)
            .filter((proposal) => !!proposal)))
        : rxjs_1.of([]);
}
exports.proposals = proposals;
