"use strict";
/*
 *
 * Copyright 2018-present NEM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.OptinPathNumberResolver = void 0;
const symbol_hd_wallets_1 = require("symbol-hd-wallets");
const symbol_sdk_1 = require("symbol-sdk");
const keys_whitelist_1 = require("../config/keys-whitelist");
const options_resolver_1 = require("../options-resolver");
const account_service_1 = require("../services/account.service");
/**
 * Optin Adresses and Path Number resolver
 */
class OptinPathNumberResolver {
    constructor(mnemonic, networkType) {
        this.mnemonic = mnemonic;
        this.networkType = networkType;
    }
    /**
     * Resolves a path number provided by the user.
     * @returns {Promise<number>}
     */
    async resolve(options) {
        const possibleOptInAccounts = account_service_1.AccountService.generateAccountsFromMnemonic(this.mnemonic, this.networkType, symbol_hd_wallets_1.Network.BITCOIN);
        const optinPublicKeys = keys_whitelist_1.publicKeys
            ? keys_whitelist_1.publicKeys
            : {
                mainnet: [''],
                testnet: [''],
            };
        // whitelist opt in accounts
        const key = this.networkType === symbol_sdk_1.NetworkType.MAIN_NET ? 'mainnet' : 'testnet';
        const whitelisted = optinPublicKeys[key];
        const optInAccounts = possibleOptInAccounts
            .map((account, index) => {
            return { account, inx: index, optin: true };
        })
            .filter(({ account }) => whitelisted.indexOf(account.publicKey) >= 0);
        const hdAccounts = account_service_1.AccountService.generateAccountsFromMnemonic(this.mnemonic, this.networkType, symbol_hd_wallets_1.Network.SYMBOL);
        const choices = [
            ...optInAccounts,
            ...hdAccounts.map((account, inx) => ({ account, inx, optin: false })),
        ].map(({ account, inx, optin }, index) => ({
            title: `${index + 1} \t ${account.address.pretty()} ${optin ? '(Opt-in)' : ''}`,
            value: {
                pathNumber: inx,
                optin,
            },
        }));
        const value = await (0, options_resolver_1.OptionsChoiceResolver)(options, 'optinPathNumber', 'Select the child account to import:', choices, 'select', undefined);
        return value;
    }
}
exports.OptinPathNumberResolver = OptinPathNumberResolver;
