import { join, resolve } from "node:path";
import { InternalError } from "../../util/internalError.js";
import { getSettings } from "../../util/settings.js";
import { Renderer } from "./renderer.js";
import { Site, } from "./site.js";
export class MarkdownSite extends Site {
    #pages = new Map();
    buildPagePath(...[slug, { appendIndex = false } = {}]) {
        const settings = getSettings();
        if (appendIndex) {
            slug += "/index";
        }
        return resolve(join(settings.output.pageOutDir, `${slug}.md`));
    }
    hasPage(...[path]) {
        return this.#pages.has(path);
    }
    createPage(...[path]) {
        const renderer = this.getRenderer({
            currentPagePath: path,
        });
        this.#pages.set(path, renderer);
        return renderer;
    }
    render() {
        const pages = {};
        for (const [path, renderer] of this.#pages) {
            pages[path] = renderer.render();
        }
        return pages;
    }
}
export const rendererLines = Symbol();
export class MarkdownRenderer extends Renderer {
    #isFinalized = false;
    [rendererLines] = [];
    escapeText(...[text, { escape }]) {
        switch (escape) {
            case "markdown":
                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 "html":
                return text.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
            case "none":
                return text;
        }
    }
    createHeading(...[level, text, { escape = "markdown", id } = {},]) {
        let line = `${`#`.repeat(level)} ${this.escapeText(text, { escape })}`;
        if (id) {
            line += ` \\{#${id}\\}`;
        }
        return line;
    }
    appendHeading(...args) {
        this[rendererLines].push(this.createHeading(...args));
    }
    createText(...[text, { escape = "mdx" } = {}]) {
        return this.escapeText(text, { escape });
    }
    appendText(...args) {
        this[rendererLines].push(this.createText(...args));
    }
    createCode(...[text, options]) {
        if (options?.variant === "raw") {
            if (options.style === "inline") {
                return `<code>${text}</code>`;
            }
            return `<pre>
<code>
${text}\n</code>\n</pre>`;
        }
        else {
            if (options?.style === "inline") {
                return `\`${text}\``;
            }
            return `\`\`\`${options?.language ?? ""}\n${text}\n\`\`\``;
        }
    }
    appendCode(...args) {
        this[rendererLines].push(this.createCode(...args));
    }
    createList(...[items, { escape = "markdown" } = {}]) {
        return items
            .map((item) => "- " + this.escapeText(item, { escape }))
            .join("\n");
    }
    appendList(...args) {
        this[rendererLines].push(this.createList(...args));
    }
    createExpandableSectionStart(...[title, { isOpenOnLoad = false, escape = "markdown" } = {},]) {
        return `<details ${isOpenOnLoad ? "open" : ""}>\n\n<summary>${this.escapeText(title, { escape })}</summary>`;
    }
    appendExpandableSectionStart(...args) {
        this[rendererLines].push(this.createExpandableSectionStart(...args));
    }
    createExpandableSectionEnd() {
        return "</details>";
    }
    appendExpandableSectionEnd() {
        this[rendererLines].push(this.createExpandableSectionEnd());
    }
    render() {
        if (this.#isFinalized) {
            throw new InternalError("Renderer has already been finalized");
        }
        const data = this[rendererLines].join("\n\n");
        this.#isFinalized = true;
        return data;
    }
}
//# sourceMappingURL=markdown.js.map