// 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 { ControlListRequest, GetControlListCountRequest, GetControlListMembersRequest, GetControlListTypeRequest, } from "../../request";
import ValidatedRequest from "../../../../core/validation/ValidatedArgs";
import { AddToControlListCommand } from "../../../../app/usecase/command/security/operations/addToControlList/AddToControlListCommand";
import { RemoveFromControlListCommand } from "../../../../app/usecase/command/security/operations/removeFromControlList/RemoveFromControlListCommand";
import { IsInControlListQuery } from "../../../../app/usecase/query/account/controlList/IsInControlListQuery";
import { GetControlListCountQuery } from "../../../../app/usecase/query/security/controlList/getControlListCount/GetControlListCountQuery";
import { GetControlListMembersQuery } from "../../../../app/usecase/query/security/controlList/getControlListMembers/GetControlListMembersQuery";
import { GetControlListTypeQuery } from "../../../../app/usecase/query/security/controlList/getControlListType/GetControlListTypeQuery";
import { BaseSecurityInPort } from "../BaseSecurityInPort";
export class SecurityInPortControlList extends BaseSecurityInPort {
    async addToControlList(request) {
        const { securityId, targetId } = request;
        ValidatedRequest.handleValidation("ControlListRequest", request);
        return await this.commandBus.execute(new AddToControlListCommand(targetId, securityId));
    }
    async removeFromControlList(request) {
        const { securityId, targetId } = request;
        ValidatedRequest.handleValidation("ControlListRequest", request);
        return await this.commandBus.execute(new RemoveFromControlListCommand(targetId, securityId));
    }
    async isAccountInControlList(request) {
        ValidatedRequest.handleValidation("ControlListRequest", request);
        return (await this.queryBus.execute(new IsInControlListQuery(request.securityId, request.targetId))).payload;
    }
    async getControlListCount(request) {
        ValidatedRequest.handleValidation("GetControlListCountRequest", request);
        return (await this.queryBus.execute(new GetControlListCountQuery(request.securityId))).payload;
    }
    async getControlListMembers(request) {
        ValidatedRequest.handleValidation("GetControlListMembersRequest", request);
        const membersIds = [];
        const membersEvmAddresses = (await this.queryBus.execute(new GetControlListMembersQuery(request.securityId, request.start, request.end))).payload;
        let mirrorAccount;
        for (let i = 0; i < membersEvmAddresses.length; i++) {
            mirrorAccount = await this.mirrorNode.getAccountInfo(membersEvmAddresses[i]);
            membersIds.push(mirrorAccount.id.toString());
        }
        return membersIds;
    }
    async getControlListType(request) {
        ValidatedRequest.handleValidation("GetControlListTypeRequest", request);
        return (await this.queryBus.execute(new GetControlListTypeQuery(request.securityId))).payload;
    }
}
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ControlListRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortControlList.prototype, "addToControlList", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ControlListRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortControlList.prototype, "removeFromControlList", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ControlListRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortControlList.prototype, "isAccountInControlList", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetControlListCountRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortControlList.prototype, "getControlListCount", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetControlListMembersRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortControlList.prototype, "getControlListMembers", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetControlListTypeRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortControlList.prototype, "getControlListType", null);
