import { jsx as _jsx } from "react/jsx-runtime";
import { renderToStaticMarkup } from "react-dom/server";
import OpenNetworkServer from "../react/server/index.js";
import { getOrigin } from "../listen/index.js";
import { getMaybeAuthenticationState, getMaybeUser, isUnauthenticated } from "../authentication/index.js";
import { isHTMLResponse } from "../listen/index.js";
import { getConfig } from "../config/index.js";
import { getView } from "./views.js";
export function errorHandler(error, request, response) {
    if (!isHTMLResponse(request)) {
        return response.send(error);
    }
    const { pathname } = new URL(request.url, getOrigin());
    const isFragment = pathname.endsWith("/fragment");
    const anonymous = isUnauthenticated();
    const user = getMaybeUser();
    const { DEFAULT_TIMEZONE = "Pacific/Auckland" } = process.env;
    const origin = getOrigin();
    const html = renderToStaticMarkup(_jsx(OpenNetworkServer, { view: getView("/error"), isFragment: isFragment, isUnauthenticated: anonymous, url: new URL(request.url, origin).toString(), origin: origin, error: error, organisations: [], partners: [], user: user, timezone: DEFAULT_TIMEZONE, config: getConfig(), authenticationState: getMaybeAuthenticationState() }));
    response.header("Cache-Control", "No-Store");
    response.header("Content-Type", "text/html; charset=utf-8");
    response.header("Cross-Origin-Embedder-Policy", "unsafe-none");
    response.send(html);
}
//# sourceMappingURL=error.js.map