import { headers } from "next/headers";
import type { ReactNode } from "react";
import { auth } from "@/auth";
import { StoreChrome } from "@/components/store/store-chrome";
import { StoreProvider } from "@/components/store/store-provider";
import { storefrontBasePath } from "@/lib/storefront-path";

export default async function StoreLayout({ children }: { children: ReactNode }) {
  const [session, requestHeaders] = await Promise.all([auth(), headers()]);
  const basePath = storefrontBasePath(requestHeaders.get("host"));
  const customerName = session?.user.kind === "customer" ? session.user.name ?? undefined : undefined;

  return (
    <StoreProvider basePath={basePath}>
      <StoreChrome customerName={customerName}>{children}</StoreChrome>
    </StoreProvider>
  );
}
