import Ember from 'ember'
const {
  Component,
  deprecate,
  on,
  run: {
    debounce,
    scheduleOnce
  },
  typeOf
} = Ember
import PropTypeMixin, {PropTypes} from 'ember-prop-types'

const debouncePeriod = 150

export default Component.extend(PropTypeMixin, {

  // == Component properties ==================================================

  classNames: ['frost-scroll'],

  // == State properties ======================================================

  propTypes: {
    hook: PropTypes.string
  },

  // == Events ================================================================

  /* eslint-disable complexity */
  initializeScroll: on('didInsertElement', function () {
    scheduleOnce('afterRender', this, () => {
      window.Ps.initialize(this.$()[0])
    })

    if (typeOf(this.onScrollUp) === 'function') {
      this.$().on('ps-scroll-up', () => {
        debounce(this, this.onScrollUp, debouncePeriod, true)
      })
    }

    if (typeOf(this.onScrollDown) === 'function') {
      this.$().on('ps-scroll-down', () => {
        debounce(this, this.onScrollDown, debouncePeriod, true)
      })
    }

    if (typeOf(this.onScrollYStart) === 'function') {
      this.$().on('ps-y-reach-start', () => {
        debounce(this, this.onScrollYStart, debouncePeriod, true)
      })
    }

    if (typeOf(this.onScrollYEnd) === 'function') {
      this.$().on('ps-y-reach-end', () => {
        debounce(this, this.onScrollYEnd, debouncePeriod, true)
      })
    }

    if (typeOf(this.attrs['on-scroll-y-end']) === 'function') {
      deprecate('on-scroll-y-end has been deprecated in favor of onScrollYEnd',
        false,
        {
          id: 'frost-debug.deprecate-on-scroll-y-end',
          until: '1.0.0',
          url: 'http://ciena-frost.github.io/ember-frost-core/#/scroll'
        }
      )
      this.$().on('ps-y-reach-end', () => {
        debounce(this, this['on-scroll-y-end'], debouncePeriod, true)
      })
    }
  })
  /* eslint-enable complexity */

})

@@ -777,87 +777,257 @@
     
-if (typeOf(this.onScrollUp) === 'function') %7B%0A      this.$().on('ps-scroll-up',
+this._legacyScrollYEndHandler = () =%3E %7B%0A      debounce(this, this%5B'on-scroll-y-end'%5D, debouncePeriod, true)%0A    %7D%0A%0A    this._scrollDownHandler = () =%3E %7B%0A      debounce(this, this.onScrollDown, debouncePeriod, true)%0A    %7D%0A%0A    this._scrollUpHandler =
  () 
@@ -1023,34 +1023,32 @@
 ndler = () =%3E %7B%0A
-  
       debounce(t
@@ -1099,107 +1099,149 @@
     
-  %7D)
+%7D%0A
 %0A    
-%7D%0A%0A    if (typeOf(this.onScrollDown) === 'function') %7B%0A      this.$().on('ps-scroll-down',
+this._scrollYEndHandler = () =%3E %7B%0A      debounce(this, this.onScrollYEnd, debouncePeriod, true)%0A    %7D%0A%0A    this._scrollYStartHandler =
  () 
@@ -1237,34 +1237,32 @@
 ndler = () =%3E %7B%0A
-  
       debounce(t
@@ -1271,36 +1271,38 @@
 s, this.onScroll
-Down
+YStart
 , debouncePeriod
@@ -1305,33 +1305,24 @@
 riod, true)%0A
-      %7D)%0A
     %7D%0A%0A    i
@@ -1344,22 +1344,18 @@
 onScroll
-YStart
+Up
 ) === 'f
@@ -1392,105 +1392,161 @@
 'ps-
-y-reach-start', () =%3E %7B
+scroll-up', this._scrollUpHandler)
 %0A    
+%7D%0A%0A
     
-debounce(this, this.onScrollYStart, debouncePeriod, true)%0A      %7D
+if (typeOf(this.onScrollDown) === 'function') %7B%0A      this.$().on('ps-scroll-down', this._scrollDownHandler
 )%0A  
@@ -1579,19 +1579,21 @@
 nScrollY
-End
+Start
 ) === 'f
@@ -1638,93 +1638,161 @@
 ach-
-end', () =%3E %7B%0A        debounce(this, this.onScrollYEnd, debouncePeriod, true)%0A      %7D
+start', this._scrollYStartHandler)%0A    %7D%0A%0A    if (typeOf(this.onScrollYEnd) === 'function') %7B%0A      this.$().on('ps-y-reach-end', this._scrollYEndHandler
 )%0A  
@@ -2176,139 +2176,445 @@
 d', 
-() =%3E %7B%0A        debounce(this, this%5B'on-scroll-y-end'%5D, debouncePeriod, true)%0A      %7D)%0A    %7D%0A  %7D)%0A  /* eslint-enable complexity */%0A
+this._legacyScrollYEndHandler)%0A    %7D%0A  %7D),%0A  /* eslint-enable complexity */%0A%0A  cleanup: on('willDestroyElement', function () %7B%0A    this.$().off('ps-scroll-down', this._scrollDownHandler)%0A    this.$().off('ps-scroll-up', this._scrollUpHandler)%0A    this.$().off('ps-y-reach-end', this._legacyScrollYEndHandler)%0A    this.$().off('ps-y-reach-end', this._scrollYEndHandler)%0A    this.$().off('ps-y-reach-start', this._scrollYStartHandler)%0A  %7D)
 %0A%7D)%0A

Make sure to unbind events