"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.TransactionView = void 0;
const table_builder_1 = require("../../table.builder");
const transaction_header_view_1 = require("./transaction.header.view");
const transaction_signature_view_1 = require("./transaction.signature.view");
const transactionDetailView_factory_1 = require("./transactionDetailView.factory");
class TransactionView {
    /**
     * Creates an instance of TransactionView.
     * @param {Transaction} transaction
     * @param {SignedTransaction} [signedTransaction?]
     * @param {Profile} profile
     */
    constructor(transaction, signedTransaction, profile) {
        this.header = transaction_header_view_1.TransactionHeaderView.get(transaction, profile);
        this.details = (0, transactionDetailView_factory_1.transactionDetailViewFactory)(transaction);
        this.signature = signedTransaction && transaction_signature_view_1.TransactionSignatureView.get(signedTransaction);
    }
    /**
     * Logs the table
     */
    print() {
        console.log(this.render().toString());
    }
    /**
     * @returns {HorizontalTable}
     */
    render() {
        return table_builder_1.TableBuilder.renderTableFromObject(this.sanitizedCellRecord);
    }
    /**
     * The whole CellRecord to render in a table, without empty values
     * @readonly
     * @protected
     * @type {CellRecord}
     */
    get sanitizedCellRecord() {
        const headerNoNull = this.filterNullValues(this.header);
        const detailsNoNull = this.filterNullValues(this.details);
        const signatureNoNull = this.signature ? this.filterNullValues(this.signature) : undefined;
        const allCellRecordsNoNull = {
            ...headerNoNull,
            ...detailsNoNull,
            ...signatureNoNull,
        };
        if (!allCellRecordsNoNull) {
            throw new Error('Something went wrong at sanitizedCellRecord');
        }
        return allCellRecordsNoNull;
    }
    /**
     * Filters out null values (eg. hash in ITransactionHeaderView)
     * @private
     * @param {CellRecord} cellRecord
     * @returns {CellRecord}
     */
    filterNullValues(cellRecord) {
        Object.keys(cellRecord).forEach((key) => cellRecord[key] == null && delete cellRecord[key]);
        return cellRecord;
    }
}
exports.TransactionView = TransactionView;
