import whatIsDraggedOver from "../../droppable/what-is-dragged-over";
import { subtract } from "../../position";
import getClientBorderBoxCenter from "../../get-center-from-impact/get-client-border-box-center";

const getNewHomeClientOffset = ({ impact, draggable, dimensions, viewport, afterCritical }) => {
  const { draggables, droppables } = dimensions;
  const droppableId = whatIsDraggedOver(impact);
  const destination = droppableId ? droppables[droppableId] : null;
  const home = droppables[draggable.descriptor.droppableId];
  const newClientCenter = getClientBorderBoxCenter({
    impact,
    draggable,
    draggables,
    // if there is no destination, then we will be dropping back into the home
    afterCritical,
    droppable: destination || home,
    viewport
  });
  const offset = subtract(newClientCenter, draggable.client.borderBox.center);
  return offset;
};

export default getNewHomeClientOffset;
