// 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);
};
/* eslint-disable @typescript-eslint/no-unused-vars */
import { LogError } from "../../../../core/decorator/LogErrorDecorator";
import ValidatedRequest from "../../../../core/validation/ValidatedArgs";
import { QueryBus } from "../../../../core/query/QueryBus";
import Injectable from "../../../../core/injectable/Injectable";
import { CommandBus } from "../../../../core/command/CommandBus";
import { ONE_THOUSAND } from "../../../../domain/context/shared/SecurityDate";
import { SetAmortizationCommand } from "../../../../app/usecase/command/amortization/set/SetAmortizationCommand";
import { CancelAmortizationCommand } from "../../../../app/usecase/command/amortization/cancel/CancelAmortizationCommand";
import { SetAmortizationHoldCommand } from "../../../../app/usecase/command/amortization/setHold/SetAmortizationHoldCommand";
import { ReleaseAmortizationHoldCommand } from "../../../../app/usecase/command/amortization/releaseHold/ReleaseAmortizationHoldCommand";
import { GetAmortizationQuery } from "../../../../app/usecase/query/amortization/getAmortization/GetAmortizationQuery";
import { GetAmortizationForQuery } from "../../../../app/usecase/query/amortization/getAmortizationFor/GetAmortizationForQuery";
import { GetAmortizationsForQuery } from "../../../../app/usecase/query/amortization/getAmortizationsFor/GetAmortizationsForQuery";
import { GetAmortizationsCountQuery } from "../../../../app/usecase/query/amortization/getAmortizationsCount/GetAmortizationsCountQuery";
import { GetAmortizationHoldersQuery } from "../../../../app/usecase/query/amortization/getAmortizationHolders/GetAmortizationHoldersQuery";
import { GetTotalAmortizationHoldersQuery } from "../../../../app/usecase/query/amortization/getTotalAmortizationHolders/GetTotalAmortizationHoldersQuery";
import { GetAmortizationPaymentAmountQuery } from "../../../../app/usecase/query/amortization/getAmortizationPaymentAmount/GetAmortizationPaymentAmountQuery";
import { GetActiveAmortizationHoldHoldersQuery } from "../../../../app/usecase/query/amortization/getActiveAmortizationHoldHolders/GetActiveAmortizationHoldHoldersQuery";
import { GetTotalActiveAmortizationHoldHoldersQuery } from "../../../../app/usecase/query/amortization/getTotalActiveAmortizationHoldHolders/GetTotalActiveAmortizationHoldHoldersQuery";
import { GetActiveAmortizationIdsQuery } from "../../../../app/usecase/query/amortization/getActiveAmortizationIds/GetActiveAmortizationIdsQuery";
import { GetTotalActiveAmortizationIdsQuery } from "../../../../app/usecase/query/amortization/getTotalActiveAmortizationIds/GetTotalActiveAmortizationIdsQuery";
import SetAmortizationRequest from "../../request/security/amortization/SetAmortizationRequest";
import CancelAmortizationRequest from "../../request/security/amortization/CancelAmortizationRequest";
import SetAmortizationHoldRequest from "../../request/security/amortization/SetAmortizationHoldRequest";
import ReleaseAmortizationHoldRequest from "../../request/security/amortization/ReleaseAmortizationHoldRequest";
import GetAmortizationRequest from "../../request/security/amortization/GetAmortizationRequest";
import GetAmortizationForRequest from "../../request/security/amortization/GetAmortizationForRequest";
import GetAmortizationsForRequest from "../../request/security/amortization/GetAmortizationsForRequest";
import GetAmortizationsCountRequest from "../../request/security/amortization/GetAmortizationsCountRequest";
import GetAmortizationHoldersRequest from "../../request/security/amortization/GetAmortizationHoldersRequest";
import GetTotalAmortizationHoldersRequest from "../../request/security/amortization/GetTotalAmortizationHoldersRequest";
import GetAmortizationPaymentAmountRequest from "../../request/security/amortization/GetAmortizationPaymentAmountRequest";
import GetActiveAmortizationHoldHoldersRequest from "../../request/security/amortization/GetActiveAmortizationHoldHoldersRequest";
import GetTotalActiveAmortizationHoldHoldersRequest from "../../request/security/amortization/GetTotalActiveAmortizationHoldHoldersRequest";
import GetActiveAmortizationIdsRequest from "../../request/security/amortization/GetActiveAmortizationIdsRequest";
import GetTotalActiveAmortizationIdsRequest from "../../request/security/amortization/GetTotalActiveAmortizationIdsRequest";
class AmortizationInPort {
    queryBus;
    commandBus;
    constructor(queryBus = Injectable.resolve(QueryBus), commandBus = Injectable.resolve(CommandBus)) {
        this.queryBus = queryBus;
        this.commandBus = commandBus;
    }
    async setAmortization(request) {
        const { securityId, recordTimestamp, executionTimestamp, tokensToRedeem } = request;
        ValidatedRequest.handleValidation("SetAmortizationRequest", request);
        return await this.commandBus.execute(new SetAmortizationCommand(securityId, recordTimestamp, executionTimestamp, tokensToRedeem));
    }
    async cancelAmortization(request) {
        const { securityId, amortizationId } = request;
        ValidatedRequest.handleValidation("CancelAmortizationRequest", request);
        return await this.commandBus.execute(new CancelAmortizationCommand(securityId, amortizationId));
    }
    async setAmortizationHold(request) {
        const { securityId, amortizationId, tokenHolder, tokenAmount } = request;
        ValidatedRequest.handleValidation("SetAmortizationHoldRequest", request);
        return await this.commandBus.execute(new SetAmortizationHoldCommand(securityId, amortizationId, tokenHolder, tokenAmount));
    }
    async releaseAmortizationHold(request) {
        const { securityId, amortizationId, tokenHolder } = request;
        ValidatedRequest.handleValidation("ReleaseAmortizationHoldRequest", request);
        return await this.commandBus.execute(new ReleaseAmortizationHoldCommand(securityId, amortizationId, tokenHolder));
    }
    async getAmortization(request) {
        ValidatedRequest.handleValidation("GetAmortizationRequest", request);
        const res = await this.queryBus.execute(new GetAmortizationQuery(request.securityId, request.amortizationId));
        const amortization = {
            amortizationId: request.amortizationId,
            recordDate: new Date(res.amortization.amortization.recordDate * ONE_THOUSAND),
            executionDate: new Date(res.amortization.amortization.executionDate * ONE_THOUSAND),
            tokensToRedeem: res.amortization.amortization.tokensToRedeem.toString(),
            snapshotId: res.amortization.snapshotId,
            isDisabled: res.amortization.isDisabled,
        };
        return amortization;
    }
    async getAllAmortizations(request) {
        ValidatedRequest.handleValidation("GetAmortizationsCountRequest", request);
        const count = await this.queryBus.execute(new GetAmortizationsCountQuery(request.securityId));
        if (count.payload === 0)
            return [];
        const amortizations = [];
        for (let i = 1; i <= count.payload; i++) {
            const res = await this.queryBus.execute(new GetAmortizationQuery(request.securityId, i));
            const amortization = {
                amortizationId: i,
                recordDate: new Date(res.amortization.amortization.recordDate * ONE_THOUSAND),
                executionDate: new Date(res.amortization.amortization.executionDate * ONE_THOUSAND),
                tokensToRedeem: res.amortization.amortization.tokensToRedeem.toString(),
                snapshotId: res.amortization.snapshotId,
                isDisabled: res.amortization.isDisabled,
            };
            amortizations.push(amortization);
        }
        return amortizations;
    }
    async getAmortizationFor(request) {
        ValidatedRequest.handleValidation("GetAmortizationForRequest", request);
        const res = await this.queryBus.execute(new GetAmortizationForQuery(request.securityId, request.targetId, request.amortizationId));
        return _mapAmortizationFor(res.amortizationFor);
    }
    async getAmortizationsFor(request) {
        ValidatedRequest.handleValidation("GetAmortizationsForRequest", request);
        const res = await this.queryBus.execute(new GetAmortizationsForQuery(request.securityId, request.amortizationId, request.start, request.end));
        return res.payload.map(_mapAmortizationFor);
    }
    async getAmortizationsCount(request) {
        ValidatedRequest.handleValidation("GetAmortizationsCountRequest", request);
        return (await this.queryBus.execute(new GetAmortizationsCountQuery(request.securityId))).payload;
    }
    async getAmortizationHolders(request) {
        const { securityId, amortizationId, start, end } = request;
        ValidatedRequest.handleValidation(GetAmortizationHoldersRequest.name, request);
        return (await this.queryBus.execute(new GetAmortizationHoldersQuery(securityId, amortizationId, start, end)))
            .payload;
    }
    async getTotalAmortizationHolders(request) {
        const { securityId, amortizationId } = request;
        ValidatedRequest.handleValidation(GetTotalAmortizationHoldersRequest.name, request);
        return (await this.queryBus.execute(new GetTotalAmortizationHoldersQuery(securityId, amortizationId))).payload;
    }
    async getAmortizationPaymentAmount(request) {
        ValidatedRequest.handleValidation("GetAmortizationPaymentAmountRequest", request);
        const res = await this.queryBus.execute(new GetAmortizationPaymentAmountQuery(request.securityId, request.amortizationId, request.tokenHolder));
        const paymentAmount = {
            tokenAmount: res.amortizationPaymentAmount.tokenAmount.toString(),
            decimals: res.amortizationPaymentAmount.decimals,
        };
        return paymentAmount;
    }
    async getActiveAmortizationHoldHolders(request) {
        const { securityId, amortizationId, start, end } = request;
        ValidatedRequest.handleValidation(GetActiveAmortizationHoldHoldersRequest.name, request);
        return (await this.queryBus.execute(new GetActiveAmortizationHoldHoldersQuery(securityId, amortizationId, start, end))).payload;
    }
    async getTotalActiveAmortizationHoldHolders(request) {
        const { securityId, amortizationId } = request;
        ValidatedRequest.handleValidation(GetTotalActiveAmortizationHoldHoldersRequest.name, request);
        return (await this.queryBus.execute(new GetTotalActiveAmortizationHoldHoldersQuery(securityId, amortizationId)))
            .payload;
    }
    async getActiveAmortizationIds(request) {
        const { securityId, start, end } = request;
        ValidatedRequest.handleValidation(GetActiveAmortizationIdsRequest.name, request);
        return (await this.queryBus.execute(new GetActiveAmortizationIdsQuery(securityId, start, end))).payload;
    }
    async getTotalActiveAmortizationIds(request) {
        ValidatedRequest.handleValidation(GetTotalActiveAmortizationIdsRequest.name, request);
        return (await this.queryBus.execute(new GetTotalActiveAmortizationIdsQuery(request.securityId))).payload;
    }
}
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [SetAmortizationRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "setAmortization", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [CancelAmortizationRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "cancelAmortization", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [SetAmortizationHoldRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "setAmortizationHold", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ReleaseAmortizationHoldRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "releaseAmortizationHold", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAmortization", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationsCountRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAllAmortizations", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationForRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAmortizationFor", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationsForRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAmortizationsFor", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationsCountRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAmortizationsCount", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationHoldersRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAmortizationHolders", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetTotalAmortizationHoldersRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getTotalAmortizationHolders", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationPaymentAmountRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAmortizationPaymentAmount", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetActiveAmortizationHoldHoldersRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getActiveAmortizationHoldHolders", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetTotalActiveAmortizationHoldHoldersRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getTotalActiveAmortizationHoldHolders", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetActiveAmortizationIdsRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getActiveAmortizationIds", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetTotalActiveAmortizationIdsRequest]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getTotalActiveAmortizationIds", null);
function _mapAmortizationFor(af) {
    return {
        account: af.account,
        recordDate: new Date(af.recordDate * ONE_THOUSAND),
        executionDate: new Date(af.executionDate * ONE_THOUSAND),
        holdId: af.holdId,
        holdActive: af.holdActive,
        tokenHeldAmount: af.tokenHeldAmount.toString(),
        decimalsHeld: af.decimalsHeld,
        abafAtHold: af.abafAtHold.toString(),
        tokenBalance: af.tokenBalance.toString(),
        decimalsBalance: af.decimalsBalance,
        recordDateReached: af.recordDateReached,
        abafAtSnapshot: af.abafAtSnapshot.toString(),
        nominalValue: af.nominalValue.toString(),
        nominalValueDecimals: af.nominalValueDecimals,
    };
}
const AmortizationToken = new AmortizationInPort();
export default AmortizationToken;
