"use strict";
// 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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const tsyringe_1 = require("tsyringe");
const Injectable_1 = __importDefault(require("../../../core/injectable/Injectable"));
const RPCTransactionAdapter_1 = require("../../../port/out/rpc/RPCTransactionAdapter");
const Service_1 = __importDefault(require("../Service"));
const Wallet_1 = require("../../../domain/context/network/Wallet");
const InvalidWalletAccountTypeError_1 = require("../../../domain/context/network/error/InvalidWalletAccountTypeError");
const LogService_1 = __importDefault(require("../log/LogService"));
const HederaWalletConnectTransactionAdapter_1 = require("../../../port/out/hs/walletconnect/HederaWalletConnectTransactionAdapter");
const DFNSTransactionAdapter_1 = require("../../../port/out/hs/custodial/DFNSTransactionAdapter");
const FireblocksTransactionAdapter_1 = require("../../../port/out/hs/custodial/FireblocksTransactionAdapter");
const AWSKMSTransactionAdapter_1 = require("../../../port/out/hs/custodial/AWSKMSTransactionAdapter");
const WalletNotSupported_1 = require("./error/WalletNotSupported");
const InvalidResponse_1 = require("../../../core/error/InvalidResponse");
const MirrorNodeAdapter_1 = require("../../../port/out/mirror/MirrorNodeAdapter");
const EmptyResponse_1 = require("./error/EmptyResponse");
const Constants_1 = require("../../../core/Constants");
let TransactionService = class TransactionService extends Service_1.default {
    mirrorNodeAdapter;
    constructor(mirrorNodeAdapter = Injectable_1.default.resolve(MirrorNodeAdapter_1.MirrorNodeAdapter)) {
        super();
        this.mirrorNodeAdapter = mirrorNodeAdapter;
    }
    getHandler() {
        return Injectable_1.default.resolveTransactionHandler();
    }
    setHandler(adp) {
        Injectable_1.default.registerTransactionHandler(adp);
        return adp;
    }
    static getHandlerClass(type) {
        switch (type) {
            case Wallet_1.SupportedWallets.METAMASK:
                if (!Injectable_1.default.isWeb()) {
                    throw new InvalidWalletAccountTypeError_1.InvalidWalletTypeError();
                }
                LogService_1.default.logTrace("METAMASK TransactionAdapter");
                return Injectable_1.default.resolve(RPCTransactionAdapter_1.RPCTransactionAdapter);
            case Wallet_1.SupportedWallets.HWALLETCONNECT:
                if (!Injectable_1.default.isWeb()) {
                    throw new InvalidWalletAccountTypeError_1.InvalidWalletTypeError();
                }
                LogService_1.default.logTrace("HWALLETCONNECT TransactionAdapter");
                return Injectable_1.default.resolve(HederaWalletConnectTransactionAdapter_1.HederaWalletConnectTransactionAdapter);
            case Wallet_1.SupportedWallets.DFNS:
                LogService_1.default.logTrace("DFNS TransactionAdapter");
                return Injectable_1.default.resolve(DFNSTransactionAdapter_1.DFNSTransactionAdapter);
            case Wallet_1.SupportedWallets.FIREBLOCKS:
                LogService_1.default.logTrace("FIREBLOCKS TransactionAdapter");
                return Injectable_1.default.resolve(FireblocksTransactionAdapter_1.FireblocksTransactionAdapter);
            case Wallet_1.SupportedWallets.AWSKMS:
                LogService_1.default.logTrace("AWSKMS TransactionAdapter");
                return Injectable_1.default.resolve(AWSKMSTransactionAdapter_1.AWSKMSTransactionAdapter);
            default:
                throw new WalletNotSupported_1.WalletNotSupported();
        }
    }
    async getTransactionResult({ res, result, className, position, numberOfResultsItems, isContractCreation, }) {
        if (!res.id)
            throw new EmptyResponse_1.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_1.InvalidResponse(results);
        }
        if ([
            "CreateEquityCommandHandler",
            "CreateBondCommandHandler",
            "CreateBondFixedRateCommandHandler",
            "CreateBondKpiLinkedRateCommandHandler",
            "CreateTrexSuiteBondCommandHandler",
            "CreateTrexSuiteEquityCommandHandler",
            "SetRateCommandHandler",
        ].some((handler) => className.includes(handler))) {
            const data = results.map((result) => result.substring(Constants_1.BYTES_32_LENGTH - Constants_1.ADDRESS_LENGTH + 2));
            return `0x${data[position]}`;
        }
        return results[position];
    }
};
TransactionService = __decorate([
    (0, tsyringe_1.singleton)(),
    __metadata("design:paramtypes", [MirrorNodeAdapter_1.MirrorNodeAdapter])
], TransactionService);
exports.default = TransactionService;
