import { Height, Width } from '@ephox/sugar';
import * as AlloyEvents from '../../api/events/AlloyEvents';
import * as SystemEvents from '../../api/events/SystemEvents';
import * as Snappables from '../snap/Snappables';
import * as BlockerUtils from './BlockerUtils';
import * as DragMovement from './DragMovement';
const calcStartData = (dragConfig, comp) => ({
    bounds: dragConfig.getBounds(),
    height: Height.getOuter(comp.element),
    width: Width.getOuter(comp.element)
});
const move = (component, dragConfig, dragState, dragMode, event) => {
    const delta = dragState.update(dragMode, event);
    const dragStartData = dragState.getStartData().getOrThunk(() => calcStartData(dragConfig, component));
    delta.each((dlt) => {
        DragMovement.dragBy(component, dragConfig, dragStartData, dlt);
    });
};
const stop = (component, blocker, dragConfig, dragState) => {
    blocker.each(BlockerUtils.discard);
    dragConfig.snaps.each((snapInfo) => {
        Snappables.stopDrag(component, snapInfo);
    });
    const target = dragConfig.getTarget(component.element);
    dragState.reset();
    dragConfig.onDrop(component, target);
};
const handlers = (events) => (dragConfig, dragState) => {
    const updateStartState = (comp) => {
        dragState.setStartData(calcStartData(dragConfig, comp));
    };
    return AlloyEvents.derive([
        AlloyEvents.run(SystemEvents.windowScroll(), (comp) => {
            // Only update if we have some start data
            dragState.getStartData().each(() => updateStartState(comp));
        }),
        ...events(dragConfig, dragState, updateStartState)
    ]);
};
export { calcStartData, handlers, move, stop };
//# sourceMappingURL=DragUtils.js.map