import { TableDragAndDropContext } from './TableDragAndDropContext';
import { TableDragAndDrop as WDSTableDragAndDrop } from '@wix/wix-style-react-incubator/drag-and-drop';
import { minimalRequiredWdsRuntimeCheck } from '../minimalRequiredWdsRuntimeCheck';
import { TableDragAndDropState } from './DragAndDrop/TableDragAndDropState';
import { TableState } from '../state';
import { FiltersMap } from '@wix/bex-core';

if (process.env.NODE_ENV !== 'production') {
  minimalRequiredWdsRuntimeCheck({
    required: '^1.64.1',
    requiredBy: 'WixPatterns/TableDragAndDrop',
  });
}

export type TableDragAndDropType = typeof WDSTableDragAndDrop & {
  TableDragAndDropContext: typeof TableDragAndDropContext;
  setStateObject: typeof setStateObject;
};

export function setStateObject<T, F extends FiltersMap>({
  state,
  modalsContainerRef,
}: {
  state: TableState<T, F>;
  modalsContainerRef: {
    current: HTMLElement | null | undefined;
  };
}) {
  state.tableDragAndDropState ??= new TableDragAndDropState({
    tableState: state,
    modalsContainerRef,
  });
  state.toolbar._dnd = state.tableDragAndDropState.dnd;
}

export const TableDragAndDrop: TableDragAndDropType = {
  ...WDSTableDragAndDrop,
  TableDragAndDropContext,
  setStateObject,
};
