"use strict";
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ListOfRolesSelectionProvider = void 0;
const common_1 = require("@nestjs/common");
const selection_provider_decorator_1 = require("../../decorators/selection-provider.decorator");
const basic_filters_dto_1 = require("../../dtos/basic-filters.dto");
const role_metadata_service_1 = require("../role-metadata.service");
const DEFAULT_LIMIT = 100;
let ListOfRolesSelectionProvider = class ListOfRolesSelectionProvider {
    constructor(roleMetadataService) {
        this.roleMetadataService = roleMetadataService;
    }
    name() {
        return 'ListOfRolesSelectionProvider';
    }
    help() {
        return '# Simple Role selection provider (search + include/exclude)';
    }
    async value(optionValue, ctxt) {
        const basicFilterQuery = new basic_filters_dto_1.BasicFilterDto(1, 0);
        basicFilterQuery.filters = {
            name: { $eq: optionValue }
        };
        const roles = await this.roleMetadataService.find(basicFilterQuery);
        if (!roles.records || roles.records.length === 0) {
            throw new common_1.BadRequestException(`Invalid role name: ${optionValue}`);
        }
        const role = roles.records[0];
        return {
            label: role.name,
            value: role.name
        };
    }
    async values(query, ctxt) {
        const basicFilterQuery = new basic_filters_dto_1.BasicFilterDto(DEFAULT_LIMIT, 0);
        basicFilterQuery.filters = ctxt.filters || {};
        const roles = await this.roleMetadataService.find(basicFilterQuery);
        return roles.records.map(role => ({
            label: role.name,
            value: role.name
        }));
    }
};
exports.ListOfRolesSelectionProvider = ListOfRolesSelectionProvider;
exports.ListOfRolesSelectionProvider = ListOfRolesSelectionProvider = __decorate([
    (0, selection_provider_decorator_1.SelectionProvider)(),
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [role_metadata_service_1.RoleMetadataService])
], ListOfRolesSelectionProvider);
//# sourceMappingURL=list-of-roles-selectionproviders.service.js.map