import db from './db'
import map from 'lodash/map'
import omit from 'lodash/omit'

const courseCache = new Map()
// Gets a course from the database.
// @param {Number} clbid - a class/lab ID
// @param {Number} term - a course term
// @param {Object} fabrications - a (clbid, course) object of fabrications
// @returns {Promise} - TreoDatabasePromise
// @fulfill {Object} - the course object, potentially with an embedded error message.
export async function getCourse({clbid, term}, fabrications={}) {
	if (clbid in fabrications) {
		return fabrications[clbid]
	}

	if (courseCache.has(clbid)) {
		return await courseCache.get(clbid)
	}

	let promise = db.store('courses')
		.index('clbid')
		.get(clbid)
		.then(course => course || {clbid, term, error: `Could not find ${clbid}`})
		.then(course => omit(course, ['profWords', 'words', 'sourcePath']))
		.catch(error => ({clbid, term, error: error.message}))

	courseCache.set(clbid, promise)

	let course = await courseCache.get(clbid)

	courseCache.delete(clbid)

	return course
}
// export function getCourse({clbid, term}) {
// 	return db.store('courses')
// 		.index('clbid')
// 		.get(clbid)
// 		.then(course => course || {clbid, term, error: `Could not find ${clbid}`})
// 		.catch(error => ({clbid, term, error: error.message}))
// }


/**
 * Takes a list of clbids, and returns a list of the course objects for those
 * clbids.
 *
 * @param {Number[]} clbids - a list of class/lab IDs
 * @param {Object} fabrications - a list of fabrications
 * @returns {Promise} - a promise for the course data
 * @fulfill {Object[]} - the courses.
 */
export function getCourses(clbids, fabrications) {
	return Promise.all(map(clbids, c => getCourse(c, fabrications)))
}

@@ -100,16 +100,26 @@
 e = 
-new Map(
+Object.create(null
 )%0A//
@@ -588,16 +588,27 @@
 ache.has
+OwnProperty
 (clbid))
@@ -619,22 +619,16 @@
 %09return 
-await 
 courseCa
@@ -630,27 +630,23 @@
 rseCache
-.get(
+%5B
 clbid
-)
+%5D
 %0A%09%7D%0A%0A%09le
@@ -930,27 +930,25 @@
 ache
-.set(
+%5B
 clbid
-,
+%5D =
  promise
 )%0A%0A%09
@@ -943,17 +943,16 @@
  promise
-)
 %0A%0A%09let c
@@ -980,22 +980,25 @@
 ache
-.get(
+%5B
 clbid
-)
+%5D
 %0A%0A%09
+delete 
 cour
@@ -1008,22 +1008,15 @@
 ache
-.delete(
+%5B
 clbid
-)
+%5D
 %0A%0A%09r

remove use of Map from get-courses