import whenCombining from "./when-combining";
import whenReordering from "./when-reordering";
import withDroppableDisplacement from "../../with-scroll-change/with-droppable-displacement";

const getResultWithoutDroppableDisplacement = ({ impact, draggable, droppable, draggables, afterCritical }) => {
  const original = draggable.page.borderBox.center;
  const at = impact.at;
  if (!droppable) {
    return original;
  }
  if (!at) {
    return original;
  }
  if (at.type === "REORDER") {
    return whenReordering({
      impact,
      draggable,
      draggables,
      droppable,
      afterCritical
    });
  }
  return whenCombining({
    impact,
    draggables,
    afterCritical
  });
};

const getPageBorderBoxCenter = (args) => {
  const withoutDisplacement = getResultWithoutDroppableDisplacement(args);
  const droppable = args.droppable;

  return droppable ? withDroppableDisplacement(droppable, withoutDisplacement) : withoutDisplacement;
};

export default getPageBorderBoxCenter;
