import { MapperKind, getDirective, mapSchema } from '@graphql-tools/utils';
import { defaultFieldResolver } from 'graphql';
import { AuthorizationError, Directive } from 'type-graphql';
const DIRECTIVE_NAME = 'Public';
export function Public() {
    return (targetOrPrototype, propertyKey, descriptor) => Directive(`@${DIRECTIVE_NAME}`)(targetOrPrototype, propertyKey, descriptor);
}
export const publicDirective = {
    typeDefs: `directive @${DIRECTIVE_NAME} on FIELD_DEFINITION`,
    transformer: (schema) => {
        const fieldMapper = (fieldConfig) => {
            const directive = getDirective(schema, fieldConfig, DIRECTIVE_NAME)?.[0];
            if (directive) {
                return fieldConfig;
            }
            else {
                const { resolve = defaultFieldResolver } = fieldConfig;
                const directiveResolver = async (source, args, context, info) => {
                    if (!context?.userPayload?.userRecord?.IsActive) {
                        throw new AuthorizationError();
                    }
                    return await resolve(source, args, context, info);
                };
                return { ...fieldConfig, resolve: directiveResolver };
            }
        };
        return mapSchema(schema, { [MapperKind.OBJECT_FIELD]: fieldMapper });
    },
};
//# sourceMappingURL=Public.js.map