import React, { useState } from 'react';
import {
  WixPatternsDashboardBaseProvider,
  WixPatternsProviderProps,
} from './WixPatternsDashboardBaseProvider';
import { createEssentials } from '@wix/bex-utils/@wix/bex-bundled-mini-essentials';
import { i18n } from '@wix/essentials';
import { dashboard } from '@wix/dashboard';
import { useEnvironment } from './useEnvironment';

export function WixPatternsDashboardProvider(props: WixPatternsProviderProps) {
  const environment = useEnvironment();

  const [essentials] = useState(() => {
    return createEssentials({
      environment: {
        language: i18n.getLocale(),
        artifactId: '',
        version: '',
      },
      httpClient: {
        getAppToken: () => dashboard.getAccessToken(),
        baseURL: 'https://www.wixapis.com',
        isSSR: true,
      },
      experiments: {
        bag: {}, // app-level experiments are not supported in TPA
      },
    });
  });

  if (!environment) {
    return null;
  }

  return (
    <WixPatternsDashboardBaseProvider
      {...props}
      dashboard={dashboard}
      environment={environment}
      essentials={essentials}
    />
  );
}
