import { readFile } from "node:fs/promises";
import { extname, join } from "path";
import { isTruthy } from "../utils.js";
const SENTINEL_BASE_PATH = "/__CROSS_SEED_BASE_PATH__";
const STATIC_ROOT = join(import.meta.dirname, "..", "..", "dist", "webui");
const INDEX_HTML_PATH = join(STATIC_ROOT, "index.html");
const UTF8 = { encoding: "utf-8" };
const MIME_TYPES = {
    ".html": "text/html",
    ".css": "text/css",
    ".js": "application/javascript",
    ".mjs": "application/javascript",
    ".json": "application/json",
    ".png": "image/png",
    ".jpg": "image/jpeg",
    ".jpeg": "image/jpeg",
    ".svg": "image/svg+xml",
    ".ico": "image/x-icon",
};
const TEXT_EXT = [".html", ".css", ".js", ".mjs", ".json"];
function injectBasePath(content, basePath) {
    if (typeof content === "string") {
        return content.replaceAll(SENTINEL_BASE_PATH, basePath);
    }
    return content;
}
function getContentType(ext) {
    return MIME_TYPES[ext] || "application/octet-stream";
}
export async function staticFrontendPlugin(app, { basePath }) {
    // Custom static file handler that replaces sentinel values
    app.get("*", async (request, reply) => {
        const requestPath = request.url.split("?")[0];
        const basePathRelativeUrl = requestPath.startsWith(basePath)
            ? requestPath.slice(basePath.length)
            : "MALFORMED_REQUEST_URL"; // should never happen because this route is only registered under basePath
        const desiredFilePath = join(STATIC_ROOT, ...basePathRelativeUrl.split("/").filter(isTruthy));
        let fileContents;
        let fileExtension = extname(desiredFilePath);
        try {
            const isText = TEXT_EXT.includes(fileExtension);
            fileContents = await readFile(desiredFilePath, isText ? UTF8 : null);
        }
        catch (e) {
            if (e.code == "ENOENT" ||
                e.code == "EISDIR") {
                if (!(request.headers.accept?.includes("text/html") ?? false)) {
                    return reply.code(404).type("text/plain").send("Not Found");
                }
                fileContents = await readFile(INDEX_HTML_PATH, UTF8);
                fileExtension = ".html";
            }
            else {
                throw e;
            }
        }
        return reply
            .type(getContentType(fileExtension))
            .send(injectBasePath(fileContents, basePath));
    });
}
//# sourceMappingURL=staticFrontendPlugin.js.map