import Map from './map';
import { computed, action } from 'mobx';
import { CourseListingActions, CourseListingStore } from '../flux/course-listing';
import Course from './course';
import { once } from 'lodash';

class CoursesMap extends Map {


}

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

const listenForLoad = once(() => {
  CourseListingStore.on('loaded', onLoaded);
});

function mapWhere(condition) {
  const map = new CoursesMap();
  coursesMap.forEach(c => {
    if(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);
  }

  @action addNew(courseData) {
    const course = new Course(courseData);
    this.set(course.id, course);
    CourseListingActions.loaded([courseData]);
    return course;
  }

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

}

const coursesMap = new AllCoursesMap();

export default coursesMap;

@@ -440,165 +440,8 @@
 );%0A%0A
-function mapWhere(condition) %7B%0A  const map = new CoursesMap();%0A  coursesMap.forEach(c =%3E %7B%0A    if(condition(c)) %7B map.set(c.id, c); %7D%0A  %7D);%0A  return map;%0A%7D%0A%0A
 clas
@@ -508,36 +508,38 @@
 () %7B%0A    return 
-mapW
+this.w
 here(c =%3E c.is_a
@@ -584,36 +584,38 @@
 () %7B%0A    return 
-mapW
+this.w
 here(c =%3E c.hasE
@@ -656,36 +656,38 @@
 () %7B%0A    return 
-mapW
+this.w
 here(c =%3E !c.has
@@ -753,12 +753,14 @@
 urn 
-mapW
+this.w
 here
@@ -784,16 +784,95 @@
 );%0A  %7D%0A%0A
+  @computed get nonPreview() %7B%0A    return this.where(c =%3E !c.is_preview);%0A  %7D%0A%0A
   @actio

Return new instance from filters to allow chaining