"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.ProfileCommand = void 0;
const clime_1 = require("clime");
const ora_1 = require("ora");
const app_conf_1 = require("../config/app.conf");
const password_resolver_1 = require("../resolvers/password.resolver");
const profile_repository_1 = require("../respositories/profile.repository");
const profile_service_1 = require("../services/profile.service");
/**
 * Base command class to use the stored profile.
 */
class ProfileCommand extends clime_1.Command {
    /**
     * Constructor.
     */
    constructor(fileUrl) {
        super();
        this.spinner = (0, ora_1.default)('Processing');
        const profileRepository = new profile_repository_1.ProfileRepository(fileUrl || app_conf_1.default.PROFILES_FILE_NAME);
        this.profileService = new profile_service_1.ProfileService(profileRepository);
    }
    /**
     * Gets profile by name.
     * @param {ProfileOptions} options - The  attribute "profile" should include the name.
     * @throws {ExpectedError}
     * @returns {Profile}
     */
    getProfile(options) {
        try {
            if (options.profile) {
                return this.profileService.findProfileNamed(options.profile);
            }
            return this.profileService.getDefaultProfile();
        }
        catch (err) {
            throw new clime_1.ExpectedError("Can't retrieve the current profile." +
                "Use 'symbol-cli profile list' to check whether the profile exist, " +
                "if not, use 'symbol-cli profile create' to create a new profile");
        }
    }
    /**
     *  Gets a signing account based on the profile.
     *
     * @param profile the profile
     * @param options the options
     */
    async getSigningAccount(profile, options) {
        return await profile.getSigningAccount(() => new password_resolver_1.PasswordResolver().resolve(options));
    }
    /**
     * Gets default profile.
     * @throws {ExpectedError}
     * @returns {Profile}
     */
    getDefaultProfile() {
        try {
            return this.profileService.getDefaultProfile();
        }
        catch (err) {
            throw new clime_1.ExpectedError("Can't retrieve the default profile." + "Use 'symbol-cli profile create' to create a new default profile");
        }
    }
    /**
     * Gets all profiles.
     * @throws {ExpectedError}
     * @returns {Profile[]}
     */
    findAllProfiles() {
        try {
            return this.profileService.findAllProfiles();
        }
        catch (err) {
            throw new clime_1.ExpectedError("Can't retrieve the profile list");
        }
    }
}
exports.ProfileCommand = ProfileCommand;
