"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.MultisigInfoTable = exports.BalanceInfoTable = exports.AccountInfoTable = 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 Table = require("cli-table3");
const clime_1 = require("clime");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const symbol_sdk_1 = require("symbol-sdk");
const profile_command_1 = require("../../interfaces/profile.command");
const profile_options_1 = require("../../interfaces/profile.options");
const address_resolver_1 = require("../../resolvers/address.resolver");
const formatter_service_1 = require("../../services/formatter.service");
class CommandOptions extends profile_options_1.ProfileOptions {
}
__decorate([
    (0, clime_1.option)({
        flag: 'a',
        description: 'Account address.',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "address", void 0);
exports.CommandOptions = CommandOptions;
class AccountInfoTable {
    constructor(accountInfo) {
        this.accountInfo = accountInfo;
        this.table = new Table({
            style: { head: ['cyan'] },
            head: ['Property', 'Value'],
        });
        this.table.push(['Address', accountInfo.address.pretty()], ['Address Height', accountInfo.addressHeight.toString()], ['Public Key', accountInfo.publicKey], ['Public Key Height', accountInfo.publicKeyHeight.toString()], ['Importance', accountInfo.importance.toString()], ['Importance Height', accountInfo.importanceHeight.toString()]);
    }
    toString() {
        let text = '';
        text += formatter_service_1.FormatterService.title('Account Information');
        text += '\n' + this.table.toString();
        return text;
    }
}
exports.AccountInfoTable = AccountInfoTable;
class BalanceInfoTable {
    constructor(mosaicsInfo) {
        this.mosaicsInfo = mosaicsInfo;
        if (mosaicsInfo.length > 0) {
            this.table = new Table({
                style: { head: ['cyan'] },
                head: ['Mosaic Id', 'Relative Amount', 'Absolute Amount', 'Expiration Height'],
            });
            mosaicsInfo.map((mosaic) => {
                this.table.push([
                    mosaic.fullName(),
                    mosaic.relativeAmount().toLocaleString(),
                    mosaic.amount.toString(),
                    mosaic.mosaicInfo.duration.compact() === 0
                        ? 'Never'
                        : mosaic.mosaicInfo.startHeight.add(mosaic.mosaicInfo.duration).toString(),
                ]);
            });
        }
    }
    toString() {
        let text = '';
        if (this.table) {
            text += formatter_service_1.FormatterService.title('Balance Information');
            text += '\n' + this.table.toString();
        }
        return text;
    }
}
exports.BalanceInfoTable = BalanceInfoTable;
class MultisigInfoTable {
    constructor(multisigAccountInfo) {
        this.multisigAccountInfo = multisigAccountInfo;
        if (multisigAccountInfo && multisigAccountInfo.cosignatoryAddresses.length > 0) {
            this.multisigTable = new Table({
                style: { head: ['cyan'] },
                head: ['Property', 'Value'],
            });
            this.multisigTable.push(['Min Approval', multisigAccountInfo.minApproval.toString()], ['Min Removal', multisigAccountInfo.minRemoval.toString()]);
            this.cosignatoriesTable = new Table({
                style: { head: ['cyan'] },
                head: ['Address'],
            });
            multisigAccountInfo.cosignatoryAddresses.map((address) => {
                this.cosignatoriesTable.push([address.pretty()]);
            });
        }
        if (multisigAccountInfo && multisigAccountInfo.multisigAddresses.length > 0) {
            this.cosignatoryOfTable = new Table({
                style: { head: ['cyan'] },
                head: ['Address'],
            });
            multisigAccountInfo.multisigAddresses.map((address) => {
                this.cosignatoryOfTable.push([address.pretty()]);
            });
        }
    }
    toString() {
        let text = '';
        if (this.multisigTable) {
            text += formatter_service_1.FormatterService.title('Multisig Account Information');
            text += '\n' + this.multisigTable.toString();
            text += formatter_service_1.FormatterService.title('Cosignatories');
            text += '\n' + this.cosignatoriesTable.toString();
        }
        if (this.cosignatoryOfTable) {
            text += formatter_service_1.FormatterService.title('Is cosignatory of');
            text += '\n' + this.cosignatoryOfTable.toString();
        }
        return text;
    }
}
exports.MultisigInfoTable = MultisigInfoTable;
let default_1 = class default_1 extends profile_command_1.ProfileCommand {
    constructor() {
        super();
    }
    async execute(options) {
        const profile = this.getProfile(options);
        const address = await new address_resolver_1.AddressResolver().resolve(options, profile);
        this.spinner.start();
        const repositoryFactory = profile.repositoryFactory;
        const accountHttp = repositoryFactory.createAccountRepository();
        const multisigHttp = repositoryFactory.createMultisigRepository();
        const mosaicHttp = repositoryFactory.createMosaicRepository();
        const mosaicService = new symbol_sdk_1.MosaicService(accountHttp, mosaicHttp);
        (0, rxjs_1.forkJoin)(accountHttp.getAccountInfo(address), mosaicService.mosaicsAmountViewFromAddress(address).pipe((0, operators_1.mergeMap)((_) => _), (0, operators_1.toArray)()), multisigHttp.getMultisigAccountInfo(address).pipe((0, operators_1.catchError)((ignored) => (0, rxjs_1.of)(null)))).subscribe((res) => {
            const accountInfo = res[0];
            const mosaicsInfo = res[1];
            const multisigInfo = res[2];
            console.log(new AccountInfoTable(accountInfo).toString(), new BalanceInfoTable(mosaicsInfo).toString(), new MultisigInfoTable(multisigInfo).toString());
            this.spinner.stop();
        }, (err) => {
            this.spinner.stop();
            console.log(formatter_service_1.FormatterService.error(err));
            if (err instanceof Object) {
                console.log(formatter_service_1.FormatterService.info('The account has to receive at least one transaction to be recorded on the network'));
            }
        });
    }
};
__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: 'Get account information',
    }),
    __metadata("design:paramtypes", [])
], default_1);
exports.default = default_1;
