import crypto from "node:crypto";
import fs from "node:fs/promises";
import path from "pathe";
/**
 * Lists SQL files from the directory, sorted by filename.
 * @param directory Directory containing .sql files
 */
export async function listSqlFiles(directory) {
    const files = await Array.fromAsync(fs.glob("**/*.sql", {
        cwd: directory,
    }));
    const sortedFiles = files.sort((a, b) => {
        const aNum = getPrefix(a);
        const bNum = getPrefix(b);
        if (aNum !== null && bNum !== null)
            return aNum - bNum;
        if (aNum !== null)
            return -1;
        if (bNum !== null)
            return 1;
        return a.localeCompare(b);
    });
    return Promise.all(sortedFiles.map((id) => readSqlFile(directory, id)));
}
export async function readSqlFile(directory, name) {
    const sql = await fs.readFile(path.resolve(directory, name), "utf-8");
    const hash = crypto.createHash("sha256").update(sql).digest("hex");
    const file = { id: name, sql, hash };
    // Make the sql property non-enumerable so it's not included in state. This prevents state store errors caused by large sql files.
    Object.defineProperty(file, "sql", { enumerable: false });
    return file;
}
const getPrefix = (name) => {
    const prefix = name.split("_")[0];
    const num = Number.parseInt(prefix, 10);
    return Number.isNaN(num) ? null : num;
};
//# sourceMappingURL=d1-sql-file.js.map