"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.TransactionStatementViews = void 0;
const symbol_sdk_1 = require("symbol-sdk");
const recipients_view_1 = require("../recipients.view");
const abstractStatementView_view_1 = require("./abstractStatementView.view");
class TransactionStatementViews extends abstractStatementView_view_1.AbstractStatementView {
    constructor(statements) {
        super();
        this.statements = statements;
    }
    /**
     * Renders cell records
     * @returns {CellRecord[]}
     */
    render() {
        if (!this.statements.length) {
            return null;
        }
        return this.statements.map(({ height, source, receipts }, index, self) => [
            this.getSectionTitle(`Transaction statement ${index + 1} of ${self.length}`),
            { Height: height.compact() },
            { Source: `Primary Id: ${source.primaryId}, Secondary Id: ${source.secondaryId}` },
            this.getSectionTitle('Transaction receipts'),
            ...this.renderReceipts(receipts),
        ]);
    }
    /**
     * Renders receipts
     * @private
     * @param {Receipt[]} receipts
     * @returns {CellRecord[]}
     */
    renderReceipts(receipts) {
        return receipts
            .map((receipt, index, self) => this.renderReceipt(receipt, index, self.length))
            .reduce((acc, entry) => [...acc, ...entry], []);
    }
    /**
     * Renders a receipt
     * @private
     * @param {Receipt} receipt
     * @param {number} index
     * @param {number} numberOfReceipts
     * @returns {CellRecord[]}
     */
    renderReceipt(receipt, index, numberOfReceipts) {
        const { type, size } = receipt;
        return [
            this.getSectionTitle(`Receipt ${index + 1} of ${numberOfReceipts}`),
            { Type: symbol_sdk_1.ReceiptType[type] },
            { Size: size || 'N/A' },
            ...this.renderSpecificReceiptProperties(receipt),
        ];
    }
    /**
     * Renders a receipt specific properties
     * @private
     * @param {Receipt} receipt
     * @returns {CellRecord[]}
     */
    renderSpecificReceiptProperties(receipt) {
        if (receipt instanceof symbol_sdk_1.BalanceTransferReceipt) {
            return this.renderBalanceTransferReceipt(receipt);
        }
        if (receipt instanceof symbol_sdk_1.BalanceChangeReceipt) {
            return this.renderBalanceChangeReceipt(receipt);
        }
        if (receipt instanceof symbol_sdk_1.ArtifactExpiryReceipt) {
            return this.renderArtifactExpiryReceipt(receipt);
        }
        if (receipt instanceof symbol_sdk_1.InflationReceipt) {
            return this.renderInflationReceipt(receipt);
        }
        throw new Error('unknown receipt instance provided to renderSpecificReceiptProperties ' + receipt.type);
    }
    /**
     * Renders a balance transfer receipt
     * @private
     * @param {BalanceTransferReceipt} receipt
     * @returns {CellRecord[]}
     */
    renderBalanceTransferReceipt(receipt) {
        return [
            { Sender: receipt.senderAddress.pretty() },
            { Recipient: recipients_view_1.RecipientsView.get(receipt.recipientAddress) },
            { 'Mosaic Id': receipt.mosaicId.toHex() },
            { Amount: receipt.amount.compact() },
        ];
    }
    /**
     *  Renders a balance change receipt
     * @private
     * @param {BalanceChangeReceipt} receipt
     * @returns {CellRecord[]}
     */
    renderBalanceChangeReceipt(receipt) {
        return [
            { 'Target address': receipt.targetAddress.pretty() },
            { MosaicId: receipt.mosaicId.toHex() },
            { Amount: receipt.amount.compact() },
        ];
    }
    /**
     * Renders an artifact expiry receipt
     * @private
     * @param {ArtifactExpiryReceipt} receipt
     * @returns {CellRecord[]}
     */
    renderArtifactExpiryReceipt(receipt) {
        const { artifactId } = receipt;
        const artifactType = artifactId instanceof symbol_sdk_1.MosaicId ? 'Mosaic Id' : 'Namespace Id';
        return [{ [artifactType]: artifactId.toHex() }];
    }
    /**
     * Renders an inflation receipt
     * @private
     * @param {InflationReceipt} receipt
     * @returns {CellRecord[]}
     */
    renderInflationReceipt(receipt) {
        return [{ MosaicId: receipt.mosaicId.toHex() }, { Amount: receipt.amount.compact() }];
    }
}
exports.TransactionStatementViews = TransactionStatementViews;
