import { readdir } from "fs/promises";
import { resolve, join } from "path";

/**
 * Looks in one or more relative/absolute directories for a .zip file.
 * Returns the **full path** to the first .zip file found, or null if none.
 */
export async function checkBuildUpdate(dirs: string[]): Promise<string | null> {
    for (const inputDir of dirs) {
        const dir = resolve(inputDir); // resolves "../../" relative to cwd
        try {
            const files = await readdir(dir);
            const zipFile = files.find((file) =>
                file.toLowerCase().endsWith(".zip")
            );
            if (zipFile) {
                //return join(dir, zipFile);
                return zipFile;
            }
        } catch (err) {
            // Ignore if directory doesn't exist, allow search to continue
            if ((err as NodeJS.ErrnoException).code !== "ENOENT") {
                throw err;
            }
        }
    }
    return null;
}
