"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_sdk_1 = require("symbol-sdk");
const announce_transactions_command_1 = require("../../interfaces/announce.transactions.command");
const announce_transactions_options_1 = require("../../interfaces/announce.transactions.options");
const aggregateType_resolver_1 = require("../../resolvers/aggregateType.resolver");
const maxFee_resolver_1 = require("../../resolvers/maxFee.resolver");
const mosaic_resolver_1 = require("../../resolvers/mosaic.resolver");
const publicKey_resolver_1 = require("../../resolvers/publicKey.resolver");
const account_service_1 = require("../../services/account.service");
const accountkeylink_1 = require("./accountkeylink");
const votingkeylink_1 = require("./votingkeylink");
const vrfkeylink_1 = require("./vrfkeylink");
class CommandOptions extends announce_transactions_options_1.AnnounceTransactionsOptions {
}
__decorate([
    (0, clime_1.option)({
        description: 'Linked Remote Account Public Key.',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "remotePublicKey", void 0);
__decorate([
    (0, clime_1.option)({
        description: 'Remote Account Key Link action (Link, Unlink).',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "remoteLinkAction", void 0);
__decorate([
    (0, clime_1.option)({
        description: 'Linked Voting Account Public Key.',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "votingPublicKey", void 0);
__decorate([
    (0, clime_1.option)({
        description: 'Voting Key Link Start Point.',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "votingStartPoint", void 0);
__decorate([
    (0, clime_1.option)({
        description: 'Voting Key Link End Point.',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "votingEndPoint", void 0);
__decorate([
    (0, clime_1.option)({
        description: 'Voting Key Link action (Link, Unlink).',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "votingLinkAction", void 0);
__decorate([
    (0, clime_1.option)({
        description: 'Linked VRF Account Public Key.',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "vrfPublicKey", void 0);
__decorate([
    (0, clime_1.option)({
        description: 'VRF Key Link action (Link, Unlink).',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "vrfLinkAction", void 0);
__decorate([
    (0, clime_1.option)({
        flag: 't',
        description: 'Aggregate Type (AGGREGATE_BONDED or AGGREGATE_COMPLETE)',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "aggregateType", void 0);
__decorate([
    (0, clime_1.option)({
        description: 'Cosigner Account Public Key',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "mainAccountPublicKey", void 0);
__decorate([
    (0, clime_1.option)({
        description: '(Optional) Enrol Transport Public Key',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "enrolTransportPublicKey", void 0);
__decorate([
    (0, clime_1.option)({
        description: '(Optional) Enrol Agent url',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "enrolAgentUrl", void 0);
__decorate([
    (0, clime_1.option)({
        description: '(Optional) Enrol Recipient Address',
    }),
    __metadata("design:type", String)
], CommandOptions.prototype, "enrolRecipientAdress", void 0);
exports.CommandOptions = CommandOptions;
let default_1 = class default_1 extends announce_transactions_command_1.AnnounceTransactionsCommand {
    constructor() {
        super();
    }
    async execute(options) {
        const profile = this.getProfile(options);
        const account = await this.getSigningAccount(profile, options);
        const maxFee = await new maxFee_resolver_1.MaxFeeResolver().resolve(options);
        console.log('Account Key Link Transaction:');
        const accountKeyLinkTx = await new accountkeylink_1.default().createTransaction(maxFee, options, profile, 'remotePublicKey', 'remoteLinkAction');
        console.log('Voting Key Link Transaction:');
        const votingKeyLinkTx = await new votingkeylink_1.default().createTransaction(maxFee, options, profile, 'votingPublicKey', 'votingLinkAction', 'votingStartPoint', 'votingEndPoint');
        console.log('Vrf Key Link Transaction:');
        const vrfKeyLinkTx = await new vrfkeylink_1.default().createTransaction(maxFee, options, profile, 'vrfPublicKey', 'vrfLinkAction');
        const aggregateType = await new aggregateType_resolver_1.AggregateTypeResolver().resolve(options);
        let publicAccount = await new publicKey_resolver_1.OptionalPublicKeyResolver().resolve(options, profile.networkType, 'Who is going to sign these transactions? Enter public key (Leave blank to use current account):', 'mainAccountPublicKey');
        if (!publicAccount) {
            publicAccount = account.publicAccount;
        }
        const multisigSigner = { publicAccount };
        const transactions = [accountKeyLinkTx, votingKeyLinkTx, vrfKeyLinkTx];
        const transactionSigners = [undefined, undefined, undefined];
        const zeroValueMosaics = await new mosaic_resolver_1.MosaicsResolver().resolve({ mosaics: '@symbol.xym::0' });
        if (options.enrolTransportPublicKey && options.enrolAgentUrl) {
            const enrolRecipientAdress = account_service_1.AccountService.getUnresolvedAddress(options.enrolRecipientAdress ? options.enrolRecipientAdress : 'TDL73SDUMPDK7EOF7H3O4F5WB5WHG2SX7XUSFZQ');
            const message = symbol_sdk_1.PlainMessage.create(`enrol ${options.enrolTransportPublicKey} ${options.enrolAgentUrl}`);
            transactions.push(symbol_sdk_1.TransferTransaction.create(symbol_sdk_1.Deadline.create(profile.epochAdjustment), enrolRecipientAdress, zeroValueMosaics, message, profile.networkType, maxFee));
            transactionSigners.push(undefined);
        }
        // for an aggregate-bonded tx to be valid, an inner transaction(announcing account as a signer) needs to be added
        transactions.push(symbol_sdk_1.TransferTransaction.create(symbol_sdk_1.Deadline.create(profile.epochAdjustment), publicAccount.address, zeroValueMosaics, symbol_sdk_1.PlainMessage.create(''), profile.networkType, maxFee));
        transactionSigners.push(account.publicAccount);
        const signatureOptions = {
            account,
            transactions,
            maxFee,
            multisigSigner,
            isAggregate: true,
            isAggregateBonded: aggregateType === symbol_sdk_1.TransactionType.AGGREGATE_BONDED,
            transactionSigners,
        };
        const signedTransactions = await this.signTransactions(signatureOptions, options);
        this.announceTransactions(options, signedTransactions);
    }
};
__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: 'Wraps all key link transactions(Remote Account, Voting, VRF) and optionally a SuperNode enrol transaction with an aggregate bonded transaction to be cosigned by another account. This is useful to setup a node from one account but keep the main account (the one with the funds) isolated.',
    }),
    __metadata("design:paramtypes", [])
], default_1);
exports.default = default_1;
