import React, { useEffect } from 'react';
import { observer } from 'mobx-react-lite';
import { useWixPatternsContainer } from '@wix/bex-core/react';
import { usePageContext } from '../../providers';
import { CustomColumnsEntityPageState } from '../../state/CustomColumnsEntityPageState';
import { WixPatternsModalsContainer } from '../WixPatternsModalsContainer';
import { WixPatternsPanelModalsContainer } from '../WixPatternsPanelModalsContainer';
import { CustomColumnsEntityPagePanelEntry } from './CustomColumnsEntityPagePanelEntry';

export interface CustomColumnsEntityPageCTAProps {
  state: CustomColumnsEntityPageState;
}

function _CustomColumnsEntityPageCTA({
  state,
}: CustomColumnsEntityPageCTAProps) {
  const { panels } = state;
  const { isPanelLayout, useRelativeSidePanel } = useWixPatternsContainer();
  const { modalsContainerRef, stackingRootRef } = usePageContext();

  useEffect(() => {
    CustomColumnsEntityPagePanelEntry.state = state;
    panels.registerSidePanel(
      'customColumns',
      CustomColumnsEntityPagePanelEntry,
    );
    return panels.init();
  }, []);

  if (isPanelLayout) {
    return <WixPatternsPanelModalsContainer state={panels} />;
  }

  return (
    <WixPatternsModalsContainer
      rootRef={stackingRootRef}
      anchorRef={useRelativeSidePanel ? stackingRootRef : modalsContainerRef}
      state={panels}
    />
  );
}

_CustomColumnsEntityPageCTA.displayName = 'CustomColumnsEntityPageCTA';

export const CustomColumnsEntityPageCTA = observer(_CustomColumnsEntityPageCTA);
