import { Selectors } from './globals'

/**
* Prepares the callback functions for `show` and `hide` methods
* @param {Object} refData -  the element/popper reference data
* @param {Number} duration
* @param {Function} callback - callback function to fire once transitions complete
*/
export default function onTransitionEnd(refData, duration, callback) {
  // Make callback synchronous if duration is 0
  if (!duration) {
    return callback()
  }

  const tooltip = refData.popper.querySelector(Selectors.TOOLTIP)
  let transitionendFired = false

  const listenerCallback = e => {
    if (e.target !== tooltip) return

    transitionendFired = true

    tooltip.removeEventListener('webkitTransitionEnd', listenerCallback)
    tooltip.removeEventListener('transitionend', listenerCallback)

    callback()
  }

  // Wait for transitions to complete
  tooltip.addEventListener('webkitTransitionEnd', listenerCallback)
  tooltip.addEventListener('transitionend', listenerCallback)

  // transitionend listener sometimes may not fire
  clearTimeout(refData._transitionendTimeout)
  refData._transitionendTimeout = setTimeout(() => {
    !transitionendFired && callback()
  }, duration)
}

@@ -32,16 +32,74 @@
 obals'%0A%0A
+import getInnerElements from '../utils/getInnerElements'%0A%0A
 /**%0A* Pr
@@ -179,48 +179,8 @@
 ect%7D
- refData -  the element/popper reference
  dat
@@ -334,20 +334,17 @@
 tionEnd(
-refD
+d
 ata, dur
@@ -463,24 +463,26 @@
 %0A  const
+ %7B
  tooltip
  = refDa
@@ -477,58 +477,43 @@
 ltip
+ %7D
  = 
-refData.popper.querySelector(Selectors.TOOLTIP)
+getInnerElements(data.popper)%0A
 %0A  l
@@ -581,46 +581,8 @@
 %3E %7B%0A
-    if (e.target !== tooltip) return%0A%0A
     
@@ -749,16 +749,50 @@
 lback)%0A%0A
+    if (e.target === tooltip) %7B%0A  
     call
@@ -794,24 +794,30 @@
  callback()%0A
+    %7D%0A
   %7D%0A%0A  // Wa
@@ -818,16 +818,26 @@
  // 
-Wait for
+Fire callback upon
  tra
@@ -847,20 +847,16 @@
 tion
-s to
  complet
 e%0A  
@@ -851,17 +851,19 @@
  complet
-e
+ion
 %0A  toolt
@@ -990,16 +990,26 @@
 k)%0A%0A  //
+ Fallback:
  transit
@@ -1062,20 +1062,17 @@
 Timeout(
-refD
+d
 ata._tra
@@ -1096,12 +1096,9 @@
 )%0A  
-refD
+d
 ata.
@@ -1144,16 +1144,20 @@
 %3E %7B%0A    
+if (
 !transit
@@ -1171,11 +1171,17 @@
 ired
- &&
+) %7B%0A     
  cal
@@ -1188,16 +1188,22 @@
 lback()%0A
+    %7D%0A
   %7D, dur

Fix transitionend glitch