import Ember from 'ember';

const {
  computed,
  defineProperty,
  inject: { service },
  isEmpty,
  run
} = Ember;

/**
 * Connect a Route to stateToComputed and dispatchToActions.
 *
 * This is the nearly the same implementation as ember-redux/components/connect,
 * except we pass `params` to stateToComputed and dispatchToActions.
 */
export default (stateToComputed=() => ({}), dispatchToActions=() => ({})) => {

  return Route => {

    return Route.extend({

      redux: service(),

      getCurrentParams(controller) {
        return Object.assign({},
          this.paramsFor(this.routeName), controller.getProperties(controller.queryParams)
        );
      },

      setupController(controller) {
        const redux = this.get('redux');
        const params = this.getCurrentParams(controller);
        const props = stateToComputed(redux.getState(), params);

        for (let name in props) {
          defineProperty(controller, name, computed(() =>
            stateToComputed(redux.getState(), this.getCurrentParams(controller))[name]
          ).property().readOnly());
        }

        if (!isEmpty(Object.keys(props))) {
          this.unsubscribe = redux.subscribe(() => {
            run(() => this.handleChange(controller));
          });
          for (let param in params) {
            if (controller.queryParams.includes(param)) {
              controller.addObserver(param, () => {
                run(() => this.handleChange(controller));
              });
            }
          }
        }

        controller.actions = Object.assign({},
          controller.actions, dispatchToActions(redux.dispatch.bind(redux), params)
        );

        this._super(...arguments);
      },

      handleChange(controller) {
        const redux = this.get('redux');
        const params = Object.assign({},
          this.paramsFor(this.routeName), controller.getProperties(controller.queryParams)
        );
        const props = stateToComputed(redux.getState(), params);

        for (let name in props) {
          if (controller.get(name) !== props[name]) {
            controller.notifyPropertyChange(name);
          }
        }
      },

      deactivate() {
        this._super(...arguments);
        if (this.unsubscribe) {
          this.unsubscribe();
          this.unsubscribe = null;
        }
      }
    });
  };
};

@@ -877,39 +877,50 @@
         
-for (let name in props)
+Object.keys(props).forEach(name =%3E
  %7B%0A     
@@ -1096,32 +1096,34 @@
 ly());%0A        %7D
+);
 %0A%0A        if (!i
@@ -1287,33 +1287,44 @@
     
-for (let param in 
+Object.keys(params).forEach(
 param
-s)
+ =%3E
  %7B%0A 
@@ -1525,32 +1525,34 @@
    %7D%0A          %7D
+);
 %0A        %7D%0A%0A    
@@ -2030,31 +2030,42 @@
     
-for (let name in props)
+Object.keys(props).forEach(name =%3E
  %7B%0A 
@@ -2172,32 +2172,32 @@
 e);%0A          %7D%0A
-
         %7D%0A      
@@ -2181,32 +2181,34 @@
      %7D%0A        %7D
+);
 %0A      %7D,%0A%0A     

use Object.keys instead of for/in