"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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const NetworkService_1 = __importDefault(require("../../../app/service/network/NetworkService"));
const SecurityService_1 = __importDefault(require("../../../app/service/security/SecurityService"));
const GrantRoleCommand_1 = require("../../../app/usecase/command/security/roles/grantRole/GrantRoleCommand");
const RevokeRoleCommand_1 = require("../../../app/usecase/command/security/roles/revokeRole/RevokeRoleCommand");
const GetRoleCountForQuery_1 = require("../../../app/usecase/query/security/roles/getRoleCountFor/GetRoleCountForQuery");
const GetRoleMemberCountQuery_1 = require("../../../app/usecase/query/security/roles/getRoleMemberCount/GetRoleMemberCountQuery");
const GetRoleMembersQuery_1 = require("../../../app/usecase/query/security/roles/getRoleMembers/GetRoleMembersQuery");
const GetRolesForQuery_1 = require("../../../app/usecase/query/security/roles/getRolesFor/GetRolesForQuery");
const HasRoleQuery_1 = require("../../../app/usecase/query/security/roles/hasRole/HasRoleQuery");
const Injectable_1 = __importDefault(require("../../../core/injectable/Injectable"));
const CommandBus_1 = require("../../../core/command/CommandBus");
const LazyInjectDecorator_1 = require("../../../core/decorator/LazyInjectDecorator");
const LogErrorDecorator_1 = require("../../../core/decorator/LogErrorDecorator");
const QueryBus_1 = require("../../../core/query/QueryBus");
const MirrorNodeAdapter_1 = require("../../out/mirror/MirrorNodeAdapter");
const ValidatedArgs_1 = __importDefault(require("../../../core/validation/ValidatedArgs"));
const GetRoleCountForRequest_1 = __importDefault(require("../request/security/roles/GetRoleCountForRequest"));
const GetRoleMemberCountRequest_1 = __importDefault(require("../request/security/roles/GetRoleMemberCountRequest"));
const GetRoleMembersRequest_1 = __importDefault(require("../request/security/roles/GetRoleMembersRequest"));
const GetRolesForRequest_1 = __importDefault(require("../request/security/roles/GetRolesForRequest"));
const RoleRequest_1 = __importDefault(require("../request/security/roles/RoleRequest"));
const ApplyRolesRequest_1 = __importDefault(require("../request/security/roles/ApplyRolesRequest"));
const ApplyRolesCommand_1 = require("../../../app/usecase/command/security/roles/applyRoles/ApplyRolesCommand");
let RoleInPort = class RoleInPort {
    queryBus;
    commandBus;
    securityService;
    networkService;
    mirrorNode;
    constructor(queryBus = Injectable_1.default.resolve(QueryBus_1.QueryBus), commandBus = Injectable_1.default.resolve(CommandBus_1.CommandBus), securityService = Injectable_1.default.resolve(SecurityService_1.default), networkService = Injectable_1.default.resolve(NetworkService_1.default), mirrorNode = Injectable_1.default.resolve(MirrorNodeAdapter_1.MirrorNodeAdapter)) {
        this.queryBus = queryBus;
        this.commandBus = commandBus;
        this.securityService = securityService;
        this.networkService = networkService;
        this.mirrorNode = mirrorNode;
    }
    async hasRole(request) {
        const { securityId, targetId, role } = request;
        ValidatedArgs_1.default.handleValidation("RoleRequest", request);
        return (await this.queryBus.execute(new HasRoleQuery_1.HasRoleQuery(role, targetId, securityId))).payload;
    }
    async grantRole(request) {
        const { securityId, targetId, role } = request;
        ValidatedArgs_1.default.handleValidation("RoleRequest", request);
        return await this.commandBus.execute(new GrantRoleCommand_1.GrantRoleCommand(role, targetId, securityId));
    }
    async revokeRole(request) {
        const { securityId, targetId, role } = request;
        ValidatedArgs_1.default.handleValidation("RoleRequest", request);
        return await this.commandBus.execute(new RevokeRoleCommand_1.RevokeRoleCommand(role, targetId, securityId));
    }
    async getRoleCountFor(request) {
        ValidatedArgs_1.default.handleValidation("GetRoleCountForRequest", request);
        return (await this.queryBus.execute(new GetRoleCountForQuery_1.GetRoleCountForQuery(request.targetId, request.securityId))).payload;
    }
    async getRolesFor(request) {
        ValidatedArgs_1.default.handleValidation("GetRolesForRequest", request);
        return (await this.queryBus.execute(new GetRolesForQuery_1.GetRolesForQuery(request.targetId, request.securityId, request.start, request.end))).payload;
    }
    async getRoleMemberCount(request) {
        ValidatedArgs_1.default.handleValidation("GetRoleMemberCountRequest", request);
        return (await this.queryBus.execute(new GetRoleMemberCountQuery_1.GetRoleMemberCountQuery(request.role, request.securityId))).payload;
    }
    async getRoleMembers(request) {
        ValidatedArgs_1.default.handleValidation("GetRoleMembersRequest", request);
        const membersIds = [];
        const membersEvmAddresses = (await this.queryBus.execute(new GetRoleMembersQuery_1.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;
        ValidatedArgs_1.default.handleValidation("ApplyRolesRequest", request);
        return await this.commandBus.execute(new ApplyRolesCommand_1.ApplyRolesCommand(roles, actives, targetId, securityId));
    }
};
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [RoleRequest_1.default]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "hasRole", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [RoleRequest_1.default]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "grantRole", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [RoleRequest_1.default]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "revokeRole", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetRoleCountForRequest_1.default]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "getRoleCountFor", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetRolesForRequest_1.default]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "getRolesFor", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetRoleMemberCountRequest_1.default]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "getRoleMemberCount", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [GetRoleMembersRequest_1.default]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "getRoleMembers", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ApplyRolesRequest_1.default]),
    __metadata("design:returntype", Promise)
], RoleInPort.prototype, "applyRoles", null);
RoleInPort = __decorate([
    __param(4, (0, LazyInjectDecorator_1.lazyInject)(MirrorNodeAdapter_1.MirrorNodeAdapter)),
    __metadata("design:paramtypes", [QueryBus_1.QueryBus,
        CommandBus_1.CommandBus,
        SecurityService_1.default,
        NetworkService_1.default,
        MirrorNodeAdapter_1.MirrorNodeAdapter])
], RoleInPort);
const Role = new RoleInPort();
exports.default = Role;
