"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildConfig = buildConfig;
/**
 * Crea la configuración y lista de ejercicios para un curso.
 *
 * @param bucket - Instancia de GCS Bucket donde está el curso.
 * @param courseSlug - Slug del curso a procesar.
 * @returns Promise con objeto { config, exercises } listo para usar.
 */
async function buildConfig(bucket, courseSlug) {
    const prefix = `courses/${courseSlug}/`;
    const [files] = await bucket.getFiles({ prefix });
    // 1) Leer learn.json
    const learnFile = files.find(f => f.name.endsWith("learn.json"));
    const [learnBuf] = await learnFile.download();
    const learnJson = JSON.parse(learnBuf.toString());
    // 2) Agrupar ejercicios
    const map = {};
    for (const file of files) {
        const parts = file.name.split("/");
        if (!parts.includes("exercises"))
            continue;
        const slug = parts[parts.indexOf("exercises") + 1];
        if (!map[slug]) {
            map[slug] = {
                title: slug,
                slug,
                graded: false,
                files: [],
                translations: {},
            };
        }
        const fname = parts.pop();
        const m = fname.match(/^readme(?:\.([a-z]{2}))?\.md$/i);
        if (m) {
            const lang = m[1] || "us";
            map[slug].translations[lang] = fname;
        }
        else {
            map[slug].files.push({
                name: fname,
                slug: fname,
                hidden: false,
            });
        }
    }
    const exercises = Object.values(map).map((ex, i) => (Object.assign(Object.assign({}, ex), { position: i })));
    return {
        config: Object.assign({}, learnJson),
        exercises,
    };
}
