import { invariant } from "../../../invariant";
import { add } from "../../position";
import getCombinedItemDisplacement from "../../get-combined-item-displacement";
import { tryGetCombine } from "../../get-impact-location";

const whenCombining = ({ afterCritical, impact, draggables }) => {
  const combine = tryGetCombine(impact);
  invariant(combine);
  const combineWith = combine.draggableId;
  const center = draggables[combineWith].page.borderBox.center;
  const displaceBy = getCombinedItemDisplacement({
    displaced: impact.displaced,
    afterCritical,
    combineWith,
    displacedBy: impact.displacedBy
  });
  return add(center, displaceBy);
};

// Returns the client offset required to move an item from its
// original client position to its final resting position
export default whenCombining;
