"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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var CreateEquityCommandHandler_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateEquityCommandHandler = void 0;
const CreateEquityCommand_1 = require("./CreateEquityCommand");
const InvalidRequest_1 = require("../../error/InvalidRequest");
const CommandHandlerDecorator_1 = require("../../../../../core/decorator/CommandHandlerDecorator");
const LazyInjectDecorator_1 = require("../../../../../core/decorator/LazyInjectDecorator");
const ContractId_1 = __importDefault(require("../../../../../domain/context/contract/ContractId"));
const Security_1 = require("../../../../../domain/context/security/Security");
const TransactionService_1 = __importDefault(require("../../../../service/transaction/TransactionService"));
const MirrorNodeAdapter_1 = require("../../../../../port/out/mirror/MirrorNodeAdapter");
const EvmAddress_1 = __importDefault(require("../../../../../domain/context/contract/EvmAddress"));
const EquityDetails_1 = require("../../../../../domain/context/equity/EquityDetails");
const ContractService_1 = __importDefault(require("../../../../service/contract/ContractService"));
const AccountService_1 = __importDefault(require("../../../../service/account/AccountService"));
const BigDecimal_1 = __importDefault(require("../../../../../domain/context/shared/BigDecimal"));
const CreateEquityCommandError_1 = require("./error/CreateEquityCommandError");
const MissingRegulationType_1 = require("../../../../../domain/context/factory/error/MissingRegulationType");
const MissingRegulationSubType_1 = require("../../../../../domain/context/factory/error/MissingRegulationSubType");
const Constants_1 = require("../../../../../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_1.InvalidRequest("Factory not found in request");
            }
            if (!resolver) {
                throw new InvalidRequest_1.InvalidRequest("Resolver not found in request");
            }
            if (!configId) {
                throw new InvalidRequest_1.InvalidRequest("Config Id not found in request");
            }
            if (configVersion === undefined) {
                throw new InvalidRequest_1.InvalidRequest("Config Version not found in request");
            }
            if (!security.regulationType) {
                throw new MissingRegulationType_1.MissingRegulationType();
            }
            if (!security.regulationsubType) {
                throw new MissingRegulationSubType_1.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_1.default(Constants_1.EVM_ZERO_ADDRESS);
            const identityRegistryEvmAddress = identityRegistryId
                ? await this.contractService.getContractEvmAddress(identityRegistryId)
                : new EvmAddress_1.default(Constants_1.EVM_ZERO_ADDRESS);
            const handler = this.transactionService.getHandler();
            const equityInfo = new EquityDetails_1.EquityDetails(votingRight, informationRight, liquidationRight, subscriptionRight, conversionRight, redemptionRight, putRight, dividendRight, currency, BigDecimal_1.default.fromString(nominalValue), nominalValueDecimals);
            res = await handler.createEquity(new Security_1.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 CreateEquityCommand_1.CreateEquityCommandResponse(new ContractId_1.default(contractId), res.id));
        }
        catch (error) {
            if (res?.response == 1)
                return Promise.resolve(new CreateEquityCommand_1.CreateEquityCommandResponse(new ContractId_1.default("0.0.0"), res.id));
            else
                throw new CreateEquityCommandError_1.CreateEquityCommandError(error);
        }
    }
};
exports.CreateEquityCommandHandler = CreateEquityCommandHandler;
exports.CreateEquityCommandHandler = CreateEquityCommandHandler = CreateEquityCommandHandler_1 = __decorate([
    (0, CommandHandlerDecorator_1.CommandHandler)(CreateEquityCommand_1.CreateEquityCommand),
    __param(0, (0, LazyInjectDecorator_1.lazyInject)(TransactionService_1.default)),
    __param(1, (0, LazyInjectDecorator_1.lazyInject)(MirrorNodeAdapter_1.MirrorNodeAdapter)),
    __param(2, (0, LazyInjectDecorator_1.lazyInject)(ContractService_1.default)),
    __param(3, (0, LazyInjectDecorator_1.lazyInject)(AccountService_1.default)),
    __metadata("design:paramtypes", [TransactionService_1.default,
        MirrorNodeAdapter_1.MirrorNodeAdapter,
        ContractService_1.default,
        AccountService_1.default])
], CreateEquityCommandHandler);
