import { Store } from './globals'

import getCorePlacement from '../utils/getCorePlacement'
import find             from '../utils/find'
import prefix           from '../utils/prefix'

/**
* Mousemove event listener callback method for follow cursor setting
* @param {MouseEvent} e
*/
export default function followCursorHandler(e) {
    const refData = find(Store, refData => refData.el === this)
    const { popper } = refData

    const position = getCorePlacement(popper.getAttribute('x-placement'))
    const halfPopperWidth = Math.round( popper.offsetWidth / 2 )
    const halfPopperHeight = Math.round( popper.offsetHeight / 2 )
    const viewportPadding = 5
    const pageWidth = document.documentElement.offsetWidth || document.body.offsetWidth

    const { pageX, pageY } = e

    let x, y

    switch (position) {
        case 'top':
            x = pageX - halfPopperWidth
            y = pageY - 2.5 * halfPopperHeight
            break
        case 'left':
            x = pageX - ( 2 * halfPopperWidth ) - 15
            y = pageY - halfPopperHeight
            break
        case 'right':
            x = pageX + halfPopperHeight
            y = pageY - halfPopperHeight
            break
        case 'bottom':
            x = pageX - halfPopperWidth
            y = pageY + halfPopperHeight/1.5
            break
    }

    const isRightOverflowing = pageX + viewportPadding + halfPopperWidth > pageWidth
    const isLeftOverflowing = pageX - viewportPadding - halfPopperWidth < 0

    // Prevent left/right overflow
    if (position === 'top' || position === 'bottom') {
        if (isRightOverflowing) {
            x = pageWidth - viewportPadding - ( 2 * halfPopperWidth)
        }

        if (isLeftOverflowing) {
            x = viewportPadding
        }
    }

    popper.style[prefix('transform')] = `translate3d(${x}px, ${y}px, 0)`
}

@@ -7,16 +7,27 @@
  %7B Store
+, Selectors
  %7D from 
@@ -187,16 +187,64 @@
 /prefix'
+%0Aimport closest          from '../utils/closest'
 %0A%0A/**%0A* 
@@ -450,16 +450,17 @@
 = this)%0A
+%0A
     cons
@@ -462,23 +462,85 @@
  const %7B
- popper
+%0A        popper,%0A        settings: %7B%0A            offset%0A        %7D%0A   
  %7D = ref
@@ -650,33 +650,32 @@
 th = Math.round(
- 
 popper.offsetWid
@@ -676,25 +676,24 @@
 setWidth / 2
- 
 )%0A    const 
@@ -722,17 +722,16 @@
 h.round(
- 
 popper.o
@@ -745,17 +745,16 @@
 ight / 2
- 
 )%0A    co
@@ -987,32 +987,41 @@
  halfPopperWidth
+ + offset
 %0A            y =
@@ -1031,16 +1031,17 @@
 geY - 2.
+2
 5 * half
@@ -1143,17 +1143,17 @@
 th ) - 1
-5
+0
 %0A       
@@ -1177,32 +1177,41 @@
 halfPopperHeight
+ + offset
 %0A            bre
@@ -1308,32 +1308,41 @@
 halfPopperHeight
+ + offset
 %0A            bre
@@ -1402,24 +1402,33 @@
 fPopperWidth
+ + offset
 %0A           
@@ -1559,16 +1559,25 @@
 erWidth 
++ offset 
 %3E pageWi
@@ -1652,16 +1652,25 @@
 erWidth 
++ offset 
 %3C 0%0A%0A   

Make followCursor respect offset setting