import React from "react";
import {
  AppleWebAppMeta,
  BasicMetadata,
  FormatDetectionMeta,
  ItunesMeta,
  VerificationMeta,
} from "./generate/basic";
import { AlternatesMetadata } from "./generate/alternate";
import {
  AppLinksMeta,
  OpenGraphMetadata,
  TwitterMetadata,
} from "./generate/opengraph";
import { IconsMetadata } from "./generate/icons";
import { accumulateMetadata, resolveMetadata } from "./resolve-metadata";
import { LoaderTree } from "../../server/lib/app-dir-module";
import { GetDynamicParamFromSegment } from "../../server/app-render/app-render";

// Generate the actual React elements from the resolved metadata.
export async function MetadataTree({
  tree,
  pathname,
  searchParams,
  getDynamicParamFromSegment,
}: {
  tree: LoaderTree;
  pathname: string;
  searchParams: { [key: string]: any };
  getDynamicParamFromSegment: GetDynamicParamFromSegment;
}) {
  const options = {
    pathname,
  };
  const resolvedMetadata = await resolveMetadata({
    tree,
    parentParams: {},
    metadataItems: [],
    searchParams,
    getDynamicParamFromSegment,
  });
  const metadata = await accumulateMetadata(resolvedMetadata, options);

  return (
    <>
      <BasicMetadata metadata={metadata} />
      <AlternatesMetadata alternates={metadata.alternates} />
      <ItunesMeta itunes={metadata.itunes} />
      <FormatDetectionMeta formatDetection={metadata.formatDetection} />
      <VerificationMeta verification={metadata.verification} />
      <AppleWebAppMeta appleWebApp={metadata.appleWebApp} />
      <OpenGraphMetadata openGraph={metadata.openGraph} />
      <TwitterMetadata twitter={metadata.twitter} />
      <AppLinksMeta appLinks={metadata.appLinks} />
      <IconsMetadata icons={metadata.icons} />
    </>
  );
}
