import { ObservableMap, computed } from 'mobx';
import { each } from 'lodash';
import { CourseListingActions, CourseListingStore } from '../flux/course-listing';
import Course from './course';


class CoursesMap extends ObservableMap {
  @computed get array() {
    return this.values();
  }
}

function onLoaded(courseData) {
  courseData.forEach(cd => coursesMap.set(String(cd.id), new Course(cd)));
}

function mapWhere(condition) {
  const map = new CoursesMap();
  each(coursesMap.values(), c => condition(c) && map.set(c.id, c));
  return map;
}

class AllCoursesMap extends CoursesMap {

  @computed get active() {
    return mapWhere(c => c.is_active);
  }

  @computed get completed() {
    return mapWhere(c => c.hasEnded);
  }

  @computed get future() {
    return mapWhere(c => !c.hasStarted);
  }

  @computed get currentAndFuture() {
    return mapWhere(c => !c.hasEnded);
  }

  bootstrap( courseData, options = {} ) {
    CourseListingActions.loaded(courseData);
    if (options.clear) { this.clear(); }
    onLoaded(courseData);
    CourseListingStore.on('loaded', onLoaded);
    return coursesMap;
  }
}

const coursesMap = new AllCoursesMap();

export default coursesMap;

@@ -45,39 +45,8 @@
 x';%0A
-import %7B each %7D from 'lodash';%0A
 impo
@@ -436,13 +436,8 @@
 ;%0A  
-each(
 cour
@@ -447,23 +447,30 @@
 Map.
-values(), c =%3E 
+forEach(c =%3E %7B%0A    if(
 cond
@@ -481,11 +481,11 @@
 n(c)
- &&
+) %7B
  map
@@ -497,16 +497,23 @@
 c.id, c)
+; %7D%0A  %7D
 );%0A  ret

use forEach instead of lodash each