"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 SetDividendCommand_1 = require("../../../app/usecase/command/dividend/set/SetDividendCommand");
const CancelDividendCommand_1 = require("../../../app/usecase/command/dividend/cancel/CancelDividendCommand");
const GetDividendQuery_1 = require("../../../app/usecase/query/dividend/getDividend/GetDividendQuery");
const GetDividendsCountQuery_1 = require("../../../app/usecase/query/dividend/getDividendsCount/GetDividendsCountQuery");
const GetDividendForQuery_1 = require("../../../app/usecase/query/dividend/getDividendFor/GetDividendForQuery");
const GetDividendAmountForQuery_1 = require("../../../app/usecase/query/dividend/getDividendAmountFor/GetDividendAmountForQuery");
const GetDividendHoldersQuery_1 = require("../../../app/usecase/query/dividend/getDividendHolders/GetDividendHoldersQuery");
const GetTotalDividendHoldersQuery_1 = require("../../../app/usecase/query/dividend/getTotalDividendHolders/GetTotalDividendHoldersQuery");
const Injectable_1 = __importDefault(require("../../../core/injectable/Injectable"));
const CommandBus_1 = require("../../../core/command/CommandBus");
const LogErrorDecorator_1 = require("../../../core/decorator/LogErrorDecorator");
const QueryBus_1 = require("../../../core/query/QueryBus");
const SecurityDate_1 = require("../../../domain/context/shared/SecurityDate");
const ValidatedArgs_1 = __importDefault(require("../../../core/validation/ValidatedArgs"));
const SetDividendRequest_1 = __importDefault(require("../request/dividend/SetDividendRequest"));
const CancelDividendRequest_1 = __importDefault(require("../request/dividend/CancelDividendRequest"));
const GetDividendForRequest_1 = __importDefault(require("../request/dividend/GetDividendForRequest"));
const GetDividendRequest_1 = __importDefault(require("../request/dividend/GetDividendRequest"));
const GetAllDividendsRequest_1 = __importDefault(require("../request/dividend/GetAllDividendsRequest"));
const GetDividendHoldersRequest_1 = __importDefault(require("../request/dividend/GetDividendHoldersRequest"));
const GetTotalDividendHoldersRequest_1 = __importDefault(require("../request/dividend/GetTotalDividendHoldersRequest"));
class DividendInPort {
    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 setDividend(request) {
        const { amountPerUnitOfSecurity, recordTimestamp, executionTimestamp, securityId } = request;
        ValidatedArgs_1.default.handleValidation("SetDividendRequest", request);
        return await this.commandBus.execute(new SetDividendCommand_1.SetDividendCommand(securityId, recordTimestamp, executionTimestamp, amountPerUnitOfSecurity));
    }
    async cancelDividend(request) {
        const { securityId, dividendId } = request;
        ValidatedArgs_1.default.handleValidation("CancelDividendRequest", request);
        return await this.commandBus.execute(new CancelDividendCommand_1.CancelDividendCommand(securityId, dividendId));
    }
    async getDividendFor(request) {
        ValidatedArgs_1.default.handleValidation("GetDividendForRequest", request);
        const res = await this.queryBus.execute(new GetDividendForQuery_1.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) {
        ValidatedArgs_1.default.handleValidation("GetDividendForRequest", request);
        const res = await this.queryBus.execute(new GetDividendAmountForQuery_1.GetDividendAmountForQuery(request.targetId, request.securityId, request.dividendId));
        const dividendAmountFor = {
            numerator: res.numerator,
            denominator: res.denominator,
            recordDateReached: res.recordDateReached,
        };
        return dividendAmountFor;
    }
    async getDividend(request) {
        ValidatedArgs_1.default.handleValidation("GetDividendRequest", request);
        const res = await this.queryBus.execute(new GetDividendQuery_1.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 * SecurityDate_1.ONE_THOUSAND),
            executionDate: new Date(res.dividend.executionTimeStamp * SecurityDate_1.ONE_THOUSAND),
            isDisabled: res.dividend.isDisabled,
        };
        return dividend;
    }
    async getAllDividends(request) {
        ValidatedArgs_1.default.handleValidation("GetAllDividendsRequest", request);
        const count = await this.queryBus.execute(new GetDividendsCountQuery_1.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_1.GetDividendQuery(request.securityId, i));
            const dividend = {
                dividendId: i,
                amountPerUnitOfSecurity: res.dividend.amountPerUnitOfSecurity.toString(),
                amountDecimals: res.dividend.amountDecimals,
                recordDate: new Date(res.dividend.recordTimeStamp * SecurityDate_1.ONE_THOUSAND),
                executionDate: new Date(res.dividend.executionTimeStamp * SecurityDate_1.ONE_THOUSAND),
                isDisabled: res.dividend.isDisabled,
            };
            dividends.push(dividend);
        }
        return dividends;
    }
    async getDividendHolders(request) {
        const { securityId, dividendId, start, end } = request;
        ValidatedArgs_1.default.handleValidation(GetDividendHoldersRequest_1.default.name, request);
        return (await this.queryBus.execute(new GetDividendHoldersQuery_1.GetDividendHoldersQuery(securityId, dividendId, start, end))).payload;
    }
    async getTotalDividendHolders(request) {
        const { securityId, dividendId } = request;
        ValidatedArgs_1.default.handleValidation(GetTotalDividendHoldersRequest_1.default.name, request);
        return (await this.queryBus.execute(new GetTotalDividendHoldersQuery_1.GetTotalDividendHoldersQuery(securityId, dividendId))).payload;
    }
}
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [SetDividendRequest_1.default]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "setDividend", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [CancelDividendRequest_1.default]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "cancelDividend", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetDividendForRequest_1.default]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "getDividendFor", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetDividendForRequest_1.default]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "getDividendAmountFor", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetDividendRequest_1.default]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "getDividend", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetAllDividendsRequest_1.default]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "getAllDividends", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetDividendHoldersRequest_1.default]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "getDividendHolders", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetTotalDividendHoldersRequest_1.default]),
    __metadata("design:returntype", Promise)
], DividendInPort.prototype, "getTotalDividendHolders", null);
const DividendToken = new DividendInPort();
exports.default = DividendToken;
