{"size": 2137, "ext": "js", "max_stars_repo_path": "src/state/move-in-direction/move-cross-axis/move-to-new-droppable/index.js", "max_stars_repo_name": "Snaphy-Automata/react-beautiful-dnd", "max_stars_repo_stars_event_min_datetime": "2019-05-13T02:53:55.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-23T13:43:46.000Z", "max_issues_repo_path": "src/state/move-in-direction/move-cross-axis/move-to-new-droppable/index.js", "max_issues_repo_name": "Snaphy-Automata/react-beautiful-dnd", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/state/move-in-direction/move-cross-axis/move-to-new-droppable/index.js", "max_forks_repo_name": "Snaphy-Automata/react-beautiful-dnd", "max_forks_repo_forks_event_min_datetime": "2021-08-29T10:43:47.000Z", "max_forks_repo_forks_event_max_datetime": "2021-08-29T10:43:47.000Z"}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 26.7125, "max_line_length": 87, "alphanum_fraction": 0.6963032288}
// @flow
import { type Position } from 'css-box-model';
import invariant from 'tiny-invariant';
import type {
  DraggableDimension,
  DroppableDimension,
  DragImpact,
  Viewport,
  DraggableDimensionMap,
} from '../../../../types';
import toHomeList from './to-home-list';
import toForeignList from './to-foreign-list';
import isHomeOf from '../../../droppable/is-home-of';

type Args = {|
  // the current center position of the draggable
  previousPageBorderBoxCenter: Position,
  // the draggable that is dragging and needs to move
  draggable: DraggableDimension,
  // what the draggable is moving towards
  // can be null if the destination is empty
  moveRelativeTo: ?DraggableDimension,
  // the droppable the draggable is moving to
  destination: DroppableDimension,
  // all the draggables inside the destination
  insideDestination: DraggableDimension[],
  // the impact of a previous drag,
  previousImpact: DragImpact,
  // the viewport
  viewport: Viewport,
  draggables: DraggableDimensionMap,
|};

export default ({
  previousPageBorderBoxCenter,
  destination,
  insideDestination,
  draggable,
  draggables,
  moveRelativeTo,
  previousImpact,
  viewport,
}: Args): ?DragImpact => {
  // Draggables available, but none are candidates for movement
  // Cannot move into the list
  // Note: can move to empty list and then !moveRelativeTo && !insideDestination.length
  if (insideDestination.length && !moveRelativeTo) {
    return null;
  }

  if (moveRelativeTo) {
    invariant(
      moveRelativeTo.descriptor.droppableId === destination.descriptor.id,
      'Unable to find target in destination droppable',
    );
  }

  const isMovingToHome: boolean = isHomeOf(draggable, destination);

  return isMovingToHome
    ? toHomeList({
        moveIntoIndexOf: moveRelativeTo,
        insideDestination,
        draggable,
        destination,
        previousImpact,
        viewport,
      })
    : toForeignList({
        previousPageBorderBoxCenter,
        moveRelativeTo,
        insideDestination,
        draggable,
        draggables,
        destination,
        previousImpact,
        viewport,
      });
};
