"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.LedgerResolver = void 0;
const lib_1 = require("symbol-ledger-typescript/lib");
const options_resolver_1 = require("../options-resolver");
const ledger_service_1 = require("../services/ledger.service");
/**
 * Ledger accounts resolver
 */
class LedgerResolver {
    constructor(networkType, optin, spinner) {
        this.networkType = networkType;
        this.optin = optin;
        this.spinner = spinner;
    }
    /**
     * Resolves a mnemonic passphrase provided by the user.
     * @returns {Promise<string>}
     */
    async resolve(options) {
        this.spinner.start('Connecting to Ledger');
        const accounts = await new ledger_service_1.LedgerService().resolveLedgerAccountInformation(this.networkType, this.optin);
        this.spinner.stop();
        const choices = accounts.map((key, index) => ({
            // Index is shown as 1-based to match with other wallets UX
            title: `${index + 1} - ${key.address.plain()}`,
            value: index + 1,
        }));
        const accountIndex = (await (0, options_resolver_1.OptionsChoiceResolver)(options, 'pathNumber', 'Select the child account number:', choices, 'select', undefined)) - 1;
        return {
            path: lib_1.LedgerDerivationPath.getPath(this.networkType, accountIndex),
            publicKey: accounts[accountIndex].publicKey,
            optin: this.optin,
        };
    }
}
exports.LedgerResolver = LedgerResolver;
