import { Essentials } from '@wix/fe-essentials/core';
import {
  useExperiments,
  useTranslation,
  Trans,
} from '@wix/fe-essentials/react';
import {
  WixPatternsEssentials,
  WixPatternsEssentialsOverrides,
  getExperimentsCompat,
} from '@wix/bex-core';

export function buildWixPatternsEssentials(
  essentials: Essentials,
  { overrides }: { overrides?: Partial<WixPatternsEssentialsOverrides> } = {},
): WixPatternsEssentials {
  const { errorMonitor, httpClient, environment } = essentials;

  return {
    createEssentials: (options) =>
      essentials.createChildEssentials({
        ...options,
        // currently avoid client-side conduction for cairo experiments
        experiments: {
          bag: {},
        },
      }),
    errorMonitor: errorMonitor ?? { captureException: () => {} },
    useTranslation,
    Trans,
    useExperiments,
    showToast: () => ({ remove: () => {} }),
    httpClient,
    experiments: getExperimentsCompat(essentials),
    ...overrides,
    user: {
      permissions: [],
      ...overrides?.user,
    },
    environment: {
      ...environment,
      ...overrides?.environment,
      providerType: 'essentials',
    },
    environmentBI: {
      artifactId: environment.artifactId,
    },
  };
}
