import { omit } from 'lodash/object';
import BaseAPI from './base.es6.js';
import Comment from '../models/comment.es6.js';
import Link from '../models/comment.es6.js';
import Message from '../models/message.es6.js';

const CONSTRUCTORS = {
  t1: Comment,
  t3: Link,
  t4: Message,
};

export default class Messages extends BaseAPI {
  static dataCacheConfig = undefined;

  get requestCacheRules() {
    return undefined;
  }

  move = this.notImplemented('move');
  copy = this.notImplemented('copy');
  put = this.notImplemented('put');
  patch = this.notImplemented('patch');
  del = this.notImplemented('del');

  formatQuery(query) {
    return {
      ...omit(query, 'thingId'),
      api_type: 'json',
      mark: true,
      thing_id: query.thingId,
    };
  }

  path(method, query={}) {
    const { subreddit, view, thingId } = query;
    if (method === 'get') {
      const sub = subreddit ? `r/${subreddit}/` : '';
      return `${sub}message/${view || 'inbox'}`;
    }
    if (!thingId) { return 'api/compose'; }
    return 'api/comment';
  }

  formatBody(res, req) {
    const { body } = res;

    switch (req.method) {
      case 'GET': {
        if (body) {
          return body.data.children.map(data => {
            const constructor = CONSTRUCTORS[data.kind];
            const thing = new constructor(data.data).toJSON();
            if (constructor === CONSTRUCTORS.t4 &&
                !Array.isArray(thing.replies) &&
                typeof thing.replies === 'object' &&
                Array.isArray(thing.replies.data.children)) {
              thing.replies = thing.replies.data.children.map((m) => {
                return new Message(m.data).toJSON();
              });
            }
            return thing;
          });
        }
      }
      case 'POST': {
        if (body && body.json) {
          const message = body.json.things[0].data;
          return new Message(message).toJSON();
        } else if (body && body.json.errors.length) {
          throw body.json.errors;
        } else {
          return res;
        }
      }
    }
  }
}

@@ -331,102 +331,8 @@
 I %7B%0A
-  static dataCacheConfig = undefined;%0A%0A  get requestCacheRules() %7B%0A    return undefined;%0A  %7D%0A%0A
   mo
@@ -959,22 +959,21 @@
 %0A  %7D%0A%0A  
-format
+parse
 Body(res
@@ -974,19 +974,40 @@
 dy(res, 
-req
+apiResponse, req, method
 ) %7B%0A    
@@ -1037,52 +1037,32 @@
     
-switch (req.method) %7B%0A      case 'GET':
+if (method === 'get')
  %7B%0A
-  
     
@@ -1071,32 +1071,33 @@
 if (
+!
 body) %7B
-%0A          return
+ return; %7D%0A%0A     
  bod
@@ -1116,16 +1116,21 @@
 ren.
-map
+forEach
 (dat
-a
+um
  =%3E 
@@ -1143,31 +1143,25 @@
     
- 
-   
 const 
-constructor =
+thing = new
  CON
@@ -1177,63 +1177,22 @@
 %5Bdat
-a
+um
 .kind%5D
-;%0A            const thing = new constructor
 (dat
-a
+um
 .dat
@@ -1196,36 +1196,33 @@
 data).toJSON();%0A
-    
+%0A
         if (cons
@@ -1221,74 +1221,31 @@
 if (
-constructor === CONSTRUCTORS.
+datum.kind === '
 t4
+'
  &&
-%0A                !Array.isArray(
+ 
 thin
@@ -1257,96 +1257,46 @@
 lies
-)
  &&
-%0A                typeof thing.replies === 'object' &&%0A                Array.isArray(
+ thing.replies.data%0A            && 
 thin
@@ -1319,24 +1319,20 @@
 hildren)
-)
  %7B%0A
-    
+%0A
         
@@ -1385,11 +1385,20 @@
 map(
-(m)
+messageDatum
  =%3E 
@@ -1407,34 +1407,39 @@
             
-    return
+const message =
  new Message
@@ -1440,16 +1440,27 @@
 essage(m
+essageDatum
 .data).t
@@ -1484,52 +1484,89 @@
     
-  %7D);%0A            %7D%0A            return thing
+apiResponse.addModel(message);%0A            return apiResponse.makeRecord(message)
 ;%0A  
@@ -1591,39 +1591,93 @@
   %7D%0A
+%0A  
       
-%7D%0A      case 'POST':
+apiResponse.addResult(thing);%0A      %7D);%0A    %7D else if (method === 'post')
  %7B%0A
-  
     
@@ -1699,26 +1699,24 @@
 ody.json) %7B%0A
-  
         cons
@@ -1761,25 +1761,38 @@
         
-  return 
+apiResponse.addResult(
 new Mess
@@ -1812,24 +1812,25 @@
 toJSON()
+)
 ;%0A      
   %7D else
@@ -1817,26 +1817,24 @@
 N());%0A      
-  
 %7D else if (b
@@ -1875,18 +1875,16 @@
         
-  
 throw bo
@@ -1901,26 +1901,24 @@
 rors;%0A      
-  
 %7D else %7B%0A   
@@ -1926,31 +1926,35 @@
     
-  return res;%0A        %7D
+apiResponse.addResult(res);
 %0A   

Normalize Messages (this one will need more thought, see git log)