"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 AccountIndex_1 = require("../../types/primitive/Generic/AccountIndex");
const types_1 = require("../../types");
const util_1 = require("../util");
/**
 * @name indexToId
 * @param {( AccountIndex | string )} accountIndex - An accounts index in different formats.
 * @returns Returns the corresponding AccountId.
 * @example
 * <BR>
 *
 * ```javascript
 * api.derive.accounts.indexToId('F7Hs', (accountId) => {
 *   console.log(`The AccountId of F7Hs is ${accountId}`);
 * });
 * ```
 */
function indexToId(api) {
    return util_1.memo((_accountIndex) => {
        const accountIndex = _accountIndex instanceof types_1.ClassOf(api.registry, 'AccountIndex')
            ? _accountIndex
            : types_1.createType(api.registry, 'AccountIndex', _accountIndex);
        return api.query.indices.enumSet
            ? api.query.indices.enumSet(accountIndex.div(AccountIndex_1.ENUMSET_SIZE)).pipe(operators_1.startWith([]), operators_1.map((accounts) => (accounts || [])[accountIndex.mod(AccountIndex_1.ENUMSET_SIZE).toNumber()]))
            : rxjs_1.of(undefined);
    });
}
exports.indexToId = indexToId;
