import { join, resolve } from "node:path";
import { getSettings } from "../util/settings.js";
import { rendererLines } from "./markdown.js";
import { MdxRenderer, MdxSite } from "./mdx.js";
import { getEmbedPath, getEmbedSymbol } from "./util.js";
export class DocusaurusSite extends MdxSite {
    buildPagePath(slug) {
        const settings = getSettings();
        return resolve(join(settings.output.pageOutDir, `${slug}.mdx`));
    }
    render() {
        const settings = getSettings();
        this.createPage(join(settings.output.pageOutDir, "_category_.json")).appendText(JSON.stringify({
            position: 2,
            label: "API Reference",
            collapsible: true,
            collapsed: false,
        }, null, "  "), { escape: "none" });
        this.createPage(join(settings.output.pageOutDir, "tag", "_category_.json")).appendText(JSON.stringify({
            position: 3,
            label: "Operations",
            collapsible: true,
            collapsed: false,
        }, null, "  "), { escape: "none" });
        if (settings.display.showSchemasInNav) {
            this.createPage(join(settings.output.pageOutDir, "schema", "_category_.json")).appendText(JSON.stringify({
                position: 4,
                label: "Schemas",
                collapsible: true,
                collapsed: true,
            }, null, "  "), { escape: "none" });
        }
        return super.render();
    }
    getRenderer(options) {
        return new DocusaurusRenderer(options, this);
    }
}
class DocusaurusRenderer extends MdxRenderer {
    #frontMatter;
    #includeSidebar = false;
    #currentPagePath;
    #site;
    constructor({ currentPagePath }, site) {
        super();
        this.#currentPagePath = currentPagePath;
        this.#site = site;
    }
    insertFrontMatter({ sidebarPosition, sidebarLabel, }) {
        this.#frontMatter = `---
sidebar_position: ${sidebarPosition}
sidebar_label: ${this.escapeText(sidebarLabel, { escape: "mdx" })}
---`;
    }
    appendCodeBlock(text, options) {
        if (options?.variant === "raw") {
            this.appendText(`<pre style={{
  backgroundColor: "var(--ifm-code-background)",
  border: "0.1rem solid rgba(0, 0, 0, 0.1)",
  borderRadius: "var(--ifm-code-border-radius)",
  fontFamily: "var(--ifm-font-family-monospace)",
  fontSize: "var(--ifm-code-font-size)",
  verticalAlign: "middle",
}}>
<code>
${this.escapeText(text, { escape: "html" })}
</code>
</pre>`, { escape: "none" });
        }
        else {
            super.appendCodeBlock(text, options);
        }
    }
    appendSidebarLink({ title, embedName, }) {
        const embedPath = getEmbedPath(embedName);
        // TODO: handle this more gracefully. This happens when we have a direct
        // circular dependency, and the page needs to import itself, which can't be
        // done of course
        if (this.#currentPagePath === embedPath) {
            return;
        }
        const importPath = this.getRelativeImportPath(this.#currentPagePath, embedPath);
        this.insertDefaultImport(importPath, getEmbedSymbol(embedName));
        this.#includeSidebar = true;
        this.insertThirdPartyImport("SideBarTrigger", "@speakeasy-api/docs-md");
        this.insertThirdPartyImport("SideBar", "@speakeasy-api/docs-md");
        this[rendererLines].push(`<p>
    <SideBarTrigger.Docusaurus cta="${`View ${this.escapeText(title, { escape: "mdx" })}`}" title="${this.escapeText(title, { escape: "mdx" })}">
      <${getEmbedSymbol(embedName)} />
    </SideBarTrigger.Docusaurus>
  </p>`);
        if (this.#site.hasPage(embedPath)) {
            return;
        }
        return this.#site.createPage(embedPath);
    }
    appendTryItNow({ externalDependencies, defaultValue, }) {
        this.insertThirdPartyImport("TryItNow", "@speakeasy-api/docs-md");
        this[rendererLines].push(`<TryItNow
 externalDependencies={${JSON.stringify(externalDependencies)}}
 defaultValue={\`${defaultValue}\`}
/>`);
    }
    render() {
        const parentData = super.render();
        const data = (this.#frontMatter ? this.#frontMatter + "\n\n" : "") +
            parentData +
            (this.#includeSidebar ? "\n\n<SideBar.Docusaurus />\n" : "");
        return data;
    }
}
//# sourceMappingURL=docusaurus.js.map