"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.injectServerConfig = void 0;
const config_1 = __importDefault(require("../config"));
const index_1 = __importDefault(require("./packages/index"));
function escapeAttr(str) {
    return str.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
}
function injectServerConfig(html) {
    const theme = escapeAttr(config_1.default.values.theme);
    const themeColor = escapeAttr(config_1.default.values.themeColor);
    const stylesheets = index_1.default.getStylesheets();
    const themeLink = `<link id="theme" rel="stylesheet" href="themes/${theme}.css" data-server-theme="${theme}">`;
    const packageLinks = stylesheets
        .map((css) => `\t<link rel="stylesheet" href="packages/${escapeAttr(css)}">`)
        .join("\n");
    // Inject theme/package CSS after Vite's styles so theme overrides take effect
    const headInsert = [themeLink, packageLinks, `<style id="user-specified-css"></style>`]
        .filter(Boolean)
        .join("\n\t");
    return html
        .replace("</head>", `\t${headInsert}\n\t</head>`)
        .replace(/<!--thelounge-themecolor-->/g, themeColor)
        .replace("<!--thelounge-bodyclass-->", config_1.default.values.public ? "public" : "")
        .replace("<!--thelounge-transports-->", escapeAttr(JSON.stringify(config_1.default.values.transports)));
}
exports.injectServerConfig = injectServerConfig;
