import { useEffect, useState } from 'react';
import {
  ExtensionMenuItemsState,
  ExtensionMenuItemsStateParams,
} from '../state/ExtensionMenuItemsState';
import { useWixPatternsContainer } from '@wix/bex-core/react';

export function useExtensionMenuItems(
  params: Omit<ExtensionMenuItemsStateParams, 'container'> & {
    extension?: ExtensionMenuItemsState;
  },
) {
  const container = useWixPatternsContainer();
  const { extension, ...stateParams } = params;

  const [state] = useState(() => {
    return (
      extension ?? new ExtensionMenuItemsState({ ...stateParams, container })
    );
  });

  useEffect(() => {
    if (!extension) {
      return state.init();
    }
    return () => {};
  }, []);

  return state;
}
