import { readFile } from "fs/promises";
import { JSDOM } from "jsdom";

const getWebAppId = async () => {
    const indexPath = "./public/binaries/webapp-demo/index.html";

    try {
        const data = await readFile(indexPath, "utf8");
        const dom = new JSDOM(data);
        const webAppContainer = dom.window.document.querySelector(".web-app-container");

        if (webAppContainer) {
            return webAppContainer.id;
        }
        console.log("\x1b[31m%s\x1b[0m", "No web app id found");

        return undefined;
    } catch (err) {
        console.log("\x1b[31m%s\x1b[0m", (err as { message: string }).message);
        return undefined;
    }
};

export default getWebAppId;
