import { FiltersMap } from '@wix/bex-core';
import { observer } from 'mobx-react-lite';
import { ToolbarCollectionState } from '../../state';
import { BulkActionModalRenderProp } from './BulkActionModalRenderProp';

export interface SingleCollectionBulkActionModalProps<T, F extends FiltersMap> {
  state: ToolbarCollectionState<T, F>;
  bulkActionModal: BulkActionModalRenderProp<T, F>;
}

function _SingleCollectionBulkActionModal<T, F extends FiltersMap>(
  props: SingleCollectionBulkActionModalProps<T, F>,
) {
  const { state, bulkActionModal } = props;
  const { collection, bulk } = state;

  const { modals } = bulk;

  const {
    allSelected,
    selectedValues,
    uncheckedInCurrentCollection: uncheckedValues,
    scrollToTop,
  } = collection.bulkSelect;

  const { openModal, closeModal, isModalOpen, closeModalAndScrollTop } = modals;

  return bulkActionModal({
    allSelected,
    selectedValues,
    uncheckedValues,
    openModal,
    closeModal,
    isModalOpen,
    scrollToTop,
    closeModalAndScrollTop,
    query: collection.result.originQuery,
  });
}

export const SingleCollectionBulkActionModal = observer(
  _SingleCollectionBulkActionModal,
);
