import Ember from 'ember';

export default Ember.Mixin.create({
  resizeEventsEnabled: true,
  resizeDebouncedEventsEnabled: true,

  _oldViewWidth: null,
  _oldViewHeight: null,
  _oldViewWidthDebounced: null,
  _oldViewHeightDebounced: null,
  resizeWidthSensitive: true,
  resizeHeightSensitive: false,

  didInsertElement() {
    this._super(...arguments);
    if (this.get('resizeEventsEnabled')) {
      this.get('resizeService').on('didResize', evt => {
        this._handleResizeEvent(evt);
      });
    }
    if (this.get('resizeDebouncedEventsEnabled')) {
      this.get('resizeService').on('debouncedDidResize', evt => {
        this._handleDebouncedResizeEvent(evt);
      });
    }
  },

  didResize(/*width, height, evt*/) {}, // Overridden in subclass
  debouncedDidResize(/*width, height, evt*/) {}, // Overridden in subclass

  _getComponentSize() {
    return this.$()[0].getClientRects()[0];
  },

  _handleResizeEvent(evt) {
    const w = Math.floor(this._getComponentSize().width);
    const h = Math.floor(this._getComponentSize().height);
    if ((this.get('resizeWidthSensitive') && (this.get('_oldViewWidth') !== w)) ||
      (this.get('resizeHeightSensitive') && (this.get('_oldViewHeight') !== h))) {
      this.didResize(w, h, evt);
      this.setProperties({
        _oldViewWidth: w,
        _oldViewHeight: h
      });
    }
  },

  _handleDebouncedResizeEvent(evt) {
    const w = Math.floor(this._getComponentSize().width);
    const h = Math.floor(this._getComponentSize().height);
    if ((this.get('resizeWidthSensitive') && (this.get('_oldViewWidthDebounced') !== w)) ||
      (this.get('resizeHeightSensitive') && (this.get('_oldViewHeightDebounced') !== h))) {
      this.debouncedDidResize(w, h, evt);
      this.setProperties({
        _oldViewWidthDebounced: w,
        _oldViewHeightDebounced: h
      });
    }
  }
});

@@ -445,32 +445,21 @@
 esize', 
-evt =%3E %7B%0A       
+this,
  this._h
@@ -470,38 +470,24 @@
 eResizeEvent
-(evt);%0A      %7D
 );%0A    %7D%0A   
@@ -596,71 +596,395 @@
 e', 
-evt =%3E %7B%0A        this._handleDebouncedResizeEvent(evt);%0A      %7D
+this, this._handleDebouncedResizeEvent);%0A    %7D%0A  %7D,%0A%0A  willDestroyElement() %7B%0A    this._super(...arguments);%0A    if (this.get('resizeEventsEnabled')) %7B%0A      this.get('resizeService').off('didResize', this, this._handleResizeEvent);%0A    %7D%0A    if (this.get('resizeDebouncedEventsEnabled')) %7B%0A      this.get('resizeService').off('debouncedDidResize', this, this._handleDebouncedResizeEvent
 );%0A 

Clean up events when removing component