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 NextraSite extends MdxSite {
    buildPagePath(slug) {
        const settings = getSettings();
        return resolve(join(settings.output.pageOutDir, `${slug}/page.mdx`));
    }
    render() {
        const settings = getSettings();
        const schemasEntry = settings.display.showSchemasInNav
            ? `\n  schemas: { title: "Schemas", theme: { collapsed: false } },`
            : "";
        const config = `export default {
  index: { title: "About", theme: { collapsed: false } },
  tag: { title: "Operations", theme: { collapsed: false } },${schemasEntry}
}`;
        this.createPage(join(settings.output.pageOutDir, "_meta.ts")).appendText(config, { escape: "none" });
        return super.render();
    }
    getRenderer(options) {
        return new NextraRenderer(options, this);
    }
}
class NextraRenderer extends MdxRenderer {
    #frontMatter;
    #includeSidebar = false;
    #currentPagePath;
    #site;
    constructor({ currentPagePath }, site) {
        super();
        this.#currentPagePath = currentPagePath;
        this.#site = site;
    }
    insertFrontMatter({ sidebarLabel, }) {
        this.#frontMatter = `---
sidebarTitle: ${this.escapeText(sidebarLabel, { escape: "mdx" })}
---`;
    }
    appendCodeBlock(text, options) {
        if (options?.variant === "raw") {
            this.appendText(`<pre className="x:group x:focus-visible:nextra-focus x:overflow-x-auto x:subpixel-antialiased x:text-[.9em] x:bg-white x:dark:bg-black x:py-4 x:ring-1 x:ring-inset x:ring-gray-300 x:dark:ring-neutral-700 x:contrast-more:ring-gray-900 x:contrast-more:dark:ring-gray-50 x:contrast-more:contrast-150 x:rounded-md not-prose">
<code className="nextra-code">
${this.escapeText(text, { escape: "html" })
                .split("\n")
                // Nextra does this weird thing where it wraps each line in _two_ spans with
                // it's code blocks, so we mimic that behavior here
                .map((line) => `<span><span>${line}</span></span>`)
                .join("\n")}
</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.Nextra cta="${`View ${this.escapeText(title, { escape: "mdx" })}`}" title="${this.escapeText(title, { escape: "mdx" })}">
      <${getEmbedSymbol(embedName)} />
    </SideBarTrigger.Nextra>
  </p>`);
        if (this.#site.hasPage(embedPath)) {
            return;
        }
        return this.#site.createPage(embedPath);
    }
    // TODO: we have to update Try It Now to not have a client component at the
    // root before we can support it in Nextra
    //   public override appendTryItNow({
    //     externalDependencies,
    //     defaultValue,
    //   }: {
    //     externalDependencies: Record<string, string>;
    //     defaultValue: string;
    //   }) {
    //     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.Nextra />\n" : "");
        return data;
    }
}
//# sourceMappingURL=nextra.js.map