import { SourceFile } from "ts-morph" ; export function generateHelpersFile ( sourceFile ) { sourceFile . addStatements ( `
    export function transformFields(fields: Record<string, any>): Record<string, any> {
      return Object.fromEntries(
        Object.entries(fields)
          // remove __typename and others
          .filter(([key, value]) => !key.startsWith("__"))
          .map<[string, any]>(([key, value]) => {
            if (Object.keys(value).length === 0) {
              return [key, true];
            }
            return [key, transformFields(value)];
          }),
      );
    }
  ` ) ; sourceFile . addStatements ( `
    export function getPrismaFromContext(context: any) {
      const prismaClient = context.prisma;
      if (!prismaClient) {
        throw new Error("Unable to find Prisma Client in GraphQL context. Please provide it under the \`context.prisma\` key.");
      }
      return prismaClient;
    }
  ` ) ; sourceFile . addStatements ( `
    export function transformCountFieldIntoSelectRelationsCount(_count: object) {
      return {
        include: {
          _count: {
            select: {
              ...Object.fromEntries(
                Object.entries(_count).filter(([_, v]) => v != null)
              ),
            }
          },
        },
      }
    }
  ` ) ; }