import 'server-only';
import ClientRoot from './client-root';
import { cookies } from 'next/headers';
import PzProviders from './pz-providers';
import { WebVitals } from './web-vitals';
import settings from 'settings';

export default async function PzRoot({
  translations,
  children,
  locale
}: {
  translations?: any;
  children: React.ReactNode;
  locale?: string;
}) {
  const nextCookies = await cookies();
  const sessionid = nextCookies.get('osessionid')?.value;

  if (!translations) {
    const { getTranslations } =
      settings.useOptimizedTranslations && locale
        ? require('translations')
        : require('../utils/server-translation');

    translations = await getTranslations(locale);
  }

  return (
    <PzProviders translations={translations}>
      <WebVitals />
      <ClientRoot sessionId={sessionid}>{children}</ClientRoot>
    </PzProviders>
  );
}
