import * as timings from "../../debug/timings";
import { toDraggableMap, toDroppableMap } from "../dimension-structures";
import getViewport from "../../view/window/get-viewport";

const getInitialPublish = ({ critical, scrollOptions, registry }) => {
  const timingKey = "Initial collection from DOM";
  timings.start(timingKey);
  const viewport = getViewport();
  const windowScroll = viewport.scroll.current;
  const home = critical.droppable;
  const droppables = registry.droppable
    .getAllByType(home.type)
    .map((entry) => entry.callbacks.getDimensionAndWatchScroll(windowScroll, scrollOptions));
  const draggables = registry.draggable
    .getAllByType(critical.draggable.type)
    .map((entry) => entry.getDimension(windowScroll));
  const dimensions = {
    draggables: toDraggableMap(draggables),
    droppables: toDroppableMap(droppables)
  };
  timings.finish(timingKey);
  const result = {
    dimensions,
    critical,
    viewport
  };
  return result;
};

export default getInitialPublish;
