import { listDocuments, getDocument, createDocument, updateDocument, deleteDocument, getGlobal, updateGlobal, } from '../actions.js';
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 buildActionContext(ctx) {
    return {
        userId: ctx.userId ?? 'anonymous',
        role: ctx.role,
        db: ctx.db,
    };
}
function createFieldResolver(fieldName) {
    return (parent) => {
        if (parent.data && typeof parent.data === 'object') {
            return parent.data[fieldName] ?? null;
        }
        return parent[fieldName] ?? null;
    };
}
export function buildResolvers(config) {
    const resolvers = {
        Query: {},
        Mutation: {},
    };
    for (const [slug, collection] of Object.entries(config.collections)) {
        const typeName = toPascalCase(collection.labels.singular);
        const pluralField = toCamelCase(collection.labels.plural);
        const singularField = toCamelCase(collection.labels.singular);
        const typeResolvers = {};
        for (const fieldName of Object.keys(collection.fields)) {
            typeResolvers[fieldName] = createFieldResolver(fieldName);
        }
        resolvers[typeName] = typeResolvers;
        resolvers.Query[pluralField] = async (_parent, args, ctx) => {
            const actionCtx = buildActionContext(ctx);
            const options = {
                collection: slug,
                page: args.page,
                pageSize: args.pageSize,
                status: args.status,
            };
            return listDocuments(options, actionCtx);
        };
        resolvers.Query[singularField] = async (_parent, args, ctx) => {
            const actionCtx = buildActionContext(ctx);
            return getDocument(slug, args.id, actionCtx);
        };
        resolvers.Mutation[`create${typeName}`] = async (_parent, args, ctx) => {
            const actionCtx = buildActionContext(ctx);
            return createDocument(slug, args.input, actionCtx);
        };
        resolvers.Mutation[`update${typeName}`] = async (_parent, args, ctx) => {
            const actionCtx = buildActionContext(ctx);
            return updateDocument(slug, args.id, args.input, actionCtx);
        };
        resolvers.Mutation[`delete${typeName}`] = async (_parent, args, ctx) => {
            const actionCtx = buildActionContext(ctx);
            await deleteDocument(slug, args.id, actionCtx);
            return true;
        };
    }
    if (config.globals) {
        for (const [slug, global] of Object.entries(config.globals)) {
            const typeName = toPascalCase(global.label);
            const fieldName = toCamelCase(global.label);
            const typeResolvers = {};
            for (const fName of Object.keys(global.fields)) {
                typeResolvers[fName] = createFieldResolver(fName);
            }
            resolvers[typeName] = typeResolvers;
            resolvers.Query[fieldName] = async (_parent, _args, ctx) => {
                const actionCtx = buildActionContext(ctx);
                return getGlobal(slug, actionCtx);
            };
            resolvers.Mutation[`update${typeName}`] = async (_parent, args, ctx) => {
                const actionCtx = buildActionContext(ctx);
                return updateGlobal(slug, args.input, actionCtx);
            };
        }
    }
    return resolvers;
}
//# sourceMappingURL=resolvers.js.map