"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.LedgerProfile = exports.LedgerWallet = void 0;
const symbol_sdk_1 = require("symbol-sdk");
const ledger_service_1 = require("../services/ledger.service");
const networkCurrency_model_1 = require("./networkCurrency.model");
const profile_model_1 = require("./profile.model");
class LedgerWallet {
    constructor(name, address, publicKey, path, optin) {
        this.name = name;
        this.address = address;
        this.publicKey = publicKey;
        this.path = path;
        this.optin = optin;
        this.networkType = address.networkType;
    }
    /**
     * Instantiate a LedgerWallet from a DTO
     * @param dto ledger wallet without prototype
     * @returns an instance of LedgerWallet
     */
    static createFromDTO(dto) {
        return new LedgerWallet(dto.name, symbol_sdk_1.Address.createFromRawAddress(dto.address.address), dto.publicKey, dto.path, dto.optin);
    }
    /**
     * Creates a LedgerWallet DTO
     * @returns {ILedgerWalletDTO}
     */
    toDTO() {
        return JSON.parse(JSON.stringify(this));
    }
}
exports.LedgerWallet = LedgerWallet;
/**
 * Hd Profile model
 * @export
 * @class HdProfile
 * @extends {Profile}
 */
class LedgerProfile extends profile_model_1.Profile {
    /**
     * Creates a new LEDGER Profile from a DTO
     * @static
     * @param {LedgerProfileDTO} DTO
     * @returns {LedgerProfile}
     */
    static createFromDTO(DTO) {
        return new LedgerProfile(LedgerWallet.createFromDTO(DTO.simpleWallet), DTO.url, DTO.networkGenerationHash, DTO.epochAdjustment || profile_model_1.epochAdjustment, networkCurrency_model_1.NetworkCurrency.createFromDTO(DTO.networkCurrency), DTO.version, DTO.type, DTO.default);
    }
    /**
     * Creates a new LEDGER Profile
     * @static
     * @param {LedgerProfileCreation} args
     * @returns
     */
    static create(args) {
        const simpleWallet = new LedgerWallet(args.name, symbol_sdk_1.Address.createFromPublicKey(args.publicKey, args.networkType), args.publicKey, args.path, args.optin);
        return new LedgerProfile(simpleWallet, args.url, args.generationHash, args.epochAdjustment || profile_model_1.epochAdjustment, args.networkCurrency, profile_model_1.CURRENT_PROFILE_VERSION, 'Ledger', args.isDefault ? '1' : '0');
    }
    /**
     * Creates a DTO
     * @returns {LedgerProfileDTO}
     */
    toDTO() {
        return {
            simpleWallet: this.simpleWallet.toDTO(),
            url: this.url,
            networkGenerationHash: this.networkGenerationHash,
            epochAdjustment: this.epochAdjustment,
            networkCurrency: this.networkCurrency.toDTO(),
            version: this.version,
            default: this.isDefault,
            type: this.type,
        };
    }
    isPasswordValid() {
        throw new Error("Ledger profiles don't need a password.");
    }
    decrypt() {
        throw new Error("Ledger profiles don't need a password.");
    }
    getAccount(password) {
        if (password) {
            throw new Error('Password must is not required provided');
        }
        return symbol_sdk_1.PublicAccount.createFromPublicKey(this.simpleWallet.publicKey, this.networkType);
    }
    getTable(password) {
        const table = this.getBaseTable();
        if (password) {
            table.push(['Password', password.value]);
        }
        table.push(['Public Key', this.simpleWallet.publicKey]);
        table.push(['Path', this.simpleWallet.path]);
        table.push(['OptIn', this.simpleWallet.optin]);
        return table;
    }
    async getSigningAccount() {
        return new ledger_service_1.LedgerService().resolveLedgerAccount(this.networkType, this.simpleWallet.path, this.simpleWallet.optin);
    }
}
exports.LedgerProfile = LedgerProfile;
