"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 });
/* eslint-disable @typescript-eslint/no-unused-vars */
const LogErrorDecorator_1 = require("../../../../core/decorator/LogErrorDecorator");
const ValidatedArgs_1 = __importDefault(require("../../../../core/validation/ValidatedArgs"));
const QueryBus_1 = require("../../../../core/query/QueryBus");
const Injectable_1 = __importDefault(require("../../../../core/injectable/Injectable"));
const CommandBus_1 = require("../../../../core/command/CommandBus");
const SecurityDate_1 = require("../../../../domain/context/shared/SecurityDate");
const SetAmortizationCommand_1 = require("../../../../app/usecase/command/amortization/set/SetAmortizationCommand");
const CancelAmortizationCommand_1 = require("../../../../app/usecase/command/amortization/cancel/CancelAmortizationCommand");
const SetAmortizationHoldCommand_1 = require("../../../../app/usecase/command/amortization/setHold/SetAmortizationHoldCommand");
const ReleaseAmortizationHoldCommand_1 = require("../../../../app/usecase/command/amortization/releaseHold/ReleaseAmortizationHoldCommand");
const GetAmortizationQuery_1 = require("../../../../app/usecase/query/amortization/getAmortization/GetAmortizationQuery");
const GetAmortizationForQuery_1 = require("../../../../app/usecase/query/amortization/getAmortizationFor/GetAmortizationForQuery");
const GetAmortizationsForQuery_1 = require("../../../../app/usecase/query/amortization/getAmortizationsFor/GetAmortizationsForQuery");
const GetAmortizationsCountQuery_1 = require("../../../../app/usecase/query/amortization/getAmortizationsCount/GetAmortizationsCountQuery");
const GetAmortizationHoldersQuery_1 = require("../../../../app/usecase/query/amortization/getAmortizationHolders/GetAmortizationHoldersQuery");
const GetTotalAmortizationHoldersQuery_1 = require("../../../../app/usecase/query/amortization/getTotalAmortizationHolders/GetTotalAmortizationHoldersQuery");
const GetAmortizationPaymentAmountQuery_1 = require("../../../../app/usecase/query/amortization/getAmortizationPaymentAmount/GetAmortizationPaymentAmountQuery");
const GetActiveAmortizationHoldHoldersQuery_1 = require("../../../../app/usecase/query/amortization/getActiveAmortizationHoldHolders/GetActiveAmortizationHoldHoldersQuery");
const GetTotalActiveAmortizationHoldHoldersQuery_1 = require("../../../../app/usecase/query/amortization/getTotalActiveAmortizationHoldHolders/GetTotalActiveAmortizationHoldHoldersQuery");
const GetActiveAmortizationIdsQuery_1 = require("../../../../app/usecase/query/amortization/getActiveAmortizationIds/GetActiveAmortizationIdsQuery");
const GetTotalActiveAmortizationIdsQuery_1 = require("../../../../app/usecase/query/amortization/getTotalActiveAmortizationIds/GetTotalActiveAmortizationIdsQuery");
const SetAmortizationRequest_1 = __importDefault(require("../../request/security/amortization/SetAmortizationRequest"));
const CancelAmortizationRequest_1 = __importDefault(require("../../request/security/amortization/CancelAmortizationRequest"));
const SetAmortizationHoldRequest_1 = __importDefault(require("../../request/security/amortization/SetAmortizationHoldRequest"));
const ReleaseAmortizationHoldRequest_1 = __importDefault(require("../../request/security/amortization/ReleaseAmortizationHoldRequest"));
const GetAmortizationRequest_1 = __importDefault(require("../../request/security/amortization/GetAmortizationRequest"));
const GetAmortizationForRequest_1 = __importDefault(require("../../request/security/amortization/GetAmortizationForRequest"));
const GetAmortizationsForRequest_1 = __importDefault(require("../../request/security/amortization/GetAmortizationsForRequest"));
const GetAmortizationsCountRequest_1 = __importDefault(require("../../request/security/amortization/GetAmortizationsCountRequest"));
const GetAmortizationHoldersRequest_1 = __importDefault(require("../../request/security/amortization/GetAmortizationHoldersRequest"));
const GetTotalAmortizationHoldersRequest_1 = __importDefault(require("../../request/security/amortization/GetTotalAmortizationHoldersRequest"));
const GetAmortizationPaymentAmountRequest_1 = __importDefault(require("../../request/security/amortization/GetAmortizationPaymentAmountRequest"));
const GetActiveAmortizationHoldHoldersRequest_1 = __importDefault(require("../../request/security/amortization/GetActiveAmortizationHoldHoldersRequest"));
const GetTotalActiveAmortizationHoldHoldersRequest_1 = __importDefault(require("../../request/security/amortization/GetTotalActiveAmortizationHoldHoldersRequest"));
const GetActiveAmortizationIdsRequest_1 = __importDefault(require("../../request/security/amortization/GetActiveAmortizationIdsRequest"));
const GetTotalActiveAmortizationIdsRequest_1 = __importDefault(require("../../request/security/amortization/GetTotalActiveAmortizationIdsRequest"));
class AmortizationInPort {
    queryBus;
    commandBus;
    constructor(queryBus = Injectable_1.default.resolve(QueryBus_1.QueryBus), commandBus = Injectable_1.default.resolve(CommandBus_1.CommandBus)) {
        this.queryBus = queryBus;
        this.commandBus = commandBus;
    }
    async setAmortization(request) {
        const { securityId, recordTimestamp, executionTimestamp, tokensToRedeem } = request;
        ValidatedArgs_1.default.handleValidation("SetAmortizationRequest", request);
        return await this.commandBus.execute(new SetAmortizationCommand_1.SetAmortizationCommand(securityId, recordTimestamp, executionTimestamp, tokensToRedeem));
    }
    async cancelAmortization(request) {
        const { securityId, amortizationId } = request;
        ValidatedArgs_1.default.handleValidation("CancelAmortizationRequest", request);
        return await this.commandBus.execute(new CancelAmortizationCommand_1.CancelAmortizationCommand(securityId, amortizationId));
    }
    async setAmortizationHold(request) {
        const { securityId, amortizationId, tokenHolder, tokenAmount } = request;
        ValidatedArgs_1.default.handleValidation("SetAmortizationHoldRequest", request);
        return await this.commandBus.execute(new SetAmortizationHoldCommand_1.SetAmortizationHoldCommand(securityId, amortizationId, tokenHolder, tokenAmount));
    }
    async releaseAmortizationHold(request) {
        const { securityId, amortizationId, tokenHolder } = request;
        ValidatedArgs_1.default.handleValidation("ReleaseAmortizationHoldRequest", request);
        return await this.commandBus.execute(new ReleaseAmortizationHoldCommand_1.ReleaseAmortizationHoldCommand(securityId, amortizationId, tokenHolder));
    }
    async getAmortization(request) {
        ValidatedArgs_1.default.handleValidation("GetAmortizationRequest", request);
        const res = await this.queryBus.execute(new GetAmortizationQuery_1.GetAmortizationQuery(request.securityId, request.amortizationId));
        const amortization = {
            amortizationId: request.amortizationId,
            recordDate: new Date(res.amortization.amortization.recordDate * SecurityDate_1.ONE_THOUSAND),
            executionDate: new Date(res.amortization.amortization.executionDate * SecurityDate_1.ONE_THOUSAND),
            tokensToRedeem: res.amortization.amortization.tokensToRedeem.toString(),
            snapshotId: res.amortization.snapshotId,
            isDisabled: res.amortization.isDisabled,
        };
        return amortization;
    }
    async getAllAmortizations(request) {
        ValidatedArgs_1.default.handleValidation("GetAmortizationsCountRequest", request);
        const count = await this.queryBus.execute(new GetAmortizationsCountQuery_1.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_1.GetAmortizationQuery(request.securityId, i));
            const amortization = {
                amortizationId: i,
                recordDate: new Date(res.amortization.amortization.recordDate * SecurityDate_1.ONE_THOUSAND),
                executionDate: new Date(res.amortization.amortization.executionDate * SecurityDate_1.ONE_THOUSAND),
                tokensToRedeem: res.amortization.amortization.tokensToRedeem.toString(),
                snapshotId: res.amortization.snapshotId,
                isDisabled: res.amortization.isDisabled,
            };
            amortizations.push(amortization);
        }
        return amortizations;
    }
    async getAmortizationFor(request) {
        ValidatedArgs_1.default.handleValidation("GetAmortizationForRequest", request);
        const res = await this.queryBus.execute(new GetAmortizationForQuery_1.GetAmortizationForQuery(request.securityId, request.targetId, request.amortizationId));
        return _mapAmortizationFor(res.amortizationFor);
    }
    async getAmortizationsFor(request) {
        ValidatedArgs_1.default.handleValidation("GetAmortizationsForRequest", request);
        const res = await this.queryBus.execute(new GetAmortizationsForQuery_1.GetAmortizationsForQuery(request.securityId, request.amortizationId, request.start, request.end));
        return res.payload.map(_mapAmortizationFor);
    }
    async getAmortizationsCount(request) {
        ValidatedArgs_1.default.handleValidation("GetAmortizationsCountRequest", request);
        return (await this.queryBus.execute(new GetAmortizationsCountQuery_1.GetAmortizationsCountQuery(request.securityId))).payload;
    }
    async getAmortizationHolders(request) {
        const { securityId, amortizationId, start, end } = request;
        ValidatedArgs_1.default.handleValidation(GetAmortizationHoldersRequest_1.default.name, request);
        return (await this.queryBus.execute(new GetAmortizationHoldersQuery_1.GetAmortizationHoldersQuery(securityId, amortizationId, start, end)))
            .payload;
    }
    async getTotalAmortizationHolders(request) {
        const { securityId, amortizationId } = request;
        ValidatedArgs_1.default.handleValidation(GetTotalAmortizationHoldersRequest_1.default.name, request);
        return (await this.queryBus.execute(new GetTotalAmortizationHoldersQuery_1.GetTotalAmortizationHoldersQuery(securityId, amortizationId))).payload;
    }
    async getAmortizationPaymentAmount(request) {
        ValidatedArgs_1.default.handleValidation("GetAmortizationPaymentAmountRequest", request);
        const res = await this.queryBus.execute(new GetAmortizationPaymentAmountQuery_1.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;
        ValidatedArgs_1.default.handleValidation(GetActiveAmortizationHoldHoldersRequest_1.default.name, request);
        return (await this.queryBus.execute(new GetActiveAmortizationHoldHoldersQuery_1.GetActiveAmortizationHoldHoldersQuery(securityId, amortizationId, start, end))).payload;
    }
    async getTotalActiveAmortizationHoldHolders(request) {
        const { securityId, amortizationId } = request;
        ValidatedArgs_1.default.handleValidation(GetTotalActiveAmortizationHoldHoldersRequest_1.default.name, request);
        return (await this.queryBus.execute(new GetTotalActiveAmortizationHoldHoldersQuery_1.GetTotalActiveAmortizationHoldHoldersQuery(securityId, amortizationId)))
            .payload;
    }
    async getActiveAmortizationIds(request) {
        const { securityId, start, end } = request;
        ValidatedArgs_1.default.handleValidation(GetActiveAmortizationIdsRequest_1.default.name, request);
        return (await this.queryBus.execute(new GetActiveAmortizationIdsQuery_1.GetActiveAmortizationIdsQuery(securityId, start, end))).payload;
    }
    async getTotalActiveAmortizationIds(request) {
        ValidatedArgs_1.default.handleValidation(GetTotalActiveAmortizationIdsRequest_1.default.name, request);
        return (await this.queryBus.execute(new GetTotalActiveAmortizationIdsQuery_1.GetTotalActiveAmortizationIdsQuery(request.securityId))).payload;
    }
}
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [SetAmortizationRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "setAmortization", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [CancelAmortizationRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "cancelAmortization", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [SetAmortizationHoldRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "setAmortizationHold", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ReleaseAmortizationHoldRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "releaseAmortizationHold", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAmortization", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationsCountRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAllAmortizations", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationForRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAmortizationFor", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationsForRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAmortizationsFor", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationsCountRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAmortizationsCount", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationHoldersRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAmortizationHolders", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetTotalAmortizationHoldersRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getTotalAmortizationHolders", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAmortizationPaymentAmountRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getAmortizationPaymentAmount", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetActiveAmortizationHoldHoldersRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getActiveAmortizationHoldHolders", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetTotalActiveAmortizationHoldHoldersRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getTotalActiveAmortizationHoldHolders", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetActiveAmortizationIdsRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getActiveAmortizationIds", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetTotalActiveAmortizationIdsRequest_1.default]),
    __metadata("design:returntype", Promise)
], AmortizationInPort.prototype, "getTotalActiveAmortizationIds", null);
function _mapAmortizationFor(af) {
    return {
        account: af.account,
        recordDate: new Date(af.recordDate * SecurityDate_1.ONE_THOUSAND),
        executionDate: new Date(af.executionDate * SecurityDate_1.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();
exports.default = AmortizationToken;
