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 { CompositeKey } from '@memberjunction/core';
import { Arg, Ctx, Field, InputType, ObjectType, Query, Resolver } from 'type-graphql';
import { CompositeKeyInputType, CompositeKeyOutputType } from '../generic/KeyInputOutputTypes.js';
import { GetReadOnlyProvider } from '../util.js';
let EntityRecordNameInput = class EntityRecordNameInput {
    EntityName;
    CompositeKey;
};
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], EntityRecordNameInput.prototype, "EntityName", void 0);
__decorate([
    Field(() => CompositeKeyInputType),
    __metadata("design:type", CompositeKey)
], EntityRecordNameInput.prototype, "CompositeKey", void 0);
EntityRecordNameInput = __decorate([
    InputType()
], EntityRecordNameInput);
export { EntityRecordNameInput };
let EntityRecordNameResult = class EntityRecordNameResult {
    Success;
    Status;
    CompositeKey;
    EntityName;
    RecordName;
};
__decorate([
    Field(() => Boolean),
    __metadata("design:type", Boolean)
], EntityRecordNameResult.prototype, "Success", void 0);
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], EntityRecordNameResult.prototype, "Status", void 0);
__decorate([
    Field(() => CompositeKeyOutputType),
    __metadata("design:type", CompositeKey)
], EntityRecordNameResult.prototype, "CompositeKey", void 0);
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], EntityRecordNameResult.prototype, "EntityName", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], EntityRecordNameResult.prototype, "RecordName", void 0);
EntityRecordNameResult = __decorate([
    ObjectType()
], EntityRecordNameResult);
export { EntityRecordNameResult };
let EntityRecordNameResolver = class EntityRecordNameResolver {
    async GetEntityRecordName(EntityName, primaryKey, { providers, userPayload }) {
        const md = GetReadOnlyProvider(providers, { allowFallbackToReadWrite: true });
        return await this.InnerGetEntityRecordName(md, EntityName, primaryKey);
    }
    async GetEntityRecordNames(info, { providers }) {
        const result = [];
        const md = GetReadOnlyProvider(providers, { allowFallbackToReadWrite: true });
        for (const i of info) {
            result.push(await this.InnerGetEntityRecordName(md, i.EntityName, i.CompositeKey));
        }
        return result;
    }
    async InnerGetEntityRecordName(md, EntityName, primaryKey) {
        const pk = new CompositeKey(primaryKey.KeyValuePairs);
        const e = md.Entities.find((e) => e.Name === EntityName);
        if (e) {
            const recordName = await md.GetEntityRecordName(e.Name, pk);
            if (recordName)
                return { Success: true, Status: 'OK', CompositeKey: pk, RecordName: recordName, EntityName };
            else
                return {
                    Success: false,
                    Status: `Name for record, or record ${pk.ToString()} itself not found, could be an access issue if user doesn't have Row Level Access (RLS) if RLS is enabled for this entity`,
                    CompositeKey: pk,
                    EntityName,
                };
        }
        else
            return { Success: false, Status: `Entity ${EntityName} not found`, CompositeKey: pk, EntityName };
    }
};
__decorate([
    Query(() => EntityRecordNameResult),
    __param(0, Arg('EntityName', () => String)),
    __param(1, Arg('CompositeKey', () => CompositeKeyInputType)),
    __param(2, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, CompositeKey, Object]),
    __metadata("design:returntype", Promise)
], EntityRecordNameResolver.prototype, "GetEntityRecordName", null);
__decorate([
    Query(() => [EntityRecordNameResult]),
    __param(0, Arg('info', () => [EntityRecordNameInput])),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Array, Object]),
    __metadata("design:returntype", Promise)
], EntityRecordNameResolver.prototype, "GetEntityRecordNames", null);
EntityRecordNameResolver = __decorate([
    Resolver(EntityRecordNameResult)
], EntityRecordNameResolver);
export { EntityRecordNameResolver };
export default EntityRecordNameResolver;
//# sourceMappingURL=EntityRecordNameResolver.js.map