// 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 { SetDividendCommand } from "../../../app/usecase/command/dividend/set/SetDividendCommand";
import { CancelDividendCommand } from "../../../app/usecase/command/dividend/cancel/CancelDividendCommand";
import { GetDividendQuery } from "../../../app/usecase/query/dividend/getDividend/GetDividendQuery";
import { GetDividendsCountQuery } from "../../../app/usecase/query/dividend/getDividendsCount/GetDividendsCountQuery";
import { GetDividendForQuery } from "../../../app/usecase/query/dividend/getDividendFor/GetDividendForQuery";
import { GetDividendAmountForQuery } from "../../../app/usecase/query/dividend/getDividendAmountFor/GetDividendAmountForQuery";
import { GetDividendHoldersQuery } from "../../../app/usecase/query/dividend/getDividendHolders/GetDividendHoldersQuery";
import { GetTotalDividendHoldersQuery } from "../../../app/usecase/query/dividend/getTotalDividendHolders/GetTotalDividendHoldersQuery";
import Injectable from "../../../core/injectable/Injectable";
import { CommandBus } from "../../../core/command/CommandBus";
import { LogError } from "../../../core/decorator/LogErrorDecorator";
import { QueryBus } from "../../../core/query/QueryBus";
import { ONE_THOUSAND } from "../../../domain/context/shared/SecurityDate";
import ValidatedRequest from "../../../core/validation/ValidatedArgs";
import SetDividendRequest from "../request/dividend/SetDividendRequest";
import CancelDividendRequest from "../request/dividend/CancelDividendRequest";
import GetDividendForRequest from "../request/dividend/GetDividendForRequest";
import GetDividendRequest from "../request/dividend/GetDividendRequest";
import GetAllDividendsRequest from "../request/dividend/GetAllDividendsRequest";
import GetDividendHoldersRequest from "../request/dividend/GetDividendHoldersRequest";
import GetTotalDividendHoldersRequest from "../request/dividend/GetTotalDividendHoldersRequest";
class DividendInPort {
    queryBus;
    commandBus;
    constructor(queryBus = Injectable.resolve(QueryBus), commandBus = Injectable.resolve(CommandBus)) {
        this.queryBus = queryBus;
        this.commandBus = commandBus;
    }
    async setDividend(request) {
        const { amountPerUnitOfSecurity, recordTimestamp, executionTimestamp, securityId } = request;
        ValidatedRequest.handleValidation("SetDividendRequest", request);
        return await this.commandBus.execute(new SetDividendCommand(securityId, recordTimestamp, executionTimestamp, amountPerUnitOfSecurity));
    }
    async cancelDividend(request) {
        const { securityId, dividendId } = request;
        ValidatedRequest.handleValidation("CancelDividendRequest", request);
        return await this.commandBus.execute(new CancelDividendCommand(securityId, dividendId));
    }
    async getDividendFor(request) {
        ValidatedRequest.handleValidation("GetDividendForRequest", request);
        const res = await this.queryBus.execute(new GetDividendForQuery(request.targetId, request.securityId, request.dividendId));
        const dividendsFor = {
            tokenBalance: res.tokenBalance.toString(),
            decimals: res.decimals.toString(),
            isDisabled: res.isDisabled,
        };
        return dividendsFor;
    }
    async getDividendAmountFor(request) {
        ValidatedRequest.handleValidation("GetDividendForRequest", request);
        const res = await this.queryBus.execute(new GetDividendAmountForQuery(request.targetId, request.securityId, request.dividendId));
        const dividendAmountFor = {
            numerator: res.numerator,
            denominator: res.denominator,
            recordDateReached: res.recordDateReached,
        };
        return dividendAmountFor;
    }
    async getDividend(request) {
        ValidatedRequest.handleValidation("GetDividendRequest", request);
        const res = await this.queryBus.execute(new GetDividendQuery(request.securityId, request.dividendId));
        const dividend = {
            dividendId: request.dividendId,
            amountPerUnitOfSecurity: res.dividend.amountPerUnitOfSecurity.toString(),
            amountDecimals: res.dividend.amountDecimals,
            recordDate: new Date(res.dividend.recordTimeStamp * ONE_THOUSAND),
            executionDate: new Date(res.dividend.executionTimeStamp * ONE_THOUSAND),
            isDisabled: res.dividend.isDisabled,
        };
        return dividend;
    }
    async getAllDividends(request) {
        ValidatedRequest.handleValidation("GetAllDividendsRequest", request);
        const count = await this.queryBus.execute(new GetDividendsCountQuery(request.securityId));
        if (count.payload == 0)
            return [];
        const dividends = [];
        for (let i = 1; i <= count.payload; i++) {
            const res = await this.queryBus.execute(new GetDividendQuery(request.securityId, i));
            const dividend = {
                dividendId: i,
                amountPerUnitOfSecurity: res.dividend.amountPerUnitOfSecurity.toString(),
                amountDecimals: res.dividend.amountDecimals,
                recordDate: new Date(res.dividend.recordTimeStamp * ONE_THOUSAND),
                executionDate: new Date(res.dividend.executionTimeStamp * ONE_THOUSAND),
                isDisabled: res.dividend.isDisabled,
            };
            dividends.push(dividend);
        }
        return dividends;
    }
    async getDividendHolders(request) {
        const { securityId, dividendId, start, end } = request;
        ValidatedRequest.handleValidation(GetDividendHoldersRequest.name, request);
        return (await this.queryBus.execute(new GetDividendHoldersQuery(securityId, dividendId, start, end))).payload;
    }
    async getTotalDividendHolders(request) {
        const { securityId, dividendId } = request;
        ValidatedRequest.handleValidation(GetTotalDividendHoldersRequest.name, request);
        return (await this.queryBus.execute(new GetTotalDividendHoldersQuery(securityId, dividendId))).payload;
    }
}
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [SetDividendRequest]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "setDividend", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [CancelDividendRequest]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "cancelDividend", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetDividendForRequest]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "getDividendFor", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetDividendForRequest]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "getDividendAmountFor", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetDividendRequest]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "getDividend", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAllDividendsRequest]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "getAllDividends", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetDividendHoldersRequest]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "getDividendHolders", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetTotalDividendHoldersRequest]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "getTotalDividendHolders", null);
const DividendToken = new DividendInPort();
export default DividendToken;
