function toPascalCase(str) {
    return str
        .replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : ''))
        .replace(/^(.)/, (_, c) => c.toUpperCase());
}
function toCamelCase(str) {
    const pascal = toPascalCase(str);
    return pascal.charAt(0).toLowerCase() + pascal.slice(1);
}
function mapFieldToGraphQLType(field) {
    switch (field.type) {
        case 'text':
        case 'slug':
        case 'color':
        case 'url':
        case 'email':
        case 'media':
            return 'String';
        case 'richText':
        case 'json':
            return 'JSON';
        case 'number':
            return 'Float';
        case 'boolean':
            return 'Boolean';
        case 'date':
            return 'DateTime';
        case 'select':
            return field.multiple ? '[String!]' : 'String';
        case 'relationship':
            return field.hasMany ? '[ID!]' : 'ID';
        case 'array':
        case 'blocks':
            return '[JSON!]';
        default:
            return 'String';
    }
}
function buildFieldLines(fields, forInput) {
    const lines = [];
    for (const [name, field] of Object.entries(fields)) {
        const gqlType = mapFieldToGraphQLType(field);
        const required = !forInput && field.required ? '!' : '';
        lines.push(`  ${name}: ${gqlType}${required}`);
    }
    return lines;
}
function buildCollectionTypes(collection) {
    const typeName = toPascalCase(collection.labels.singular);
    const fieldLines = buildFieldLines(collection.fields, false);
    const inputFieldLines = buildFieldLines(collection.fields, true);
    const parts = [];
    parts.push(`type ${typeName} {\n  id: ID!\n  collection: String!\n  status: DocumentStatus\n  createdAt: DateTime\n  updatedAt: DateTime\n${fieldLines.join('\n')}\n}`);
    parts.push(`type ${typeName}List {\n  docs: [${typeName}!]!\n  total: Int!\n  page: Int!\n  pageSize: Int!\n}`);
    parts.push(`input ${typeName}Input {\n${inputFieldLines.join('\n')}\n}`);
    return parts.join('\n\n');
}
function buildGlobalTypes(global) {
    const typeName = toPascalCase(global.label);
    const fieldLines = buildFieldLines(global.fields, false);
    const inputFieldLines = buildFieldLines(global.fields, true);
    const parts = [];
    parts.push(`type ${typeName} {\n  id: ID!\n${fieldLines.join('\n')}\n}`);
    parts.push(`input ${typeName}Input {\n${inputFieldLines.join('\n')}\n}`);
    return parts.join('\n\n');
}
export function buildSchemaString(config) {
    const parts = [];
    parts.push('scalar JSON');
    parts.push('scalar DateTime');
    parts.push('enum DocumentStatus {\n  DRAFT\n  PUBLISHED\n  ARCHIVED\n  SCHEDULED\n}');
    const queryFields = [];
    const mutationFields = [];
    for (const [, collection] of Object.entries(config.collections)) {
        parts.push(buildCollectionTypes(collection));
        const typeName = toPascalCase(collection.labels.singular);
        const pluralField = toCamelCase(collection.labels.plural);
        const singularField = toCamelCase(collection.labels.singular);
        queryFields.push(`  ${pluralField}(page: Int, pageSize: Int, status: String): ${typeName}List!`);
        queryFields.push(`  ${singularField}(id: ID!): ${typeName}`);
        mutationFields.push(`  create${typeName}(input: ${typeName}Input!): ${typeName}!`);
        mutationFields.push(`  update${typeName}(id: ID!, input: ${typeName}Input!): ${typeName}!`);
        mutationFields.push(`  delete${typeName}(id: ID!): Boolean!`);
    }
    if (config.globals) {
        for (const [, global] of Object.entries(config.globals)) {
            parts.push(buildGlobalTypes(global));
            const typeName = toPascalCase(global.label);
            const fieldName = toCamelCase(global.label);
            queryFields.push(`  ${fieldName}: ${typeName}`);
            mutationFields.push(`  update${typeName}(input: ${typeName}Input!): ${typeName}!`);
        }
    }
    if (queryFields.length === 0) {
        queryFields.push('  _health: Boolean');
    }
    parts.push(`type Query {\n${queryFields.join('\n')}\n}`);
    if (mutationFields.length > 0) {
        parts.push(`type Mutation {\n${mutationFields.join('\n')}\n}`);
    }
    return parts.join('\n\n');
}
//# sourceMappingURL=schema-builder.js.map