import Ember from 'ember';
import DS from 'ember-data';

const { computed, inject, RSVP, ObjectProxy, Component } = Ember;
const { service } = inject;
const { PromiseObject } = DS;
const { collect, sort } = computed;

const CourseProxy = ObjectProxy.extend({
  content: null,
  currentUser: null,
  showRemoveConfirmation: false,
  status: computed('content.isPublished', 'content.isScheduled', function(){
    let course = this.get('content');
    if (course.get('isScheduled')) {
      return 's';
    }
    if (course.get('isPublished')) {
      return 'p';
    }

    return 'd';
  }),
  userCanDelete: computed('content', 'currentUser.model.directedCourses.[]', function(){
    let defer = RSVP.defer();
    const course = this.get('content');
    if (course.get('isPublishedOrScheduled')) {
      defer.resolve(false);
    } else {
      this.get('currentUser.userIsDeveloper').then(isDeveloper => {
        if(isDeveloper){
          defer.resolve(true);
        } else {
          this.get('currentUser.model').then(user => {
            user.get('directedCourses').then(directedCourses => {
              defer.resolve(directedCourses.contains(course));
            });
          });
        }
      });
    }

    return PromiseObject.create({
      promise: defer.promise
    });
  })
});
export default Component.extend({
  currentUser: service(),
  courses: [],
  proxiedCourses: computed('courses.[]', function(){
    return this.get('courses').map(course => {
      return CourseProxy.create({
        content: course,
        currentUser: this.get('currentUser')
      });
    });
  }),
  sortBy: 'title',
  sortCourseBy: collect('sortBy'),
  sortedCourses: sort('proxiedCourses', 'sortCourseBy'),
  sortedAscending: computed('sortBy', function(){
    const sortBy = this.get('sortBy');
    return sortBy.search(/desc/) === -1;
  }),
  actions: {
    edit: function(courseProxy){
      this.sendAction('edit', courseProxy.get('content'));
    },
    remove: function(courseProxy){
      this.sendAction('remove', courseProxy.get('content'));
    },
    cancelRemove: function(courseProxy){
      courseProxy.set('showRemoveConfirmation', false);
    },
    confirmRemove: function(courseProxy){
      courseProxy.set('showRemoveConfirmation', true);
    },
    sortBy(what){
      const sortBy = this.get('sortBy');
      if(sortBy === what){
        what += ':desc';
      }
      this.get('setSortBy')(what);
    },
  }
});

@@ -323,16 +323,30 @@
  false,%0A
+  i18n: null,%0A
   status
@@ -406,32 +406,67 @@
 d', function()%7B%0A
+    const i18n = this.get('i18n');%0A
     let course =
@@ -479,32 +479,66 @@
 get('content');%0A
+    let translation = 'general.';%0A
     if (course.g
@@ -568,25 +568,41 @@
     
-return 's
+translation += 'scheduled
 ';%0A    %7D
 %0A   
@@ -597,20 +597,21 @@
 ';%0A    %7D
-%0A   
+ else
  if (cou
@@ -646,20 +646,87 @@
     
-return 'p
+translation += 'published';%0A    %7D else %7B%0A      translation += 'notPublished
 ';%0A
+%0A
     
@@ -743,11 +743,34 @@
 urn 
-'d'
+i18n.t(translation).string
 ;%0A  
@@ -1543,16 +1543,35 @@
 vice(),%0A
+  i18n: service(),%0A
   course
@@ -1577,16 +1577,16 @@
 es: %5B%5D,%0A
-
   proxie
@@ -1626,24 +1626,59 @@
 function()%7B%0A
+    const i18n = this.get('i18n');%0A
     return t
@@ -1742,24 +1742,24 @@
 xy.create(%7B%0A
-
         cont
@@ -1771,16 +1771,30 @@
 course,%0A
+        i18n,%0A
         

Use translations instead of English keys for sorting courses