import { useEffect, useState } from 'react';
import { useWixPatternsContainer } from '@wix/bex-core/react';
import type {
  ExploreAppsModalProps,
  ExploreAppsState,
} from './ExploreAppsState';

export const useExploreAppsState = (
  exploreAppsModalProps?: ExploreAppsModalProps,
) => {
  const { reportBi, translate } = useWixPatternsContainer();
  const [exploreAppsState, setExploreAppsState] = useState<ExploreAppsState>();

  useEffect(() => {
    if (!exploreAppsModalProps) {
      return;
    }

    import('./ExploreAppsState').then(({ ExploreAppsState }) => {
      setExploreAppsState(
        new ExploreAppsState({
          modalProps: exploreAppsModalProps,
          reportBi,
          translatedText: translate(
            'cairo.collectionPage.appInstall-inContext.moreActions.relatedApps.CTA',
          ),
        }),
      );
    });
  }, []);

  return exploreAppsState;
};
