"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
 *
 * 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.
 *
 */
const clime_1 = require("clime");
const create_profile_command_1 = require("../../interfaces/create.profile.command");
const importProfile_options_1 = require("../../interfaces/importProfile.options");
const importType_enum_1 = require("../../models/importType.enum");
const default_resolver_1 = require("../../resolvers/default.resolver");
const epochAdjustment_resolver_1 = require("../../resolvers/epochAdjustment.resolver");
const generationHash_resolver_1 = require("../../resolvers/generationHash.resolver");
const importType_resolver_1 = require("../../resolvers/importType.resolver");
const ledger_resolver_1 = require("../../resolvers/ledger.resolver");
const mnemonic_resolver_1 = require("../../resolvers/mnemonic.resolver");
const network_resolver_1 = require("../../resolvers/network.resolver");
const networkCurrency_resolver_1 = require("../../resolvers/networkCurrency.resolver");
const optin_resolver_1 = require("../../resolvers/optin.resolver");
const optinPathNumber_resolver_1 = require("../../resolvers/optinPathNumber.resolver");
const password_resolver_1 = require("../../resolvers/password.resolver");
const pathNumber_resolver_1 = require("../../resolvers/pathNumber.resolver");
const privateKey_resolver_1 = require("../../resolvers/privateKey.resolver");
const profile_resolver_1 = require("../../resolvers/profile.resolver");
const url_resolver_1 = require("../../resolvers/url.resolver");
const formatter_service_1 = require("../../services/formatter.service");
let default_1 = class default_1 extends create_profile_command_1.CreateProfileCommand {
    constructor() {
        super();
    }
    async execute(options) {
        const networkType = await new network_resolver_1.NetworkResolver().resolve(options);
        options.url = await new url_resolver_1.URLResolver().resolve(options);
        const name = await new profile_resolver_1.ProfileNameResolver().resolve(options);
        const isDefault = await new default_resolver_1.DefaultResolver().resolve(options);
        this.spinner.start();
        const generationHash = await new generationHash_resolver_1.GenerationHashResolver().resolve(options);
        const epochAdjustment = await new epochAdjustment_resolver_1.EpochAdjustmentResolver().resolve(options);
        const networkCurrency = await new networkCurrency_resolver_1.NetworkCurrencyResolver().resolve(options);
        this.spinner.stop();
        const importType = await new importType_resolver_1.ImportTypeResolver().resolve(options);
        const url = options.url;
        const baseArguments = {
            generationHash,
            epochAdjustment,
            isDefault,
            name,
            networkCurrency,
            networkType,
            url,
        };
        if (importType === importType_enum_1.ImportType.PrivateKey) {
            const password = await new password_resolver_1.PasswordResolver().resolve(options);
            const privateKey = await new privateKey_resolver_1.PrivateKeyResolver().resolve(options);
            const args = { ...baseArguments, password, privateKey };
            const profile = this.createProfile(args);
            console.log(profile.getTable(password).toString());
        }
        else if (importType === importType_enum_1.ImportType.Mnemonic) {
            const password = await new password_resolver_1.PasswordResolver().resolve(options);
            const mnemonic = await new mnemonic_resolver_1.MnemonicResolver().resolve(options);
            let optinPathNumber;
            if (options.pathNumber) {
                const optin = await new optin_resolver_1.OptinResolver().resolve(options);
                optinPathNumber = { pathNumber: await new pathNumber_resolver_1.PathNumberResolver().resolve(options), optin: optin };
            }
            else {
                optinPathNumber = await new optinPathNumber_resolver_1.OptinPathNumberResolver(mnemonic, networkType).resolve(options);
            }
            const args = { ...baseArguments, password, mnemonic, ...optinPathNumber };
            const profile = this.createProfile(args);
            console.log(profile.getTable(password).toString());
        }
        else if (importType === importType_enum_1.ImportType.Ledger) {
            const optin = await new optin_resolver_1.OptinResolver().resolve(options);
            const ledgerArguments = await new ledger_resolver_1.LedgerResolver(networkType, optin, this.spinner).resolve(options);
            const args = { ...baseArguments, ...ledgerArguments };
            const profile = this.createProfile(args);
            console.log(profile.getTable(undefined).toString());
        }
        else {
            throw new Error(`Invalid import type ${importType}`);
        }
        console.log(formatter_service_1.FormatterService.success(`Stored ${name} profile`));
    }
};
__decorate([
    clime_1.metadata,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [importProfile_options_1.ImportProfileOptions]),
    __metadata("design:returntype", Promise)
], default_1.prototype, "execute", null);
default_1 = __decorate([
    (0, clime_1.command)({
        description: 'Create a new profile with existing private key',
    }),
    __metadata("design:paramtypes", [])
], default_1);
exports.default = default_1;
