import { serverClient } from "@/lib/trpc/server"

import BreadcrumbsComp from "@/components/TempDesignSystem/Breadcrumbs"
import { generateBreadcrumbsSchema } from "@/utils/jsonSchemas"

import type { BreadcrumbsProps } from "@/components/TempDesignSystem/Breadcrumbs/breadcrumbs"

export default async function Breadcrumbs({
  variant,
  subpageTitle,
}: Pick<BreadcrumbsProps, "variant" | "subpageTitle">) {
  const breadcrumbs = await serverClient().contentstack.breadcrumbs.get()
  if (!breadcrumbs?.length) {
    return null
  }

  if (subpageTitle) {
    breadcrumbs.push({ title: subpageTitle, uid: subpageTitle, href: "" })
  }

  const jsonSchema = generateBreadcrumbsSchema(breadcrumbs)

  return (
    <>
      <script
        type={jsonSchema.type}
        dangerouslySetInnerHTML={{
          __html: JSON.stringify(jsonSchema.jsonLd),
        }}
      />
      <BreadcrumbsComp breadcrumbs={breadcrumbs} variant={variant} />
    </>
  )
}
