import { AssessmentItem, uuid4, resolveUpdater } from 'shared/data/resources';

/**
 * Load all assessment items belonging to a content node.
 */
export function loadNodeAssessmentItems(context, nodeId) {
  return loadAssessmentItems(context, { contentnode: nodeId });
}

export function loadAssessmentItems(context, params = {}) {
  return AssessmentItem.where(params).then(assessmentItems => {
    assessmentItems.forEach(assessmentItem => {
      context.commit('ADD_ASSESSMENTITEM', assessmentItem);
    });
    return assessmentItems;
  });
}

export function loadAssessmentItem(context, id) {
  return AssessmentItem.get(id)
    .then(assessmentItem => {
      context.commit('ADD_ASSESSMENTITEM', assessmentItem);
      return assessmentItem;
    })
    .catch(() => {
      return;
    });
}

export function addAssessmentItem(context, assessmentItem) {
  // API accepts answers and hints as strings
  const stringifiedAssessmentItem = {
    ...assessmentItem,
    answers: JSON.stringify(assessmentItem.answers || []),
    hints: JSON.stringify(assessmentItem.hints || []),
  };

  return AssessmentItem.put(stringifiedAssessmentItem).then(assessment_id => {
    context.commit('ADD_ASSESSMENTITEM', {
      ...assessmentItem,
      assessment_id,
    });
  });
}

export function updateAssessmentItem(context, assessmentItem) {
  // Don't wait for IndexedDB update to be finished before
  // commiting update to store on purpose to allow for immediate
  // updates (needed when typing text to answers or hints editor
  // fast for example)
  context.commit('UPDATE_ASSESSMENTITEM', assessmentItem);

  // API accepts answers and hints as strings
  const stringifiedAssessmentItem = {
    ...assessmentItem,
    answers: JSON.stringify(assessmentItem.answers || []),
    hints: JSON.stringify(assessmentItem.hints || []),
  };
  return AssessmentItem.put(stringifiedAssessmentItem);
}

export function copyAssessmentItems(context, { params, updater }) {
  let itemMap = {};
  updater = resolveUpdater(updater);
  return AssessmentItem.where(params)
    .then(assessmentItems => {
      if (!assessmentItems.length) {
        return [];
      }

      return AssessmentItem.bulkCopy(assessmentItems, assessmentItem => {
        const id = uuid4();
        itemMap[assessmentItem.id] = id;
        return item => ({ ...updater(item), id });
      });
    })
    .then(newAssessmentItems => {
      if (!newAssessmentItems.length) {
        return [];
      }

      context.commit('ADD_ASSESSMENTITEMS', newAssessmentItems);

      return context
        .dispatch('file/copyFiles', {
          params: {
            assessment_items: Object.keys(itemMap),
          },
          updater: file => {
            return {
              assessment_item: itemMap[file.assessment_item],
            };
          },
        })
        .then(() => newAssessmentItems);
    });
}

export function deleteAssessmentItem(context, assesmentItem) {
  return AssessmentItem.delete(assesmentItem.assessment_id).then(() => {
    context.commit('DELETE_ASSESSMENTITEM', assesmentItem);
  });
}

@@ -2302,49 +2302,76 @@
     
-return item =%3E (%7B ...updater(item)
+const base = updater(assessmentItem);%0A        return %7B ...base
 , id %7D
-)
 ;%0A  
@@ -2592,16 +2592,27 @@
 ispatch(
+%0A          
 'file/co
@@ -2620,19 +2620,31 @@
 yFiles',
+%0A         
  %7B%0A
+  
         
@@ -2655,16 +2655,18 @@
 rams: %7B%0A
+  
         
@@ -2711,32 +2711,34 @@
 Map),%0A          
+  
 %7D,%0A          upd
@@ -2734,16 +2734,18 @@
         
+ 
+ 
 updater:
@@ -2759,32 +2759,34 @@
 %3E %7B%0A            
+  
 return %7B%0A       
@@ -2778,16 +2778,18 @@
 eturn %7B%0A
+  
         
@@ -2854,19 +2854,23 @@
         
+  
 %7D;%0A
+  
         
@@ -2878,25 +2878,62 @@
  %7D,%0A        
-%7D
+  %7D,%0A          %7B root: true %7D%0A        
 )%0A        .t

Fix assessment item copy updater