function escapeString(value) {
    return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
}
function objectKey(name) {
    return /^[A-Za-z_][A-Za-z0-9_]*$/.test(name) ? name : JSON.stringify(name);
}
export function generateMetadataTypesFile(catalog) {
    const entityTypes = catalog.entities.map((e) => e.typeName);
    const fieldsLines = catalog.entities.map((e) => {
        const names = e.fields
            .map((f) => f.name)
            .filter((n) => Boolean(n));
        const literals = names.map((n) => `"${escapeString(n)}"`).join(", ");
        return `  ${objectKey(e.typeName)}: [${literals}] as const`;
    });
    const relationsLines = catalog.entities.map((e) => {
        const rels = e.relations
            .filter((r) => r.name && r.relatedTypeName)
            .map((r) => `{ name: "${escapeString(r.name)}", relatedTypeName: "${escapeString(r.relatedTypeName)}" }`);
        return `  ${objectKey(e.typeName)}: [${rels.join(", ")}] as const`;
    });
    return `// Auto-generated by npm run scrape:metadata. Do not edit manually.
/* eslint-disable */

export const ENTITY_TYPES = [
${entityTypes.map((t) => `  "${escapeString(t)}",`).join("\n")}
] as const;

export type EntityTypeName = (typeof ENTITY_TYPES)[number];

export const FIELDS_BY_ENTITY = {
${fieldsLines.join(",\n")},
} as const;

export type EntityFieldName<T extends EntityTypeName> =
  (typeof FIELDS_BY_ENTITY)[T][number];

export const RELATIONS_BY_ENTITY = {
${relationsLines.join(",\n")},
} as const;

export type EntityRelationEntry<T extends EntityTypeName> =
  (typeof RELATIONS_BY_ENTITY)[T][number];

export type EntityRelationName<T extends EntityTypeName> =
  EntityRelationEntry<T>["name"];

export function entityFields<T extends EntityTypeName>(
  typeName: T,
): readonly EntityFieldName<T>[] {
  return FIELDS_BY_ENTITY[typeName];
}

export function entityRelations<T extends EntityTypeName>(
  typeName: T,
): readonly EntityRelationEntry<T>[] {
  return RELATIONS_BY_ENTITY[typeName];
}
`;
}
//# sourceMappingURL=generate-types.js.map