import {
  WixPatternsContainerOverridesProvider,
  useWixPatternsContainerOverrides,
} from '@wix/bex-core/react';
import React, { ReactNode, useState } from 'react';
import { WixPatternsContainerParams, QueryCache } from '@wix/bex-core';

export interface WixPatternsTestProviderProps {
  children: ReactNode;
  overrides?: Partial<WixPatternsContainerParams>;
}

export const WixPatternsTestProvider = (
  props: WixPatternsTestProviderProps,
) => {
  const { overrides, children } = props;

  const existingWixPatternsContainerOverrides =
    useWixPatternsContainerOverrides();

  const [wixPatternsContainerOverrides] = useState(
    (): Partial<WixPatternsContainerParams> => ({
      queryCache: new QueryCache(),
      ...existingWixPatternsContainerOverrides,
      ...overrides,
    }),
  );

  return (
    <WixPatternsContainerOverridesProvider
      value={wixPatternsContainerOverrides}
    >
      {children}
    </WixPatternsContainerOverridesProvider>
  );
};
