var log = require('debug')('node-map-reduce:controller:Chunk');

var Chunk = function(id, rawChunk) {
  this.id_ = id  || (function() { throw new Error('id not provided'); })();

  // kept during the map stage JIC we want to recover from failed mapper
  this.rawChunk_ = id ||  (function() { throw new Error('rawChunk not provided'); })();

  this.mapper_ = null;
  this.reductionKeys_ = null;
  this.isDone_ = false;
};

Chunk.prototype = {
  id: function () { return this.id_; },

  toJson: function () {
    return {
      id: this.id_,
      status: this.status_
    }
  },

  getStatus: function() {
    if (this.mapper_) {
      return Chunk.Status.MAPPING;
    } else if (this.reductionKeys_) {
      return Chunk.Status.REDUCING;
    } else {
      return Chunk.Status.DONE;
    }
  },

  canDelete: function() {
    return this.isDone_;
  },

  setMapping: function(mapper) {
    this.mapper_ = mapper;
  },

  setReducing: function(keys) {
    this.markMapperDone_();
    this.rawChunk_ = null;
    this.reductionKeys_ = keys;
  },

  setDone: function() {
    this.markMapperDone_();
    this.rawChunk_ = null;
    this.reductionKeys_ = null;
    this.isDone_ = true;
  },

  markMapperDone_: function() {
    if (this.mapper_) {
      this.mapper_.becameAvailable();
      this.mapper_ = null;
    }
  }
};

Chunk.Status = {
  MAPPING: 'MAPPING',
  REDUCING: 'REDUCING',
  DONE: 'DONE'
};

module.exports = Chunk;

@@ -268,75 +268,16 @@
 _ = 
-id %7C%7C  (function() %7B throw new Error('rawChunk not provided'); %7D)()
+rawChunk
 ;%0A%0A 
@@ -478,35 +478,8 @@
 d_,%0A
-      status: this.status_%0A
     
@@ -861,36 +861,8 @@
 ) %7B%0A
-    this.markMapperDone_();%0A
     
@@ -916,16 +916,69 @@
 = keys;%0A
+    this.markMapperDone_();%0A    this.mapper_ = null;%0A
   %7D,%0A%0A  
@@ -1003,36 +1003,8 @@
 ) %7B%0A
-    this.markMapperDone_();%0A
     
@@ -1083,16 +1083,69 @@
 = true;%0A
+    this.markMapperDone_();%0A    this.mapper_ = null;%0A
   %7D,%0A%0A  
@@ -1244,122 +1244,13 @@
     
-  this.mapper_ = null;%0A    %7D%0A  %7D%0A%7D;%0A%0AChunk.Status = %7B%0A  MAPPING: 'MAPPING',%0A  REDUCING: 'REDUCING',%0A  DONE: 'DONE'
+%7D%0A  %7D
 %0A%7D;%0A

tidy up chunk model