function generateGetNextPosition(nextPositionDirection) {
  return function(element, previousPosition, connectedAtPosition) {
    var nextPosition = {
      x: previousPosition.x,
      y: previousPosition.y
    };

    [ 'x', 'y' ].forEach(function(axis) {

      var nextPositionDirectionForAxis = nextPositionDirection[ axis ];

      if (!nextPositionDirectionForAxis) {
        return;
      }

      var dimension = axis === 'x' ? 'width' : 'height';

      var margin = nextPositionDirectionForAxis.margin,
          minDistance = nextPositionDirectionForAxis.minDistance;

      if (margin < 0) {
        nextPosition[ axis ] = Math.min(
          connectedAtPosition[ axis ] + margin - element[ dimension ] / 2,
          previousPosition[ axis ] - minDistance + margin
        );
      } else {
        nextPosition[ axis ] = Math.max(
          connectedAtPosition[ axis ] + connectedAtPosition[ dimension ] + margin + element[ dimension ] / 2,
          previousPosition[ axis ] + minDistance + margin
        );
      }
    });

    return nextPosition;
  };
}
Returns function that returns next position.

@param {Object} nextPositionDirection
@param {Object} [nextPositionDirection.x]
@param {Object} [nextPositionDirection.y]

@return {(element: Element, previousPosition: Point, connectedAtPosition: Element) => Point}