import React, { ReactNode, useState } from 'react';
import { CairoProvider } from './CairoProvider';
import { experimentsFallbackApi, WixPatternsEssentials } from '@wix/bex-core';
import { CairoProviderBaseProps } from '@wix/bex-core/react';
import { Trans } from '@wix/bex-utils/@wix/wix-i18n-config';
import { WixPatternsMinimalEssentials } from './WixPatternsMinimalEssentials';

export interface WixPatternsBaseProviderProps extends CairoProviderBaseProps {
  /**
   * Minimal essentials that are required for wix-patterns to work (i.e i18n,bi,experiments)
   */
  value: WixPatternsMinimalEssentials;
  children?: ReactNode;
  localCache?: boolean;
}

export function WixPatternsBaseProvider({
  value: valueProp,
  children,
  ...rest
}: WixPatternsBaseProviderProps) {
  const [value] = useState((): WixPatternsEssentials => {
    const experiments = experimentsFallbackApi();

    return {
      useExperiments: () => ({
        ready: true,
        experiments,
      }),
      experiments,
      Trans,
      ...valueProp,
      user: {
        permissions: [],
        ...valueProp.user,
      },
    };
  });

  return (
    <CairoProvider {...rest} value={value}>
      {children}
    </CairoProvider>
  );
}
