'use client';

import type * as React from 'react';
import { ConfigProviderOverride } from '../components/ConfigProvider/ConfigProviderOverride';
import { type BrowserInfo, computeBrowserInfo } from './browser';
import type { Direction } from './direction';
import { platform as getPlatform } from './platform';

export interface SSRWrapperProps {
  userAgent?: string | undefined;
  browserInfo?: BrowserInfo | undefined;
  direction?: Direction | undefined;
  children?: React.ReactNode | undefined;
}

/**
 * @see https://vkui.io/overview/ssr
 */
export const SSRWrapper: React.FC<SSRWrapperProps> = ({
  userAgent,
  browserInfo,
  direction = 'ltr',
  children,
}) => {
  if (!browserInfo && userAgent) {
    browserInfo = computeBrowserInfo(userAgent);
  }

  return (
    <ConfigProviderOverride direction={direction} platform={getPlatform(browserInfo)}>
      {children}
    </ConfigProviderOverride>
  );
};
