// 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 CreateEquityCommandHandler_1;
import { CreateEquityCommand, CreateEquityCommandResponse } from "./CreateEquityCommand";
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 TransactionService from "../../../../service/transaction/TransactionService";
import { MirrorNodeAdapter } from "../../../../../port/out/mirror/MirrorNodeAdapter";
import EvmAddress from "../../../../../domain/context/contract/EvmAddress";
import { EquityDetails } from "../../../../../domain/context/equity/EquityDetails";
import ContractService from "../../../../service/contract/ContractService";
import AccountService from "../../../../service/account/AccountService";
import BigDecimal from "../../../../../domain/context/shared/BigDecimal";
import { CreateEquityCommandError } from "./error/CreateEquityCommandError";
import { MissingRegulationType } from "../../../../../domain/context/factory/error/MissingRegulationType";
import { MissingRegulationSubType } from "../../../../../domain/context/factory/error/MissingRegulationSubType";
import { EVM_ZERO_ADDRESS } from "../../../../../core/Constants";
let CreateEquityCommandHandler = CreateEquityCommandHandler_1 = class CreateEquityCommandHandler {
    transactionService;
    mirrorNodeAdapter;
    contractService;
    accountService;
    constructor(transactionService, mirrorNodeAdapter, contractService, accountService) {
        this.transactionService = transactionService;
        this.mirrorNodeAdapter = mirrorNodeAdapter;
        this.contractService = contractService;
        this.accountService = accountService;
    }
    async execute(command) {
        let res;
        try {
            const { security, factory, resolver, configId, configVersion, diamondOwnerAccount, votingRight, informationRight, liquidationRight, subscriptionRight, conversionRight, redemptionRight, putRight, dividendRight, currency, nominalValue, nominalValueDecimals, externalPausesIds, externalControlListsIds, externalKycListsIds, complianceId, identityRegistryId, } = command;
            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),
            ]);
            const complianceEvmAddress = complianceId
                ? await this.contractService.getContractEvmAddress(complianceId)
                : new EvmAddress(EVM_ZERO_ADDRESS);
            const identityRegistryEvmAddress = identityRegistryId
                ? await this.contractService.getContractEvmAddress(identityRegistryId)
                : new EvmAddress(EVM_ZERO_ADDRESS);
            const handler = this.transactionService.getHandler();
            const equityInfo = new EquityDetails(votingRight, informationRight, liquidationRight, subscriptionRight, conversionRight, redemptionRight, putRight, dividendRight, currency, BigDecimal.fromString(nominalValue), nominalValueDecimals);
            res = await handler.createEquity(new Security(security), equityInfo, factoryEvmAddress, resolverEvmAddress, configId, configVersion, complianceEvmAddress, identityRegistryEvmAddress, externalPausesEvmAddresses, externalControlListsEvmAddresses, externalKycListsEvmAddresses, diamondOwnerAccountEvmAddress, factory.toString());
            const contractAddress = await this.transactionService.getTransactionResult({
                res,
                result: res.response?.equityAddress,
                className: CreateEquityCommandHandler_1.name,
                position: 0,
                numberOfResultsItems: 1,
            });
            const contractId = await this.mirrorNodeAdapter.getHederaIdfromContractAddress(contractAddress);
            return Promise.resolve(new CreateEquityCommandResponse(new ContractId(contractId), res.id));
        }
        catch (error) {
            if (res?.response == 1)
                return Promise.resolve(new CreateEquityCommandResponse(new ContractId("0.0.0"), res.id));
            else
                throw new CreateEquityCommandError(error);
        }
    }
};
CreateEquityCommandHandler = CreateEquityCommandHandler_1 = __decorate([
    CommandHandler(CreateEquityCommand),
    __param(0, lazyInject(TransactionService)),
    __param(1, lazyInject(MirrorNodeAdapter)),
    __param(2, lazyInject(ContractService)),
    __param(3, lazyInject(AccountService)),
    __metadata("design:paramtypes", [TransactionService,
        MirrorNodeAdapter,
        ContractService,
        AccountService])
], CreateEquityCommandHandler);
export { CreateEquityCommandHandler };
