"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyDir = copyDir;
exports.downloadS3Folder = downloadS3Folder;
exports.getFilesList = getFilesList;
exports.getCourseMetadata = getCourseMetadata;
const path = require("path");
const fs = require("fs");
const mkdirp = require("mkdirp");
// Utility function to copy directory recursively
function copyDir(src, dest) {
    if (!fs.existsSync(dest))
        mkdirp.sync(dest);
    for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
        const srcPath = path.join(src, entry.name);
        const destPath = path.join(dest, entry.name);
        if (entry.isDirectory()) {
            copyDir(srcPath, destPath);
        }
        else {
            fs.copyFileSync(srcPath, destPath);
        }
    }
}
// Download all files from a GCS Bucket prefix to a local folder
async function downloadS3Folder(bucket, prefix, localPath) {
    const [files] = await bucket.getFiles({ prefix });
    for (const file of files) {
        const relPath = file.name.replace(prefix, "");
        if (!relPath)
            continue;
        const outPath = path.join(localPath, relPath);
        mkdirp.sync(path.dirname(outPath));
        // esling-disable-next-line
        const [buf] = await file.download();
        fs.writeFileSync(outPath, buf);
    }
}
// Generate file list for manifest, given a folder
function getFilesList(folder, base) {
    let list = [];
    const files = fs.readdirSync(folder, { withFileTypes: true });
    for (const file of files) {
        const filePath = path.join(folder, file.name);
        if (file.isDirectory()) {
            // esling-disable-next-line
            list = list.concat(getFilesList(filePath, path.join(base, file.name)));
        }
        else {
            list.push(`<file href="${path.join(base, file.name).replace(/\\/g, "/")}" />`);
        }
    }
    return list;
}
// Read course metadata from learn.json
async function getCourseMetadata(bucket, courseSlug) {
    const [learnBuf] = await bucket
        .file(`courses/${courseSlug}/learn.json`)
        .download();
    return JSON.parse(learnBuf.toString());
}
