"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 });
exports.SecurityInPortControlList = void 0;
const LogErrorDecorator_1 = require("../../../../core/decorator/LogErrorDecorator");
const request_1 = require("../../request");
const ValidatedArgs_1 = __importDefault(require("../../../../core/validation/ValidatedArgs"));
const AddToControlListCommand_1 = require("../../../../app/usecase/command/security/operations/addToControlList/AddToControlListCommand");
const RemoveFromControlListCommand_1 = require("../../../../app/usecase/command/security/operations/removeFromControlList/RemoveFromControlListCommand");
const IsInControlListQuery_1 = require("../../../../app/usecase/query/account/controlList/IsInControlListQuery");
const GetControlListCountQuery_1 = require("../../../../app/usecase/query/security/controlList/getControlListCount/GetControlListCountQuery");
const GetControlListMembersQuery_1 = require("../../../../app/usecase/query/security/controlList/getControlListMembers/GetControlListMembersQuery");
const GetControlListTypeQuery_1 = require("../../../../app/usecase/query/security/controlList/getControlListType/GetControlListTypeQuery");
const BaseSecurityInPort_1 = require("../BaseSecurityInPort");
class SecurityInPortControlList extends BaseSecurityInPort_1.BaseSecurityInPort {
    async addToControlList(request) {
        const { securityId, targetId } = request;
        ValidatedArgs_1.default.handleValidation("ControlListRequest", request);
        return await this.commandBus.execute(new AddToControlListCommand_1.AddToControlListCommand(targetId, securityId));
    }
    async removeFromControlList(request) {
        const { securityId, targetId } = request;
        ValidatedArgs_1.default.handleValidation("ControlListRequest", request);
        return await this.commandBus.execute(new RemoveFromControlListCommand_1.RemoveFromControlListCommand(targetId, securityId));
    }
    async isAccountInControlList(request) {
        ValidatedArgs_1.default.handleValidation("ControlListRequest", request);
        return (await this.queryBus.execute(new IsInControlListQuery_1.IsInControlListQuery(request.securityId, request.targetId))).payload;
    }
    async getControlListCount(request) {
        ValidatedArgs_1.default.handleValidation("GetControlListCountRequest", request);
        return (await this.queryBus.execute(new GetControlListCountQuery_1.GetControlListCountQuery(request.securityId))).payload;
    }
    async getControlListMembers(request) {
        ValidatedArgs_1.default.handleValidation("GetControlListMembersRequest", request);
        const membersIds = [];
        const membersEvmAddresses = (await this.queryBus.execute(new GetControlListMembersQuery_1.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) {
        ValidatedArgs_1.default.handleValidation("GetControlListTypeRequest", request);
        return (await this.queryBus.execute(new GetControlListTypeQuery_1.GetControlListTypeQuery(request.securityId))).payload;
    }
}
exports.SecurityInPortControlList = SecurityInPortControlList;
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.ControlListRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortControlList.prototype, "addToControlList", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.ControlListRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortControlList.prototype, "removeFromControlList", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.ControlListRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortControlList.prototype, "isAccountInControlList", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.GetControlListCountRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortControlList.prototype, "getControlListCount", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.GetControlListMembersRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortControlList.prototype, "getControlListMembers", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.GetControlListTypeRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortControlList.prototype, "getControlListType", null);
