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

export async function generateMetadata() {
  const metaData = await serverClient().contentstack.metaData.get()

  if (!metaData) {
    return {
      title: "",
      description: "",
      openGraph: {
        images: [],
      },
    }
  }

  const title = metaData?.breadcrumbsTitle ?? metaData?.title ?? ""
  const description = metaData?.description ?? ""
  const images =
    metaData?.imageConnection?.edges?.map((edge) => ({
      url: edge.node.url,
    })) || []

  return {
    title,
    description,
    openGraph: {
      images,
    },
  }
}

export async function generateMetadataAccountPage() {
  const metaData = await serverClient().contentstack.accountPage.metadata.get()

  if (!metaData) {
    return {
      title: "",
      description: "",
      openGraph: {
        images: [],
      },
    }
  }

  const title = metaData?.breadcrumbsTitle ?? metaData?.title ?? ""
  const description = metaData?.description ?? ""
  const images =
    metaData?.imageConnection?.edges?.map((edge) => ({
      url: edge.node.url,
    })) || []

  return {
    title,
    description,
    openGraph: {
      images,
    },
  }
}
