import { DragAndDropState } from '../../components/DragAndDrop';
import { CollectionState, FiltersMap } from '@wix/bex-core';
import { reaction } from 'mobx';

export function createOptimisticActionsOrderByModeReaction<
  T,
  F extends FiltersMap,
>(dnd: DragAndDropState<T, F>, collection: CollectionState<T, F>) {
  return reaction(
    () => dnd.isDisabled,
    (isDisabled) => {
      const { _optimisticActions } = collection;
      if (!_optimisticActions) {
        return;
      }

      _optimisticActions._orderByMode = isDisabled ? 'orders' : 'moves';
    },
    { fireImmediately: true },
  );
}
