// SPDX-License-Identifier: Apache-2.0
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { singleton } from "tsyringe";
import Injectable from "../../../core/injectable/Injectable";
import { RPCTransactionAdapter } from "../../../port/out/rpc/RPCTransactionAdapter";
import Service from "../Service";
import { SupportedWallets } from "../../../domain/context/network/Wallet";
import { InvalidWalletTypeError } from "../../../domain/context/network/error/InvalidWalletAccountTypeError";
import LogService from "../log/LogService";
import { HederaWalletConnectTransactionAdapter } from "../../../port/out/hs/walletconnect/HederaWalletConnectTransactionAdapter";
import { DFNSTransactionAdapter } from "../../../port/out/hs/custodial/DFNSTransactionAdapter";
import { FireblocksTransactionAdapter } from "../../../port/out/hs/custodial/FireblocksTransactionAdapter";
import { AWSKMSTransactionAdapter } from "../../../port/out/hs/custodial/AWSKMSTransactionAdapter";
import { WalletNotSupported } from "./error/WalletNotSupported";
import { InvalidResponse } from "../../../core/error/InvalidResponse";
import { MirrorNodeAdapter } from "../../../port/out/mirror/MirrorNodeAdapter";
import { EmptyResponse } from "./error/EmptyResponse";
import { ADDRESS_LENGTH, BYTES_32_LENGTH } from "../../../core/Constants";
let TransactionService = class TransactionService extends Service {
    mirrorNodeAdapter;
    constructor(mirrorNodeAdapter = Injectable.resolve(MirrorNodeAdapter)) {
        super();
        this.mirrorNodeAdapter = mirrorNodeAdapter;
    }
    getHandler() {
        return Injectable.resolveTransactionHandler();
    }
    setHandler(adp) {
        Injectable.registerTransactionHandler(adp);
        return adp;
    }
    static getHandlerClass(type) {
        switch (type) {
            case SupportedWallets.METAMASK:
                if (!Injectable.isWeb()) {
                    throw new InvalidWalletTypeError();
                }
                LogService.logTrace("METAMASK TransactionAdapter");
                return Injectable.resolve(RPCTransactionAdapter);
            case SupportedWallets.HWALLETCONNECT:
                if (!Injectable.isWeb()) {
                    throw new InvalidWalletTypeError();
                }
                LogService.logTrace("HWALLETCONNECT TransactionAdapter");
                return Injectable.resolve(HederaWalletConnectTransactionAdapter);
            case SupportedWallets.DFNS:
                LogService.logTrace("DFNS TransactionAdapter");
                return Injectable.resolve(DFNSTransactionAdapter);
            case SupportedWallets.FIREBLOCKS:
                LogService.logTrace("FIREBLOCKS TransactionAdapter");
                return Injectable.resolve(FireblocksTransactionAdapter);
            case SupportedWallets.AWSKMS:
                LogService.logTrace("AWSKMS TransactionAdapter");
                return Injectable.resolve(AWSKMSTransactionAdapter);
            default:
                throw new WalletNotSupported();
        }
    }
    async getTransactionResult({ res, result, className, position, numberOfResultsItems, isContractCreation, }) {
        if (!res.id)
            throw new EmptyResponse(className);
        if (result) {
            return result;
        }
        let results;
        if (isContractCreation) {
            results = await this.mirrorNodeAdapter.getContractResults(res.id.toString(), numberOfResultsItems, true);
        }
        else {
            results = await this.mirrorNodeAdapter.getContractResults(res.id.toString(), numberOfResultsItems);
        }
        if (!results || results.length !== numberOfResultsItems) {
            throw new InvalidResponse(results);
        }
        if ([
            "CreateEquityCommandHandler",
            "CreateBondCommandHandler",
            "CreateBondFixedRateCommandHandler",
            "CreateBondKpiLinkedRateCommandHandler",
            "CreateTrexSuiteBondCommandHandler",
            "CreateTrexSuiteEquityCommandHandler",
            "SetRateCommandHandler",
        ].some((handler) => className.includes(handler))) {
            const data = results.map((result) => result.substring(BYTES_32_LENGTH - ADDRESS_LENGTH + 2));
            return `0x${data[position]}`;
        }
        return results[position];
    }
};
TransactionService = __decorate([
    singleton(),
    __metadata("design:paramtypes", [MirrorNodeAdapter])
], TransactionService);
export default TransactionService;
