import { SidebarsConfig } from "@docusaurus/plugin-content-docs";
import { SidebarItemConfig } from "@docusaurus/plugin-content-docs/src/sidebars/types.js";

function createLibraryReferenceStructure(
  libName: string,
  labelName: string,
  hasLinterRules: boolean,
  extra: SidebarItemConfig[]
): SidebarItemConfig {
  const rules: SidebarItemConfig = {
    type: "category",
    label: "Rules",
    items: [
      {
        type: "autogenerated",
        dirName: `libraries/${libName}/rules`,
      },
    ],
  };
  return {
    type: "category",
    label: labelName,
    link: {
      type: "doc",
      id: `libraries/${libName}/reference/index`,
    },
    items: [
      {
        type: "autogenerated",
        dirName: `libraries/${libName}/reference`,
      },
      ...(hasLinterRules ? [rules] : []),
      ...(extra ?? []),
    ],
  };
}

const sidebars: SidebarsConfig = {
  docsSidebar: [
    {
      type: "category",
      label: "Getting started",
      items: [
        "introduction/installation",
        {
          type: "category",
          label: "Editor",
          items: ["introduction/editor/vscode", "introduction/editor/vs"],
        },
      ],
    },
    {
      type: "category",
      label: "Guides",
      items: [
        "getting-started/getting-started",
        "getting-started/getting-started-http",
        "getting-started/typespec-for-openapi-dev",
      ],
    },
    {
      type: "category",
      label: "Handbook",
      items: [
        "handbook/cli",
        "handbook/style-guide",
        "handbook/formatter",
        "handbook/reproducibility",
        {
          type: "category",
          label: "Configuration",
          items: ["handbook/configuration/configuration", "handbook/configuration/tracing"],
        },
        "handbook/releases",
        "handbook/faq",
      ],
    },
    {
      type: "category",
      label: "📐 Language Basics",
      items: [
        "language-basics/overview",
        "language-basics/built-in-types",
        "language-basics/imports",
        "language-basics/namespaces",
        "language-basics/decorators",
        "language-basics/documentation",
        "language-basics/scalars",
        "language-basics/models",
        "language-basics/operations",
        "language-basics/interfaces",
        "language-basics/templates",
        "language-basics/enums",
        "language-basics/unions",
        "language-basics/intersections",
        "language-basics/type-literals",
        "language-basics/aliases",
        "language-basics/type-relations",
      ],
    },
    {
      type: "category",
      label: "📘 Standard Library",
      items: [
        "standard-library/built-in-decorators",
        "standard-library/built-in-data-types",
        {
          type: "autogenerated",
          dirName: `standard-library/reference`,
        },
        "standard-library/projected-names",
        "standard-library/discriminated-types",
      ],
    },
    {
      type: "category",
      label: "📚 Libraries",
      items: [
        createLibraryReferenceStructure("http", "Http", true, [
          "libraries/http/cheat-sheet",
          "libraries/http/authentication",
          "libraries/http/operations",
          "libraries/http/content-types",
          "libraries/http/multipart",
          "libraries/http/encoding",
        ]),
        createLibraryReferenceStructure("json-schema", "JSON Schema", false, []),
        createLibraryReferenceStructure("rest", "Rest", false, [
          "libraries/rest/cheat-sheet",
          "libraries/rest/resource-routing",
        ]),
        createLibraryReferenceStructure("openapi", "OpenAPI", false, []),
        createLibraryReferenceStructure("openapi3", "OpenAPI3", false, [
          "libraries/openapi3/openapi",
          "libraries/openapi3/diagnostics",
        ]),
        createLibraryReferenceStructure("protobuf", "Protobuf", false, [
          "libraries/protobuf/guide",
        ]),
        createLibraryReferenceStructure("versioning", "Versioning", false, [
          "libraries/versioning/guide",
        ]),
        createLibraryReferenceStructure("xml", "Xml", false, ["libraries/xml/guide"]),
      ],
    },
    {
      type: "category",
      label: "🛠️ Writing TypeSpec Libraries",
      items: [
        "extending-typespec/basics",
        "extending-typespec/diagnostics",
        "extending-typespec/create-decorators",
        "extending-typespec/linters",
        "extending-typespec/codefixes",
        "extending-typespec/emitters",
        "extending-typespec/emitter-framework",
        "extending-typespec/emitter-metadata-handling",
        "extending-typespec/writing-scaffolding-template",
      ],
    },
    {
      type: "category",
      label: "🚀 Release Notes",
      collapsed: true,
      link: {
        type: "generated-index",
        title: "Release Notes",
        slug: "/release-notes",
      },
      items: [
        {
          type: "autogenerated",
          dirName: "release-notes",
        },
      ],
    },
  ],
};

export default sidebars;
