import Ember from 'ember';

export function initialize(instance) {
  const config = instance.container.lookupFactory('config:environment');

  // Default to true when not set
  let _includeRouteName = true;
  if (config['ember-body-class'] && config['ember-body-class'].includeRouteName === false) {
    _includeRouteName = false;
  }

  Ember.Route.reopen({
    classNames: [],
    bodyClasses: [], // Backwards compatibility

    addClasses: Ember.on('activate', function() {
      const $body = Ember.$('body');
      ['bodyClasses', 'classNames'].forEach((classes) => {
        this.get(classes).forEach(function(klass) {
          $body.addClass(klass);
        });
      });

      if (_includeRouteName) {
        let routeName = this.get('routeName').split('.').get('lastObject');
        $body.addClass(routeName);
      }
    }),

    removeClasses: Ember.on('deactivate', function() {
      const $body = Ember.$('body');
      ['bodyClasses', 'classNames'].forEach((classes) => {
        this.get(classes).forEach(function(klass) {
          $body.removeClass(klass);
        });
      });

      if (_includeRouteName) {
        let routeName = this.get('routeName').split('.').get('lastObject');
        $body.removeClass(routeName);
      }
    }),
  });
}

export default {
  name: 'body-class',
  initialize: initialize
};

@@ -62,21 +62,268 @@
 ce) %7B%0A  
-const
+var config;%0A  if (instance.resolveRegistration) %7B%0A    // Ember 2.1+%0A    // http://emberjs.com/blog/2015/08/16/ember-2-1-beta-released.html#toc_registry-and-container-reform%0A    config = instance.resolveRegistration('config:environment');%0A  %7D else %7B%0A   
  config 
@@ -379,16 +379,20 @@
 nment');
+%0A  %7D
 %0A%0A  // D
@@ -672,16 +672,150 @@
 bility%0A%0A
+    _getRouteName() %7B%0A      const nameParts = this.get('routeName').split('.');%0A      return nameParts%5BnameParts.length - 1%5D;%0A    %7D,%0A%0A
     addC
@@ -1095,84 +1095,8 @@
 ) %7B%0A
-        let routeName = this.get('routeName').split('.').get('lastObject');%0A
     
@@ -1110,33 +1110,42 @@
 dy.addClass(
-r
+this._getR
 outeName
 );%0A      %7D%0A 
@@ -1124,32 +1124,34 @@
 is._getRouteName
+()
 );%0A      %7D%0A    %7D
@@ -1451,84 +1451,8 @@
 ) %7B%0A
-        let routeName = this.get('routeName').split('.').get('lastObject');%0A
     
@@ -1473,25 +1473,34 @@
 veClass(
-r
+this._getR
 outeName
 );%0A     
@@ -1491,16 +1491,18 @@
 outeName
+()
 );%0A     

Use updated registry in Ember 2.1