"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.exportToScorm = exportToScorm;
const path = require("path");
const fs = require("fs");
const archiver = require("archiver");
const mkdirp = require("mkdirp");
const rimraf = require("rimraf");
const uuid_1 = require("uuid");
const shared_1 = require("./shared");
async function exportToScorm(options) {
    var _a, _b;
    const { courseSlug, bucket, outDir } = options;
    // 1. Create temporary folder
    const tmpName = (0, uuid_1.v4)();
    const scormOutDir = path.join(outDir, tmpName);
    rimraf.sync(scormOutDir);
    mkdirp.sync(scormOutDir);
    // 2. Copy SCORM template
    const scormTpl = path.join(__dirname, "../templates/scorm");
    (0, shared_1.copyDir)(scormTpl, scormOutDir);
    // Paths
    const appDir = path.resolve(__dirname, "../../ui/_app");
    const configDir = path.join(scormOutDir, "config");
    // Check if _app exists and has content
    if (fs.existsSync(appDir) && fs.readdirSync(appDir).length > 0) {
        // Copy app.css and app.js to config/
        for (const file of ["app.css", "app.js"]) {
            const src = path.join(appDir, file);
            const dest = path.join(configDir, file);
            if (fs.existsSync(src)) {
                fs.copyFileSync(src, dest);
            }
        }
        // Copy logo-192.png and logo-512.png to SCORM build root
        for (const file of ["logo-192.png", "logo-512.png"]) {
            const src = path.join(appDir, file);
            const dest = path.join(scormOutDir, file);
            if (fs.existsSync(src)) {
                fs.copyFileSync(src, dest);
            }
        }
    }
    // 3. Download exercises and .learn from bucket
    await (0, shared_1.downloadS3Folder)(bucket, `courses/${courseSlug}/exercises/`, path.join(scormOutDir, "exercises"));
    await (0, shared_1.downloadS3Folder)(bucket, `courses/${courseSlug}/.learn/`, path.join(scormOutDir, ".learn"));
    // 4. Read learn.json for course info
    const learnJson = await (0, shared_1.getCourseMetadata)(bucket, courseSlug);
    // 5. Replace imsmanifest.xml
    const manifestPath = path.join(scormOutDir, "imsmanifest.xml");
    let manifest = fs.readFileSync(manifestPath, "utf-8");
    manifest = manifest.replace(/{{organization_name}}/g, "FourGeeksAcademy");
    manifest = manifest.replace(/{{course_title}}/g, ((_a = learnJson.title) === null || _a === void 0 ? void 0 : _a.en) || learnJson.title || courseSlug);
    // Generate exercises list
    const exercisesList = (0, shared_1.getFilesList)(path.join(scormOutDir, "exercises"), "exercises").join("\n      ");
    manifest = manifest.replace(/{{exercises_list}}/g, exercisesList);
    // Generate images list (optional, if you have assets)
    let imagesList = "";
    const assetsDir = path.join(scormOutDir, ".learn", "assets");
    if (fs.existsSync(assetsDir)) {
        imagesList = (0, shared_1.getFilesList)(assetsDir, ".learn/assets").join("\n      ");
    }
    manifest = manifest.replace(/{{images_list}}/g, imagesList);
    fs.writeFileSync(manifestPath, manifest, "utf-8");
    // 6. Replace title in config/index.html
    const configIndexPath = path.join(scormOutDir, "config", "index.html");
    let indexHtml = fs.readFileSync(configIndexPath, "utf-8");
    indexHtml = indexHtml.replace(/{{title}}/g, ((_b = learnJson.title) === null || _b === void 0 ? void 0 : _b.en) || learnJson.title || courseSlug);
    fs.writeFileSync(configIndexPath, indexHtml, "utf-8");
    // 7. Compress as ZIP
    const zipPath = `${scormOutDir}.zip`;
    await new Promise((resolve, reject) => {
        const output = fs.createWriteStream(zipPath);
        const archive = archiver("zip", { zlib: { level: 9 } });
        output.on("close", resolve);
        archive.on("error", reject);
        archive.pipe(output);
        archive.directory(scormOutDir, false);
        archive.finalize();
    });
    // Clean up temporary directory
    rimraf.sync(scormOutDir);
    return zipPath;
}
