"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 });
const LogErrorDecorator_1 = require("../../../core/decorator/LogErrorDecorator");
const Injectable_1 = __importDefault(require("../../../core/injectable/Injectable"));
const QueryBus_1 = require("../../../core/query/QueryBus");
const ValidatedArgs_1 = __importDefault(require("../../../core/validation/ValidatedArgs"));
const CommandBus_1 = require("../../../core/command/CommandBus");
const RateStatus_1 = require("../../../domain/context/bond/RateStatus");
const SecurityDate_1 = require("../../../domain/context/shared/SecurityDate");
const CancelCouponCommand_1 = require("../../../app/usecase/command/bond/coupon/cancel/CancelCouponCommand");
const SetCouponCommand_1 = require("../../../app/usecase/command/bond/coupon/set/SetCouponCommand");
const GetCouponQuery_1 = require("../../../app/usecase/query/bond/coupons/getCoupon/GetCouponQuery");
const GetCouponAmountForQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponAmountFor/GetCouponAmountForQuery");
const GetCouponCountQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponCount/GetCouponCountQuery");
const GetCouponForQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponFor/GetCouponForQuery");
const GetCouponsForQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponsFor/GetCouponsForQuery");
const GetCouponFromOrderedListAtQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponFromOrderedListAt/GetCouponFromOrderedListAtQuery");
const GetCouponHoldersQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponHolders/GetCouponHoldersQuery");
const GetCouponsOrderedListQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponsOrderedList/GetCouponsOrderedListQuery");
const GetCouponsOrderedListTotalQuery_1 = require("../../../app/usecase/query/bond/coupons/getCouponsOrderedListTotal/GetCouponsOrderedListTotalQuery");
const GetTotalCouponHoldersQuery_1 = require("../../../app/usecase/query/bond/coupons/getTotalCouponHolders/GetTotalCouponHoldersQuery");
const request_1 = require("../request");
const CancelCouponRequest_1 = __importDefault(require("../request/bond/CancelCouponRequest"));
const GetAllCouponsRequest_1 = __importDefault(require("../request/bond/GetAllCouponsRequest"));
const GetCouponForRequest_1 = __importDefault(require("../request/bond/GetCouponForRequest"));
const GetCouponsForRequest_1 = __importDefault(require("../request/bond/GetCouponsForRequest"));
const GetCouponFromOrderedListAtRequest_1 = __importDefault(require("../request/bond/GetCouponFromOrderedListAtRequest"));
const GetCouponRequest_1 = __importDefault(require("../request/bond/GetCouponRequest"));
const GetCouponsOrderedListRequest_1 = __importDefault(require("../request/bond/GetCouponsOrderedListRequest"));
const GetCouponsOrderedListTotalRequest_1 = __importDefault(require("../request/bond/GetCouponsOrderedListTotalRequest"));
const SetCouponRequest_1 = __importDefault(require("../request/bond/SetCouponRequest"));
class CouponInPort {
    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 setCoupon(request) {
        const { rate, recordTimestamp, executionTimestamp, securityId, startTimestamp, endTimestamp, fixingTimestamp, rateStatus, } = request;
        ValidatedArgs_1.default.handleValidation("SetCouponRequest", request);
        return await this.commandBus.execute(new SetCouponCommand_1.SetCouponCommand(securityId, recordTimestamp, executionTimestamp, rate, startTimestamp, endTimestamp, fixingTimestamp, RateStatus_1.CastRateStatus.fromNumber(rateStatus)));
    }
    async cancelCoupon(request) {
        const { securityId, couponId } = request;
        ValidatedArgs_1.default.handleValidation("CancelCouponRequest", request);
        return await this.commandBus.execute(new CancelCouponCommand_1.CancelCouponCommand(securityId, couponId));
    }
    async getCouponFor(request) {
        ValidatedArgs_1.default.handleValidation("GetCouponForRequest", request);
        const res = await this.queryBus.execute(new GetCouponForQuery_1.GetCouponForQuery(request.targetId, request.securityId, request.couponId));
        const couponFor = {
            tokenBalance: res.couponFor.tokenBalance.toString(),
            nominalValue: res.couponFor.nominalValue.toString(),
            decimals: res.couponFor.decimals.toString(),
            recordDateReached: res.couponFor.recordDateReached,
            coupon: {
                recordDate: new Date(res.couponFor.coupon.recordTimeStamp * SecurityDate_1.ONE_THOUSAND),
                executionDate: new Date(res.couponFor.coupon.executionTimeStamp * SecurityDate_1.ONE_THOUSAND),
                rate: res.couponFor.coupon.rate.toString(),
                rateDecimals: res.couponFor.coupon.rateDecimals,
                startDate: new Date(res.couponFor.coupon.startTimeStamp * SecurityDate_1.ONE_THOUSAND),
                endDate: new Date(res.couponFor.coupon.endTimeStamp * SecurityDate_1.ONE_THOUSAND),
                fixingDate: new Date(res.couponFor.coupon.fixingTimeStamp * SecurityDate_1.ONE_THOUSAND),
                rateStatus: RateStatus_1.CastRateStatus.toNumber(res.couponFor.coupon.rateStatus),
            },
            couponAmount: {
                numerator: res.couponFor.couponAmount.numerator,
                denominator: res.couponFor.couponAmount.denominator,
                recordDateReached: res.couponFor.couponAmount.recordDateReached,
            },
            isDisabled: res.couponFor.isDisabled,
        };
        return couponFor;
    }
    async getCouponsFor(request) {
        ValidatedArgs_1.default.handleValidation("GetCouponsForRequest", request);
        const res = await this.queryBus.execute(new GetCouponsForQuery_1.GetCouponsForQuery(request.securityId, request.couponId, request.pageIndex, request.pageLength));
        const coupons = res.coupons.map((couponFor) => ({
            tokenBalance: couponFor.tokenBalance.toString(),
            nominalValue: couponFor.nominalValue.toString(),
            decimals: couponFor.decimals.toString(),
            recordDateReached: couponFor.recordDateReached,
            coupon: {
                recordDate: new Date(couponFor.coupon.recordTimeStamp * SecurityDate_1.ONE_THOUSAND),
                executionDate: new Date(couponFor.coupon.executionTimeStamp * SecurityDate_1.ONE_THOUSAND),
                rate: couponFor.coupon.rate.toString(),
                rateDecimals: couponFor.coupon.rateDecimals,
                startDate: new Date(couponFor.coupon.startTimeStamp * SecurityDate_1.ONE_THOUSAND),
                endDate: new Date(couponFor.coupon.endTimeStamp * SecurityDate_1.ONE_THOUSAND),
                fixingDate: new Date(couponFor.coupon.fixingTimeStamp * SecurityDate_1.ONE_THOUSAND),
                rateStatus: RateStatus_1.CastRateStatus.toNumber(couponFor.coupon.rateStatus),
            },
            couponAmount: {
                numerator: couponFor.couponAmount.numerator,
                denominator: couponFor.couponAmount.denominator,
                recordDateReached: couponFor.couponAmount.recordDateReached,
            },
            isDisabled: couponFor.isDisabled,
        }));
        return { coupons, accounts: res.accounts };
    }
    async getCouponAmountFor(request) {
        ValidatedArgs_1.default.handleValidation("GetCouponForRequest", request);
        const res = await this.queryBus.execute(new GetCouponAmountForQuery_1.GetCouponAmountForQuery(request.targetId, request.securityId, request.couponId));
        const couponAmountFor = {
            numerator: res.numerator,
            denominator: res.denominator,
            recordDateReached: res.recordDateReached,
        };
        return couponAmountFor;
    }
    async getCoupon(request) {
        ValidatedArgs_1.default.handleValidation("GetCouponRequest", request);
        const res = await this.queryBus.execute(new GetCouponQuery_1.GetCouponQuery(request.securityId, request.couponId));
        const coupon = {
            couponId: request.couponId,
            recordDate: new Date(res.coupon.recordTimeStamp * SecurityDate_1.ONE_THOUSAND),
            executionDate: new Date(res.coupon.executionTimeStamp * SecurityDate_1.ONE_THOUSAND),
            rate: res.coupon.rate.toString(),
            rateDecimals: res.coupon.rateDecimals,
            startDate: new Date(res.coupon.startTimeStamp * SecurityDate_1.ONE_THOUSAND),
            endDate: new Date(res.coupon.endTimeStamp * SecurityDate_1.ONE_THOUSAND),
            fixingDate: new Date(res.coupon.fixingTimeStamp * SecurityDate_1.ONE_THOUSAND),
            rateStatus: RateStatus_1.CastRateStatus.toNumber(res.coupon.rateStatus),
            isDisabled: res.coupon.isDisabled,
        };
        return coupon;
    }
    async getAllCoupons(request) {
        ValidatedArgs_1.default.handleValidation("GetAllCouponsRequest", request);
        const count = await this.queryBus.execute(new GetCouponCountQuery_1.GetCouponCountQuery(request.securityId));
        if (count.payload == 0)
            return [];
        const coupons = [];
        for (let i = 1; i <= count.payload; i++) {
            const couponRequest = new GetCouponRequest_1.default({
                securityId: request.securityId,
                couponId: i,
            });
            const coupon = await this.getCoupon(couponRequest);
            coupons.push(coupon);
        }
        return coupons;
    }
    async getCouponsOrderedList(request) {
        ValidatedArgs_1.default.handleValidation("GetCouponsOrderedListRequest", request);
        const { securityId, pageIndex, pageLength } = request;
        const result = await this.queryBus.execute(new GetCouponsOrderedListQuery_1.GetCouponsOrderedListQuery(securityId, pageIndex, pageLength));
        return result.payload;
    }
    async getCouponsOrderedListTotal(request) {
        ValidatedArgs_1.default.handleValidation("GetCouponsOrderedListTotalRequest", request);
        const { securityId } = request;
        const result = await this.queryBus.execute(new GetCouponsOrderedListTotalQuery_1.GetCouponsOrderedListTotalQuery(securityId));
        return result.payload;
    }
    async getCouponHolders(request) {
        const { securityId, couponId, start, end } = request;
        ValidatedArgs_1.default.handleValidation(request_1.GetCouponHoldersRequest.name, request);
        return (await this.queryBus.execute(new GetCouponHoldersQuery_1.GetCouponHoldersQuery(securityId, couponId, start, end))).payload;
    }
    async getTotalCouponHolders(request) {
        const { securityId, couponId } = request;
        ValidatedArgs_1.default.handleValidation(request_1.GetTotalCouponHoldersRequest.name, request);
        return (await this.queryBus.execute(new GetTotalCouponHoldersQuery_1.GetTotalCouponHoldersQuery(securityId, couponId))).payload;
    }
    async getCouponFromOrderedListAt(request) {
        const { securityId, pos } = request;
        ValidatedArgs_1.default.handleValidation(GetCouponFromOrderedListAtRequest_1.default.name, request);
        return (await this.queryBus.execute(new GetCouponFromOrderedListAtQuery_1.GetCouponFromOrderedListAtQuery(securityId, pos))).couponId;
    }
}
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [SetCouponRequest_1.default]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "setCoupon", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [CancelCouponRequest_1.default]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "cancelCoupon", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponForRequest_1.default]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponFor", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponsForRequest_1.default]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponsFor", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponForRequest_1.default]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponAmountFor", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponRequest_1.default]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCoupon", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAllCouponsRequest_1.default]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getAllCoupons", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponsOrderedListRequest_1.default]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponsOrderedList", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponsOrderedListTotalRequest_1.default]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponsOrderedListTotal", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.GetCouponHoldersRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponHolders", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.GetTotalCouponHoldersRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getTotalCouponHolders", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponFromOrderedListAtRequest_1.default]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponFromOrderedListAt", null);
const CouponToken = new CouponInPort();
exports.default = CouponToken;
