import recompute from "../../update-displacement-visibility/recompute";
import { emptyGroups } from "../../no-impact";

const getDropImpact = ({ draggables, reason, lastImpact, home, viewport, onLiftImpact }) => {
  if (!lastImpact.at || reason !== "DROP") {
    // Dropping outside of a list or the drag was cancelled

    // Going to use the on lift impact
    // Need to recompute the visibility of the original impact
    // What is visible can be different to when  the drag started

    const recomputedHomeImpact = recompute({
      draggables,
      impact: onLiftImpact,
      destination: home,
      viewport,
      // We need the draggables to animate back to their positions
      forceShouldAnimate: true
    });
    return {
      impact: recomputedHomeImpact,
      didDropInsideDroppable: false
    };
  }

  // use the existing impact
  if (lastImpact.at.type === "REORDER") {
    return {
      impact: lastImpact,
      didDropInsideDroppable: true
    };
  }

  // When merging we remove the movement so that everything
  // will animate closed
  const withoutMovement = {
    ...lastImpact,
    displaced: emptyGroups
  };
  return {
    impact: withoutMovement,
    didDropInsideDroppable: true
  };
};

export default getDropImpact;
