"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 });
exports.CommandOptions = void 0;
/*
 *
 * 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 symbol_hd_wallets_1 = require("symbol-hd-wallets");
const symbol_sdk_1 = require("symbol-sdk");
const create_profile_command_1 = require("../../interfaces/create.profile.command");
const create_profile_options_1 = require("../../interfaces/create.profile.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 network_resolver_1 = require("../../resolvers/network.resolver");
const networkCurrency_resolver_1 = require("../../resolvers/networkCurrency.resolver");
const password_resolver_1 = require("../../resolvers/password.resolver");
const pathNumber_resolver_1 = require("../../resolvers/pathNumber.resolver");
const profile_resolver_1 = require("../../resolvers/profile.resolver");
const save_resolver_1 = require("../../resolvers/save.resolver");
const url_resolver_1 = require("../../resolvers/url.resolver");
const derivation_service_1 = require("../../services/derivation.service");
const formatter_service_1 = require("../../services/formatter.service");
class CommandOptions extends create_profile_options_1.CreateProfileOptions {
}
__decorate([
    (0, clime_1.option)({
        flag: 's',
        description: '(Optional) Saves the profile.',
        toggle: true,
    }),
    __metadata("design:type", Object)
], CommandOptions.prototype, "save", void 0);
exports.CommandOptions = CommandOptions;
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);
        const save = await new save_resolver_1.SaveResolver().resolve(options);
        const importType = await new importType_resolver_1.ImportTypeResolver().resolve(options);
        if (importType == importType_enum_1.ImportType.Ledger) {
            throw new clime_1.ExpectedError('Ledger cannot be generated, use the profile import or profile create command!');
        }
        if (!save) {
            if (importType === importType_enum_1.ImportType.PrivateKey) {
                const account = symbol_sdk_1.Account.generateNewAccount(networkType);
                console.log(create_profile_command_1.AccountCredentialsTable.createFromAccount(account).toString());
            }
            else if (importType === importType_enum_1.ImportType.Mnemonic) {
                const pathNumber = await new pathNumber_resolver_1.PathNumberResolver().resolve(options);
                const mnemonic = symbol_hd_wallets_1.MnemonicPassPhrase.createRandom().plain;
                const privateKey = derivation_service_1.DerivationService.getPrivateKeyFromMnemonic(mnemonic, pathNumber, networkType);
                console.log(create_profile_command_1.AccountCredentialsTable.createFromAccount(symbol_sdk_1.Account.createFromPrivateKey(privateKey, networkType), mnemonic, pathNumber).toString());
            }
            else {
                throw new Error('Ledger cannot be generated, use the profile import or profile create command!');
            }
            return;
        }
        options.url = await new url_resolver_1.URLResolver().resolve(options);
        const name = await new profile_resolver_1.ProfileNameResolver().resolve(options);
        const password = await new password_resolver_1.PasswordResolver().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 baseArguments = {
            generationHash,
            isDefault,
            epochAdjustment,
            name,
            networkCurrency,
            networkType,
            url: options.url,
        };
        if (importType === importType_enum_1.ImportType.PrivateKey) {
            const { privateKey } = symbol_sdk_1.Account.generateNewAccount(networkType);
            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 mnemonic = symbol_hd_wallets_1.MnemonicPassPhrase.createRandom().plain;
            const args = { ...baseArguments, password, mnemonic, pathNumber: 1 };
            const profile = this.createProfile(args);
            console.log(profile.getTable(password).toString());
        }
        else {
            throw new Error('Ledger cannot be generated, use the profile import or profile create command!');
        }
        console.log(formatter_service_1.FormatterService.success('Stored ' + name + ' profile'));
    }
};
__decorate([
    clime_1.metadata,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [CommandOptions]),
    __metadata("design:returntype", Promise)
], default_1.prototype, "execute", null);
default_1 = __decorate([
    (0, clime_1.command)({
        description: 'Generate new accounts',
    }),
    __metadata("design:paramtypes", [])
], default_1);
exports.default = default_1;
