import { NestedTableNestedModeState } from '../../state';
import { FiltersMap } from '@wix/bex-core';
import { NestedTableLevelDescriptor } from '../../state/NestedTableState/NestedTableLevelDescriptor';

function levelHasRequiredProps<C extends string, T, F extends FiltersMap>(
  level: NestedTableLevelDescriptor<C, T, F>,
) {
  return level.setParent != null;
}

function runWarningChecks<C extends string, T, F extends FiltersMap>(
  level: NestedTableLevelDescriptor<C, T, F>,
) {
  if (level.dragAndDropSubmit == null) {
    console.warn(
      'dragAndDropSubmit is not defined in levelDescriptor, no server side update will be made',
    );
  }
}

export function validateNestedTableDragAndDropProps(
  state: NestedTableNestedModeState,
) {
  if (!state.levels.every(levelHasRequiredProps)) {
    throw new Error(`NestedTableDragAndDrop: All levels must have 'setParent'`);
  }

  state.levels.forEach(runWarningChecks);
}
