"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.AnnounceTransactionsCommand = void 0;
const clime_1 = require("clime");
const symbol_sdk_1 = require("symbol-sdk");
const address_resolver_1 = require("../resolvers/address.resolver");
const transactionAnnounceMode_resolver_1 = require("../resolvers/transactionAnnounceMode.resolver");
const multisig_service_1 = require("../services/multisig.service");
const transaction_announce_service_1 = require("../services/transaction.announce.service");
const transaction_signature_service_1 = require("../services/transaction.signature.service");
const profile_command_1 = require("./profile.command");
/**
 * Base command class to announce transactions.
 */
class AnnounceTransactionsCommand extends profile_command_1.ProfileCommand {
    constructor() {
        super();
    }
    /**
     * Gets the signer multisig info if the transaction is multisig
     * @protected
     * @param {AnnounceTransactionsOptions} options
     * @throws {ExpectedError}
     * @returns {(Promise<MultisigAccount | null>)}
     */
    async getMultisigSigner(options) {
        const profile = this.getProfile(options);
        const announceMode = await new transactionAnnounceMode_resolver_1.TransactionAnnounceModeResolver(profile.networkType).resolve(options);
        if (announceMode === transactionAnnounceMode_resolver_1.TransactionAnnounceMode.normal) {
            return null;
        }
        // Get the profile's multisig accounts info
        const childMultisigAccountsInfo = await new multisig_service_1.MultisigService(profile).getChildrenMultisigAccountInfo();
        if (!childMultisigAccountsInfo) {
            throw new clime_1.ExpectedError('The selected profile does not have multisig accounts');
        }
        // A signer public key was provided as an option
        if (options.signer) {
            const signerPublicAccount = symbol_sdk_1.PublicAccount.createFromPublicKey(options.signer, profile.networkType);
            const multisigInfo = childMultisigAccountsInfo.find(({ accountAddress }) => accountAddress.equals(signerPublicAccount.address));
            if (!multisigInfo) {
                throw new clime_1.ExpectedError(`
                    ${options.signer} is not a multisig account of the profile ${profile.name}
                `);
            }
            return { info: multisigInfo, publicAccount: signerPublicAccount };
        }
        const availableAddresses = childMultisigAccountsInfo.map(({ accountAddress }) => accountAddress.plain());
        const chosenSigner = await new address_resolver_1.AddressChoiceResolver().resolve(availableAddresses);
        const chosenMultisigSigner = childMultisigAccountsInfo.find(({ accountAddress }) => accountAddress.equals(chosenSigner));
        const chosenSignerPublicAccount = (await new symbol_sdk_1.AccountHttp(profile.url).getAccountInfo(chosenSigner).toPromise()).publicAccount;
        if (!chosenMultisigSigner || !chosenSignerPublicAccount) {
            throw new clime_1.ExpectedError('Could not retrieve the multisig account information from the node.');
        }
        return { info: chosenMultisigSigner, publicAccount: chosenSignerPublicAccount };
    }
    /**
     * Signs a set of regular transactions.
     * @protected
     * @param {TransactionSignatureOptions} signatureOptions
     * @param {AnnounceTransactionsOptions} options
     * @returns { Promise<SignedTransaction[]> }
     */
    async signTransactions(signatureOptions, options) {
        try {
            const profile = this.getProfile(options);
            return await transaction_signature_service_1.TransactionSignatureService.create(profile, options).signTransactions(signatureOptions);
        }
        finally {
            await signatureOptions.account.close();
        }
    }
    /**
     * Announces a set of transactions.
     * @protected
     * @param {AnnounceTransactionsOptions} options
     * @param {SignedTransaction[]} signedTransactions
     */
    async announceTransactions(options, signedTransactions) {
        const profile = this.getProfile(options);
        transaction_announce_service_1.TransactionAnnounceService.create(profile, options).announce(signedTransactions);
    }
}
exports.AnnounceTransactionsCommand = AnnounceTransactionsCommand;
