// SPDX-License-Identifier: Apache-2.0
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { TransactionRecord, } from "@hiero-ledger/sdk";
import TransactionResponse from "../../../domain/context/transaction/TransactionResponse";
import { TransactionResponseError } from "../../out/error/TransactionResponseError";
import { TransactionResponseAdapter } from "../../out/TransactionResponseAdapter";
import { TransactionType } from "../../out/TransactionResponseEnums";
export class HTSTransactionResponseAdapter extends TransactionResponseAdapter {
    static async manageResponse(network, transactionResponse, responseType = TransactionType.RECEIPT, client, nameFunction, abi) {
        let results = new Uint8Array();
        if (responseType === TransactionType.RECEIPT) {
            const transactionReceipt = await this.getReceipt(client, transactionResponse);
            const transId = transactionResponse.transactionId;
            return this.createTransactionResponse(transId, responseType, results, transactionReceipt);
        }
        if (responseType === TransactionType.RECORD) {
            const transactionRecord = await this.getRecord(client, transactionResponse);
            let record;
            if (nameFunction) {
                if (transactionRecord instanceof TransactionRecord) {
                    record = transactionRecord?.contractFunctionResult?.bytes;
                }
                else if (transactionRecord instanceof Uint32Array) {
                    record = transactionRecord;
                }
                if (!record)
                    throw new 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({
            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((transactionId ?? "").toString(), response);
    }
}
