"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.CreateProfileCommand = exports.AccountCredentialsTable = void 0;
const Table = require("cli-table3");
const clime_1 = require("clime");
const ora_1 = require("ora");
const symbol_sdk_1 = require("symbol-sdk");
const app_conf_1 = require("../config/app.conf");
const profile_repository_1 = require("../respositories/profile.repository");
const derivation_service_1 = require("../services/derivation.service");
const formatter_service_1 = require("../services/formatter.service");
const profile_service_1 = require("../services/profile.service");
class AccountCredentialsTable {
    constructor(args) {
        this.account = args.account;
        this.table = new Table({
            style: { head: ['cyan'] },
            head: ['Property', 'Value'],
            colWidths: [15, 70],
            wordWrap: true,
        });
        this.renderAccountProperties();
        if (args.password) {
            this.table.push(['Password', args.password.value]);
        }
        if (args.mnemonic) {
            this.table.push(['Mnemonic', args.mnemonic]);
        }
        if (args.pathNumber !== undefined && args.pathNumber !== null) {
            const networkType = args.account.address.networkType;
            this.table.push([
                'Path',
                // Address paths are 0-based but shown as 1-based to the users
                `Seed wallet n. ${args.pathNumber + 1} (${derivation_service_1.DerivationService.getPathFromPathNumber(args.pathNumber, networkType)})`,
            ]);
        }
    }
    static createFromAccount(account, mnemonic, pathNumber) {
        return new AccountCredentialsTable({ account, mnemonic, pathNumber });
    }
    renderAccountProperties() {
        this.table.push(['Address', this.account.address.pretty()], ['Public Key', this.account.publicKey]);
        if (this.account instanceof symbol_sdk_1.Account) {
            this.table.push(['Private Key', this.account.privateKey]);
        }
    }
    toString() {
        let text = '';
        text += formatter_service_1.FormatterService.title('Account');
        text += '\n' + this.table.toString();
        return text;
    }
}
exports.AccountCredentialsTable = AccountCredentialsTable;
/**
 * Base command class to create a new profile.
 */
class CreateProfileCommand extends clime_1.Command {
    /**
     * Constructor.
     */
    constructor(fileUrl) {
        super();
        this.spinner = (0, ora_1.default)('Processing');
        this.profileRepository = new profile_repository_1.ProfileRepository(fileUrl || app_conf_1.default.PROFILES_FILE_NAME);
        this.profileService = new profile_service_1.ProfileService(this.profileRepository);
    }
    /**
     * Creates a new profile.
     * @protected
     * @param {ProfileCreation} args creation arguments
     * @returns {Profile}
     */
    createProfile(args) {
        try {
            const profile = this.profileService.createNewProfile(args);
            if (args.isDefault) {
                this.setDefaultProfile(args.name);
            }
            return profile;
        }
        catch (ignored) {
            throw new clime_1.ExpectedError(`The profile [${args.name}] already exists`);
        }
    }
    /**
     * Sets a profile as default.
     * @param {string} profileName - The name of the profile to set as default.
     * @throws {ExpectedError}
     */
    setDefaultProfile(profileName) {
        try {
            this.profileService.setDefaultProfile(profileName);
        }
        catch (err) {
            throw new clime_1.ExpectedError("Can't set the profile [" +
                profileName +
                '] as the default profile.' +
                "Use 'symbol-cli profile list' to check whether the profile exist, " +
                "if not, use 'symbol-cli profile create' to create a profile");
        }
    }
}
exports.CreateProfileCommand = CreateProfileCommand;
