"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 operators_1 = require("rxjs/operators");
const util_1 = require("../util");
function parse(stashId, [_controllerId, nominators, rewardDestination, stakers, validatorPrefs, _nextKeys]) {
    return {
        controllerId: _controllerId.unwrapOr(undefined),
        nextKeys: _nextKeys.unwrapOr(undefined),
        nominators: nominators.targets,
        nominateAt: nominators.submittedIn,
        rewardDestination,
        stakers,
        stashId,
        validatorPrefs
    };
}
function query(api, stashId) {
    return api.queryMulti([
        [api.query.staking.bonded, stashId],
        [api.query.staking.nominators, stashId],
        [api.query.staking.payee, stashId],
        [api.query.staking.stakers, stashId],
        [api.query.staking.validators, stashId],
        [api.query.session.nextKeys, [api.consts.session.dedupKeyPrefix, stashId]]
    ]);
}
function infoStash(api) {
    return util_1.memo((stashId) => query(api, stashId).pipe(operators_1.map((result) => parse(stashId, result))));
}
exports.infoStash = infoStash;
