import Mixin from '@ember/object/mixin';
import { computed, get } from '@ember/object';
import { reads } from '@ember/object/computed';
import { decamelize } from '@ember/string';
import { schedule } from '@ember/runloop';

/**
 * Register Google Maps events on any map component.
 *
 * The mixin filters the component attributes for those that begin with `on` and
 * are not on the `_ignoreAttrs` list. The attribute name is decamelize and the
 * `on` prefixed is dropped to generate the event name. The attribute function
 * is then bound to that event by name.
 *
 * For example, passing `onClick` will add a `click` event that will
 * call the function passed in as `onClick`.
 */
export default Mixin.create({
  domComponent: reads('mapComponent'),

  events: computed(function() {
    const attrs = Object.keys(this.attrs);
    return attrs.filter((attr) => this._filterEventsByName(attr));
  }),

  _filterEventsByName(attr) {
    return attr.startsWith('on') && get(this, '_ignoreAttrs').indexOf(attr) < 0;
  },

  willDestroyElement() {
    const domComponent = get(this, 'domComponent');
    if (domComponent && typeof google !== 'undefined') {
      google.maps.event.clearInstanceListeners(domComponent);
    }
    this._super(...arguments);
  },

  registerEvents() {
    const eventsToRegister = get(this, 'events');
    eventsToRegister.forEach((event) => {
      const eventName = decamelize(event).slice(3);
      const domComponent = get(this, 'domComponent');
      google.maps.event.addDomListener(domComponent, eventName, (e) => {
        if (e && e.stopPropagation) { e.stopPropagation(); }
        schedule('actions',
          this,
          event,
          {
            event: e,
            eventName: eventName,
            target: domComponent,
            publicAPI: this.publicAPI,
            map: get(this, 'map')
          }
        );
      });
    });
  }
});

@@ -176,51 +176,8 @@
 ng';
-%0Aimport %7B schedule %7D from '@ember/runloop';
 %0A%0A/*
@@ -667,27 +667,27 @@
 ate(%7B%0A  
-domComponen
+_eventTarge
 t: reads
@@ -1002,35 +1002,34 @@
 %7B%0A    const 
-domComponen
+eventTarge
 t = get(this
@@ -1027,35 +1027,35 @@
  get(this, '
-domComponen
+_eventTarge
 t');%0A    if 
@@ -1055,27 +1055,26 @@
     if (
-domComponen
+eventTarge
 t && typ
@@ -1150,27 +1150,26 @@
 steners(
-domComponen
+eventTarge
 t);%0A    
@@ -1308,21 +1308,22 @@
 orEach((
-event
+action
 ) =%3E %7B%0A 
@@ -1356,21 +1356,22 @@
 amelize(
-event
+action
 ).slice(
@@ -1386,27 +1386,26 @@
   const 
-domComponen
+eventTarge
 t = get(
@@ -1415,24 +1415,25 @@
 s, '
-domComponen
+_eventTarge
 t');%0A
+%0A
     
@@ -1467,27 +1467,26 @@
 istener(
-domComponen
+eventTarge
 t, event
@@ -1492,17 +1492,27 @@
 tName, (
-e
+googleEvent
 ) =%3E %7B%0A 
@@ -1527,13 +1527,21 @@
 f (e
+vent
  && e
+vent
 .sto
@@ -1555,18 +1555,32 @@
 ation) %7B
- e
+%0A          event
 .stopPro
@@ -1590,27 +1590,36 @@
 ation();
+%0A       
  %7D%0A
+%0A
         
 schedule
@@ -1614,60 +1614,114 @@
     
-schedule('actions',%0A          this,%0A          event,
+if (googleEvent && googleEvent.stop) %7B%0A          googleEvent.stop();%0A        %7D%0A%0A        get(this, action)(
 %0A   
@@ -1746,19 +1746,41 @@
    event
-: e
+,%0A            googleEvent
 ,%0A      
@@ -1831,19 +1831,18 @@
 et: 
-domComponen
+eventTarge
 t,%0A 

Rename domComponent to eventTarget and improve event handling