import { getErrorOverlayScript } from "./client-error-overlay.js";
const isLocalServer = process.env.SUPERBLOCKS_SERVER_ENV === "local";
const isHostSameAsOrigin = (host, origin) => {
    try {
        const originUrl = new URL(origin);
        return originUrl.host === host;
    }
    catch (_error) {
        return false;
    }
};
const isPageUrl = (req) => {
    const url = new URL(req.url ?? "", "http://localhost");
    const headers = req.headers;
    if (url?.pathname?.startsWith("/@") ||
        url?.pathname?.startsWith("/node_modules/") ||
        (headers.origin &&
            headers.host &&
            !isHostSameAsOrigin(headers.host, headers.origin))) {
        return false;
    }
    return (headers["sec-fetch-mode"] === "navigate" &&
        (headers["sec-fetch-dest"] === "iframe" ||
            headers["sec-fetch-dest"] === "document"));
};
/**
 * This middleware is used so we can serve a custom index.html without
 * having the user specify their own file inside of their code-mode apps.
 * This gives us some extra control over the index.html file, especially
 * when it comes to building bundles for production/deployment.
 */
export function indexHtmlMiddleware(server, features) {
    return async (req, res, next) => {
        const url = new URL(req.url ?? "", "http://localhost");
        const module = await server.moduleGraph
            .getModuleByUrl(url.pathname)
            .catch(() => null);
        if (!module && isPageUrl(req)) {
            const entryPoint = process.env.SUPERBLOCKS_APP_ENTRY_POINT ?? "root.tsx";
            const transformedHtml = await server.transformIndexHtml(url.pathname, getIndexHtml(features ?? {}, entryPoint));
            if (features?.enableSessionRecording && !isLocalServer) {
                res.setHeader("Content-Security-Policy", `frame-ancestors 'self' https://*.superblocks.com/ https://*.superblocks.dev/ https://*.superblockshq.com/`);
            }
            res.setHeader("Content-Type", "text/html");
            res.write(transformedHtml);
            res.end();
            return;
        }
        next();
    };
}
function getIndexHtml(features, entryPoint) {
    return /*html*/ `
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Code Mode</title>
    <script>
      window._SB_VIEW_MODE = "dev";
      ${features?.enableSessionRecording ? `window._SB_ENABLE_SESSION_RECORDING = true;` : ""}
    </script>
    <!-- Error overlay must load before root.tsx to catch its errors -->
    <script type="module">
      ${getErrorOverlayScript()}
    </script>
    <script src="/${entryPoint}" type="module"></script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Geist+Mono:wght@100..900&family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap" rel="stylesheet">
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>
`;
}
//# sourceMappingURL=injected-index.js.map