import { join, resolve } from "node:path";
import { getSettings } from "../../util/settings.js";
export class MarkdownSite {
    #pages = new Map();
    #Renderer;
    constructor(Renderer) {
        this.#Renderer = Renderer;
    }
    buildPagePath(slug) {
        const settings = getSettings();
        return resolve(join(settings.output.pageOutDir, `${slug}.md`));
    }
    hasPage(path) {
        return this.#pages.has(path);
    }
    createPage(path) {
        // Reserve the name, since we sometimes check to see if pages already exist
        const renderer = new this.#Renderer({
            currentPagePath: path,
        });
        this.#pages.set(path, renderer);
        return renderer;
    }
    createEmbedPage(_) {
        throw new Error("Not supported");
    }
    createRawPage(path, contents) {
        const renderer = new this.#Renderer({
            currentPagePath: path,
        });
        renderer.appendRaw(contents);
        this.#pages.set(path, renderer);
    }
    finalize() {
        const pages = {};
        for (const [path, renderer] of this.#pages) {
            pages[path] = renderer.finalize();
        }
        return pages;
    }
}
export const rendererLines = Symbol();
export class MarkdownRenderer {
    #isFinalized = false;
    [rendererLines] = [];
    // TODO: don't escape if they're already escaped
    escapeText(text, { escape }) {
        switch (escape) {
            case "all":
                return (text
                    .replaceAll("\\", "\\\\")
                    .replaceAll("`", "\\`")
                    .replaceAll("*", "\\*")
                    .replaceAll("_", "\\_")
                    .replaceAll("{", "\\{")
                    .replaceAll("}", "\\}")
                    .replaceAll("[", "\\[")
                    .replaceAll("]", "\\]")
                    .replaceAll("<", "\\<")
                    .replaceAll(">", "\\>")
                    .replaceAll("(", "\\(")
                    .replaceAll(")", "\\)")
                    .replaceAll("#", "\\#")
                    .replaceAll("+", "\\+")
                    // .replace("-", "\\-")
                    // .replace(".", "\\.")
                    .replaceAll("!", "\\!")
                    .replaceAll("|", "\\|"));
            case "mdx":
                return text.replaceAll("{", "\\{").replaceAll("}", "\\}");
            case "none":
                return text;
        }
    }
    insertFrontMatter(_) {
        throw new Error("Not supported");
    }
    appendHeading(level, text, { escape = "all" } = {}) {
        this.#checkFinalized();
        this[rendererLines].push(`#`.repeat(level) + " " + this.escapeText(text, { escape }));
    }
    appendParagraph(text, { escape = "mdx" } = {}) {
        this.#checkFinalized();
        this[rendererLines].push(this.escapeText(text, { escape }));
    }
    appendCode(text) {
        this.#checkFinalized();
        this[rendererLines].push(`\`\`\`\n${text}\n\`\`\``);
    }
    appendList(items, { escape = "all" } = {}) {
        this.#checkFinalized();
        this[rendererLines].push(items.map((item) => "- " + this.escapeText(item, { escape })).join("\n"));
    }
    appendRaw(text) {
        this.#checkFinalized();
        this[rendererLines].push(text);
    }
    beginExpandableSection(title, { isOpenOnLoad = false, escape = "all", }) {
        this.#checkFinalized();
        this[rendererLines].push(`<details ${isOpenOnLoad ? "open" : ""}>`);
        this[rendererLines].push(`<summary>${this.escapeText(title, { escape })}</summary>`);
    }
    endExpandableSection() {
        this.#checkFinalized();
        this[rendererLines].push("</details>");
    }
    appendSidebarLink(_) {
        throw new Error("Not supported");
    }
    appendTryItNow(_) {
        throw new Error("Not supported");
    }
    finalize() {
        const data = this[rendererLines].join("\n\n");
        this.#isFinalized = true;
        return data;
    }
    #checkFinalized() {
        if (this.#isFinalized) {
            throw new Error("Renderer has already been finalized");
        }
    }
}
//# sourceMappingURL=renderer.js.map