// 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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var CreateBondFixedRateCommandHandler_1;
import { CreateBondFixedRateCommand, CreateBondFixedRateCommandResponse } from "./CreateBondFixedRateCommand";
import { InvalidRequest } from "../../error/InvalidRequest";
import { CommandHandler } from "../../../../../core/decorator/CommandHandlerDecorator";
import { lazyInject } from "../../../../../core/decorator/LazyInjectDecorator";
import ContractId from "../../../../../domain/context/contract/ContractId";
import { Security } from "../../../../../domain/context/security/Security";
import AccountService from "../../../../service/account/AccountService";
import TransactionService from "../../../../service/transaction/TransactionService";
import { MirrorNodeAdapter } from "../../../../../port/out/mirror/MirrorNodeAdapter";
import EvmAddress from "../../../../../domain/context/contract/EvmAddress";
import BigDecimal from "../../../../../domain/context/shared/BigDecimal";
import ContractService from "../../../../service/contract/ContractService";
import { CreateBondFixedRateCommandError } from "./error/CreateBondFixedRateCommandError";
import { MissingRegulationType } from "../../../../../domain/context/factory/error/MissingRegulationType";
import { MissingRegulationSubType } from "../../../../../domain/context/factory/error/MissingRegulationSubType";
import { EVM_ZERO_ADDRESS } from "../../../../../core/Constants";
import { BondFixedRateDetails } from "../../../../../domain/context/bond/BondFixedRateDetails";
let CreateBondFixedRateCommandHandler = CreateBondFixedRateCommandHandler_1 = class CreateBondFixedRateCommandHandler {
    accountService;
    transactionService;
    mirrorNodeAdapter;
    contractService;
    constructor(accountService, transactionService, mirrorNodeAdapter, contractService) {
        this.accountService = accountService;
        this.transactionService = transactionService;
        this.mirrorNodeAdapter = mirrorNodeAdapter;
        this.contractService = contractService;
    }
    async execute(command) {
        let res;
        try {
            const { security, currency, nominalValue, nominalValueDecimals, startingDate, maturityDate, factory, resolver, configId, configVersion, diamondOwnerAccount, externalPausesIds, externalControlListsIds, externalKycListsIds, complianceId, identityRegistryId, proceedRecipientsIds, proceedRecipientsData, } = command;
            //TODO: Boy scout: remove request validations and adjust test
            if (!factory) {
                throw new InvalidRequest("Factory not found in request");
            }
            if (!resolver) {
                throw new InvalidRequest("Resolver not found in request");
            }
            if (!configId) {
                throw new InvalidRequest("Config Id not found in request");
            }
            if (configVersion === undefined) {
                throw new InvalidRequest("Config Version not found in request");
            }
            if (!security.regulationType) {
                throw new MissingRegulationType();
            }
            if (!security.regulationsubType) {
                throw new MissingRegulationSubType();
            }
            const diamondOwnerAccountEvmAddress = await this.accountService.getAccountEvmAddress(diamondOwnerAccount);
            const factoryEvmAddress = await this.contractService.getContractEvmAddress(factory.toString());
            const resolverEvmAddress = await this.contractService.getContractEvmAddress(resolver.toString());
            const [externalPausesEvmAddresses, externalControlListsEvmAddresses, externalKycListsEvmAddresses] = await Promise.all([
                this.contractService.getEvmAddressesFromHederaIds(externalPausesIds),
                this.contractService.getEvmAddressesFromHederaIds(externalControlListsIds),
                this.contractService.getEvmAddressesFromHederaIds(externalKycListsIds),
            ]);
            let proceedRecipientsEvmAddresses = [];
            if (proceedRecipientsIds)
                proceedRecipientsEvmAddresses = await Promise.all(proceedRecipientsIds.map(async (id) => await this.accountService.getAccountEvmAddress(id)));
            const complianceEvmAddress = complianceId
                ? await this.contractService.getContractEvmAddress(complianceId)
                : new EvmAddress(EVM_ZERO_ADDRESS);
            const identityRegistryAddress = identityRegistryId
                ? await this.contractService.getContractEvmAddress(identityRegistryId)
                : new EvmAddress(EVM_ZERO_ADDRESS);
            const handler = this.transactionService.getHandler();
            const bondFixedRateInfo = new BondFixedRateDetails(currency, BigDecimal.fromString(nominalValue), nominalValueDecimals, parseInt(startingDate), parseInt(maturityDate), command.rate, command.rateDecimals);
            res = await handler.createBondFixedRate(new Security(security), bondFixedRateInfo, factoryEvmAddress, resolverEvmAddress, configId, configVersion, complianceEvmAddress, identityRegistryAddress, externalPausesEvmAddresses, externalControlListsEvmAddresses, externalKycListsEvmAddresses, diamondOwnerAccountEvmAddress, proceedRecipientsEvmAddresses, proceedRecipientsData, factory.toString());
            const contractAddress = await this.transactionService.getTransactionResult({
                res,
                result: res.response?.bondAddress,
                className: CreateBondFixedRateCommandHandler_1.name,
                position: 0,
                numberOfResultsItems: 1,
            });
            const contractId = await this.mirrorNodeAdapter.getHederaIdfromContractAddress(contractAddress);
            return Promise.resolve(new CreateBondFixedRateCommandResponse(new ContractId(contractId), res.id));
        }
        catch (error) {
            if (res?.response == 1) {
                return Promise.resolve(new CreateBondFixedRateCommandResponse(new ContractId("0.0.0"), res.id));
            }
            throw new CreateBondFixedRateCommandError(error);
        }
    }
};
CreateBondFixedRateCommandHandler = CreateBondFixedRateCommandHandler_1 = __decorate([
    CommandHandler(CreateBondFixedRateCommand),
    __param(0, lazyInject(AccountService)),
    __param(1, lazyInject(TransactionService)),
    __param(2, lazyInject(MirrorNodeAdapter)),
    __param(3, lazyInject(ContractService)),
    __metadata("design:paramtypes", [AccountService,
        TransactionService,
        MirrorNodeAdapter,
        ContractService])
], CreateBondFixedRateCommandHandler);
export { CreateBondFixedRateCommandHandler };
