// 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);
};
import { LogError } from "../../../core/decorator/LogErrorDecorator";
import Injectable from "../../../core/injectable/Injectable";
import { QueryBus } from "../../../core/query/QueryBus";
import ValidatedRequest from "../../../core/validation/ValidatedArgs";
import { CommandBus } from "../../../core/command/CommandBus";
import { CastRateStatus } from "../../../domain/context/bond/RateStatus";
import { ONE_THOUSAND } from "../../../domain/context/shared/SecurityDate";
import { CancelCouponCommand } from "../../../app/usecase/command/bond/coupon/cancel/CancelCouponCommand";
import { SetCouponCommand } from "../../../app/usecase/command/bond/coupon/set/SetCouponCommand";
import { GetCouponQuery } from "../../../app/usecase/query/bond/coupons/getCoupon/GetCouponQuery";
import { GetCouponAmountForQuery } from "../../../app/usecase/query/bond/coupons/getCouponAmountFor/GetCouponAmountForQuery";
import { GetCouponCountQuery } from "../../../app/usecase/query/bond/coupons/getCouponCount/GetCouponCountQuery";
import { GetCouponForQuery } from "../../../app/usecase/query/bond/coupons/getCouponFor/GetCouponForQuery";
import { GetCouponsForQuery } from "../../../app/usecase/query/bond/coupons/getCouponsFor/GetCouponsForQuery";
import { GetCouponFromOrderedListAtQuery } from "../../../app/usecase/query/bond/coupons/getCouponFromOrderedListAt/GetCouponFromOrderedListAtQuery";
import { GetCouponHoldersQuery } from "../../../app/usecase/query/bond/coupons/getCouponHolders/GetCouponHoldersQuery";
import { GetCouponsOrderedListQuery } from "../../../app/usecase/query/bond/coupons/getCouponsOrderedList/GetCouponsOrderedListQuery";
import { GetCouponsOrderedListTotalQuery } from "../../../app/usecase/query/bond/coupons/getCouponsOrderedListTotal/GetCouponsOrderedListTotalQuery";
import { GetTotalCouponHoldersQuery } from "../../../app/usecase/query/bond/coupons/getTotalCouponHolders/GetTotalCouponHoldersQuery";
import { GetCouponHoldersRequest, GetTotalCouponHoldersRequest } from "../request";
import CancelCouponRequest from "../request/bond/CancelCouponRequest";
import GetAllCouponsRequest from "../request/bond/GetAllCouponsRequest";
import GetCouponForRequest from "../request/bond/GetCouponForRequest";
import GetCouponsForRequest from "../request/bond/GetCouponsForRequest";
import GetCouponFromOrderedListAtRequest from "../request/bond/GetCouponFromOrderedListAtRequest";
import GetCouponRequest from "../request/bond/GetCouponRequest";
import GetCouponsOrderedListRequest from "../request/bond/GetCouponsOrderedListRequest";
import GetCouponsOrderedListTotalRequest from "../request/bond/GetCouponsOrderedListTotalRequest";
import SetCouponRequest from "../request/bond/SetCouponRequest";
class CouponInPort {
    queryBus;
    commandBus;
    constructor(queryBus = Injectable.resolve(QueryBus), commandBus = Injectable.resolve(CommandBus)) {
        this.queryBus = queryBus;
        this.commandBus = commandBus;
    }
    async setCoupon(request) {
        const { rate, recordTimestamp, executionTimestamp, securityId, startTimestamp, endTimestamp, fixingTimestamp, rateStatus, } = request;
        ValidatedRequest.handleValidation("SetCouponRequest", request);
        return await this.commandBus.execute(new SetCouponCommand(securityId, recordTimestamp, executionTimestamp, rate, startTimestamp, endTimestamp, fixingTimestamp, CastRateStatus.fromNumber(rateStatus)));
    }
    async cancelCoupon(request) {
        const { securityId, couponId } = request;
        ValidatedRequest.handleValidation("CancelCouponRequest", request);
        return await this.commandBus.execute(new CancelCouponCommand(securityId, couponId));
    }
    async getCouponFor(request) {
        ValidatedRequest.handleValidation("GetCouponForRequest", request);
        const res = await this.queryBus.execute(new 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 * ONE_THOUSAND),
                executionDate: new Date(res.couponFor.coupon.executionTimeStamp * ONE_THOUSAND),
                rate: res.couponFor.coupon.rate.toString(),
                rateDecimals: res.couponFor.coupon.rateDecimals,
                startDate: new Date(res.couponFor.coupon.startTimeStamp * ONE_THOUSAND),
                endDate: new Date(res.couponFor.coupon.endTimeStamp * ONE_THOUSAND),
                fixingDate: new Date(res.couponFor.coupon.fixingTimeStamp * ONE_THOUSAND),
                rateStatus: 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) {
        ValidatedRequest.handleValidation("GetCouponsForRequest", request);
        const res = await this.queryBus.execute(new 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 * ONE_THOUSAND),
                executionDate: new Date(couponFor.coupon.executionTimeStamp * ONE_THOUSAND),
                rate: couponFor.coupon.rate.toString(),
                rateDecimals: couponFor.coupon.rateDecimals,
                startDate: new Date(couponFor.coupon.startTimeStamp * ONE_THOUSAND),
                endDate: new Date(couponFor.coupon.endTimeStamp * ONE_THOUSAND),
                fixingDate: new Date(couponFor.coupon.fixingTimeStamp * ONE_THOUSAND),
                rateStatus: 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) {
        ValidatedRequest.handleValidation("GetCouponForRequest", request);
        const res = await this.queryBus.execute(new GetCouponAmountForQuery(request.targetId, request.securityId, request.couponId));
        const couponAmountFor = {
            numerator: res.numerator,
            denominator: res.denominator,
            recordDateReached: res.recordDateReached,
        };
        return couponAmountFor;
    }
    async getCoupon(request) {
        ValidatedRequest.handleValidation("GetCouponRequest", request);
        const res = await this.queryBus.execute(new GetCouponQuery(request.securityId, request.couponId));
        const coupon = {
            couponId: request.couponId,
            recordDate: new Date(res.coupon.recordTimeStamp * ONE_THOUSAND),
            executionDate: new Date(res.coupon.executionTimeStamp * ONE_THOUSAND),
            rate: res.coupon.rate.toString(),
            rateDecimals: res.coupon.rateDecimals,
            startDate: new Date(res.coupon.startTimeStamp * ONE_THOUSAND),
            endDate: new Date(res.coupon.endTimeStamp * ONE_THOUSAND),
            fixingDate: new Date(res.coupon.fixingTimeStamp * ONE_THOUSAND),
            rateStatus: CastRateStatus.toNumber(res.coupon.rateStatus),
            isDisabled: res.coupon.isDisabled,
        };
        return coupon;
    }
    async getAllCoupons(request) {
        ValidatedRequest.handleValidation("GetAllCouponsRequest", request);
        const count = await this.queryBus.execute(new GetCouponCountQuery(request.securityId));
        if (count.payload == 0)
            return [];
        const coupons = [];
        for (let i = 1; i <= count.payload; i++) {
            const couponRequest = new GetCouponRequest({
                securityId: request.securityId,
                couponId: i,
            });
            const coupon = await this.getCoupon(couponRequest);
            coupons.push(coupon);
        }
        return coupons;
    }
    async getCouponsOrderedList(request) {
        ValidatedRequest.handleValidation("GetCouponsOrderedListRequest", request);
        const { securityId, pageIndex, pageLength } = request;
        const result = await this.queryBus.execute(new GetCouponsOrderedListQuery(securityId, pageIndex, pageLength));
        return result.payload;
    }
    async getCouponsOrderedListTotal(request) {
        ValidatedRequest.handleValidation("GetCouponsOrderedListTotalRequest", request);
        const { securityId } = request;
        const result = await this.queryBus.execute(new GetCouponsOrderedListTotalQuery(securityId));
        return result.payload;
    }
    async getCouponHolders(request) {
        const { securityId, couponId, start, end } = request;
        ValidatedRequest.handleValidation(GetCouponHoldersRequest.name, request);
        return (await this.queryBus.execute(new GetCouponHoldersQuery(securityId, couponId, start, end))).payload;
    }
    async getTotalCouponHolders(request) {
        const { securityId, couponId } = request;
        ValidatedRequest.handleValidation(GetTotalCouponHoldersRequest.name, request);
        return (await this.queryBus.execute(new GetTotalCouponHoldersQuery(securityId, couponId))).payload;
    }
    async getCouponFromOrderedListAt(request) {
        const { securityId, pos } = request;
        ValidatedRequest.handleValidation(GetCouponFromOrderedListAtRequest.name, request);
        return (await this.queryBus.execute(new GetCouponFromOrderedListAtQuery(securityId, pos))).couponId;
    }
}
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [SetCouponRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "setCoupon", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [CancelCouponRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "cancelCoupon", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponForRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponFor", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponsForRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponsFor", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponForRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponAmountFor", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCoupon", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAllCouponsRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getAllCoupons", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponsOrderedListRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponsOrderedList", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponsOrderedListTotalRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponsOrderedListTotal", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponHoldersRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponHolders", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetTotalCouponHoldersRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getTotalCouponHolders", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetCouponFromOrderedListAtRequest]),
    __metadata("design:returntype", Promise)
], CouponInPort.prototype, "getCouponFromOrderedListAt", null);
const CouponToken = new CouponInPort();
export default CouponToken;
