"use strict";
// SPDX-License-Identifier: Apache-2.0
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HTSTransactionResponseAdapter = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
const sdk_1 = require("@hiero-ledger/sdk");
const TransactionResponse_1 = __importDefault(require("../../../domain/context/transaction/TransactionResponse"));
const TransactionResponseError_1 = require("../../out/error/TransactionResponseError");
const TransactionResponseAdapter_1 = require("../../out/TransactionResponseAdapter");
const TransactionResponseEnums_1 = require("../../out/TransactionResponseEnums");
class HTSTransactionResponseAdapter extends TransactionResponseAdapter_1.TransactionResponseAdapter {
    static async manageResponse(network, transactionResponse, responseType = TransactionResponseEnums_1.TransactionType.RECEIPT, client, nameFunction, abi) {
        let results = new Uint8Array();
        if (responseType === TransactionResponseEnums_1.TransactionType.RECEIPT) {
            const transactionReceipt = await this.getReceipt(client, transactionResponse);
            const transId = transactionResponse.transactionId;
            return this.createTransactionResponse(transId, responseType, results, transactionReceipt);
        }
        if (responseType === TransactionResponseEnums_1.TransactionType.RECORD) {
            const transactionRecord = await this.getRecord(client, transactionResponse);
            let record;
            if (nameFunction) {
                if (transactionRecord instanceof sdk_1.TransactionRecord) {
                    record = transactionRecord?.contractFunctionResult?.bytes;
                }
                else if (transactionRecord instanceof Uint32Array) {
                    record = transactionRecord;
                }
                if (!record)
                    throw new TransactionResponseError_1.TransactionResponseError({
                        message: "Invalid response type",
                        network: network,
                    });
                results = this.decodeFunctionResult(nameFunction, record, abi, network);
            }
            if (record instanceof Uint32Array) {
                return this.createTransactionResponse(undefined, responseType, results, undefined);
            }
            else {
                const tr = transactionRecord;
                return this.createTransactionResponse(tr?.transactionId, responseType, results, tr?.receipt);
            }
        }
        throw new TransactionResponseError_1.TransactionResponseError({
            message: "The response type is neither RECORD nor RECEIPT.",
            network: network,
        });
    }
    static async getRecord(client, transactionResponse) {
        return await transactionResponse.getRecord(client);
    }
    static async getReceipt(client, transactionResponse) {
        return await transactionResponse.getReceipt(client);
    }
    static createTransactionResponse(transactionId, responseType, response, receipt) {
        return new TransactionResponse_1.default((transactionId ?? "").toString(), response);
    }
}
exports.HTSTransactionResponseAdapter = HTSTransactionResponseAdapter;
