// 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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import NetworkService from "../../../app/service/network/NetworkService";
import SecurityService from "../../../app/service/security/SecurityService";
import { GrantRoleCommand } from "../../../app/usecase/command/security/roles/grantRole/GrantRoleCommand";
import { RevokeRoleCommand } from "../../../app/usecase/command/security/roles/revokeRole/RevokeRoleCommand";
import { GetRoleCountForQuery } from "../../../app/usecase/query/security/roles/getRoleCountFor/GetRoleCountForQuery";
import { GetRoleMemberCountQuery } from "../../../app/usecase/query/security/roles/getRoleMemberCount/GetRoleMemberCountQuery";
import { GetRoleMembersQuery } from "../../../app/usecase/query/security/roles/getRoleMembers/GetRoleMembersQuery";
import { GetRolesForQuery } from "../../../app/usecase/query/security/roles/getRolesFor/GetRolesForQuery";
import { HasRoleQuery } from "../../../app/usecase/query/security/roles/hasRole/HasRoleQuery";
import Injectable from "../../../core/injectable/Injectable";
import { CommandBus } from "../../../core/command/CommandBus";
import { lazyInject } from "../../../core/decorator/LazyInjectDecorator";
import { LogError } from "../../../core/decorator/LogErrorDecorator";
import { QueryBus } from "../../../core/query/QueryBus";
import { MirrorNodeAdapter } from "../../out/mirror/MirrorNodeAdapter";
import ValidatedRequest from "../../../core/validation/ValidatedArgs";
import GetRoleCountForRequest from "../request/security/roles/GetRoleCountForRequest";
import GetRoleMemberCountRequest from "../request/security/roles/GetRoleMemberCountRequest";
import GetRoleMembersRequest from "../request/security/roles/GetRoleMembersRequest";
import GetRolesForRequest from "../request/security/roles/GetRolesForRequest";
import RoleRequest from "../request/security/roles/RoleRequest";
import ApplyRolesRequest from "../request/security/roles/ApplyRolesRequest";
import { ApplyRolesCommand } from "../../../app/usecase/command/security/roles/applyRoles/ApplyRolesCommand";
let RoleInPort = class RoleInPort {
    queryBus;
    commandBus;
    securityService;
    networkService;
    mirrorNode;
    constructor(queryBus = Injectable.resolve(QueryBus), commandBus = Injectable.resolve(CommandBus), securityService = Injectable.resolve(SecurityService), networkService = Injectable.resolve(NetworkService), mirrorNode = Injectable.resolve(MirrorNodeAdapter)) {
        this.queryBus = queryBus;
        this.commandBus = commandBus;
        this.securityService = securityService;
        this.networkService = networkService;
        this.mirrorNode = mirrorNode;
    }
    async hasRole(request) {
        const { securityId, targetId, role } = request;
        ValidatedRequest.handleValidation("RoleRequest", request);
        return (await this.queryBus.execute(new HasRoleQuery(role, targetId, securityId))).payload;
    }
    async grantRole(request) {
        const { securityId, targetId, role } = request;
        ValidatedRequest.handleValidation("RoleRequest", request);
        return await this.commandBus.execute(new GrantRoleCommand(role, targetId, securityId));
    }
    async revokeRole(request) {
        const { securityId, targetId, role } = request;
        ValidatedRequest.handleValidation("RoleRequest", request);
        return await this.commandBus.execute(new RevokeRoleCommand(role, targetId, securityId));
    }
    async getRoleCountFor(request) {
        ValidatedRequest.handleValidation("GetRoleCountForRequest", request);
        return (await this.queryBus.execute(new GetRoleCountForQuery(request.targetId, request.securityId))).payload;
    }
    async getRolesFor(request) {
        ValidatedRequest.handleValidation("GetRolesForRequest", request);
        return (await this.queryBus.execute(new GetRolesForQuery(request.targetId, request.securityId, request.start, request.end))).payload;
    }
    async getRoleMemberCount(request) {
        ValidatedRequest.handleValidation("GetRoleMemberCountRequest", request);
        return (await this.queryBus.execute(new GetRoleMemberCountQuery(request.role, request.securityId))).payload;
    }
    async getRoleMembers(request) {
        ValidatedRequest.handleValidation("GetRoleMembersRequest", request);
        const membersIds = [];
        const membersEvmAddresses = (await this.queryBus.execute(new GetRoleMembersQuery(request.role, 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 applyRoles(request) {
        const { securityId, targetId, roles, actives } = request;
        ValidatedRequest.handleValidation("ApplyRolesRequest", request);
        return await this.commandBus.execute(new ApplyRolesCommand(roles, actives, targetId, securityId));
    }
};
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [RoleRequest]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "hasRole", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [RoleRequest]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "grantRole", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [RoleRequest]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "revokeRole", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetRoleCountForRequest]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "getRoleCountFor", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetRolesForRequest]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "getRolesFor", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetRoleMemberCountRequest]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "getRoleMemberCount", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetRoleMembersRequest]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "getRoleMembers", null);
__decorate([
    LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ApplyRolesRequest]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "applyRoles", null);
RoleInPort = __decorate([
    __param(4, lazyInject(MirrorNodeAdapter)),
    __metadata("design:paramtypes", [QueryBus,
        CommandBus,
        SecurityService,
        NetworkService,
        MirrorNodeAdapter])
], RoleInPort);
const Role = new RoleInPort();
export default Role;
