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 { EntityPermissionType } from '@memberjunction/core';
import { Arg, Ctx, Query, Resolver } from 'type-graphql';
import { MJEntity_, MJEntityResolverBase } from '../generated/generated.js';
import { GetReadOnlyProvider } from '../util.js';
let EntityResolver = class EntityResolver extends MJEntityResolverBase {
    async EntitiesBySchemas({ providers, userPayload }, IncludeSchemas, ExcludeSchemas) {
        this.CheckUserReadPermissions('Entities', userPayload);
        const provider = GetReadOnlyProvider(providers, { allowFallbackToReadWrite: true });
        const rlsWhere = this.getRowLevelSecurityWhereClause(provider, 'Entities', userPayload, EntityPermissionType.Read, ' WHERE');
        const includeSchemaSQL = IncludeSchemas && IncludeSchemas.length > 0 ? `SchemaName IN (${IncludeSchemas.map((s) => `'${s}'`).join(',')})` : '';
        const excludeSchemaSQL = ExcludeSchemas && ExcludeSchemas.length > 0 ? `SchemaName NOT IN (${ExcludeSchemas.map((s) => `'${s}'`).join(',')})` : '';
        let schemaSQL = '';
        if (includeSchemaSQL)
            schemaSQL = includeSchemaSQL;
        if (excludeSchemaSQL) {
            if (schemaSQL)
                schemaSQL = `${schemaSQL} AND ${excludeSchemaSQL}`;
            else
                schemaSQL = excludeSchemaSQL;
        }
        let totalWhere = '';
        if (schemaSQL)
            totalWhere = `${schemaSQL}`;
        if (rlsWhere) {
            if (totalWhere)
                totalWhere = `${totalWhere} AND ${rlsWhere}`;
            else
                totalWhere = ` ${rlsWhere}`;
        }
        const rv = provider;
        const result = await rv.RunView({
            EntityName: 'Entities',
            ExtraFilter: totalWhere,
        }, userPayload.userRecord);
        if (result && result.Success) {
            return result.Results;
        }
        else {
            throw new Error(`Failed to fetch entities: ${result?.ErrorMessage || 'Unknown error'}`);
        }
    }
};
__decorate([
    Query(() => [MJEntity_]),
    __param(0, Ctx()),
    __param(1, Arg('IncludeSchemas', () => [String], { nullable: true })),
    __param(2, Arg('ExcludeSchemas', () => [String], { nullable: true })),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Array, Array]),
    __metadata("design:returntype", Promise)
], EntityResolver.prototype, "EntitiesBySchemas", null);
EntityResolver = __decorate([
    Resolver(MJEntity_)
], EntityResolver);
export { EntityResolver };
//# sourceMappingURL=EntityResolver.js.map