import Ember from 'ember';
import DS from 'ember-data';

const {
  get,
  getOwner
} = Ember;
const keys = Object.keys || Ember.keys;
const assign = Object.assign || Ember.assign;

export default DS.RESTSerializer.extend({
  
  init: function() {
  	this._super(...arguments);
  	
    let config = getOwner(this).resolveRegistration('config:environment');
  	this.dontsavedefault = config['emberpouch'] && config['emberpouch']['dontsavehasmany'];
  },
  
  _getDontsave(relationship) {
  	return !Ember.isEmpty(relationship.options.dontsave) ? relationship.options.dontsave : this.dontsavedefault;
  },

  _shouldSerializeHasMany: function(snapshot, key, relationship) {
  	let dontsave = this._getDontsave(relationship);
  	let result = !dontsave;
    return result;
  },

  // This fixes a failure in Ember Data 1.13 where an empty hasMany
  // was saving as undefined rather than [].
  serializeHasMany(snapshot, json, relationship) {
  	if (this._shouldSerializeHasMany(snapshot, relationship.key, relationship)) {
	    this._super.apply(this, arguments);
	
	    const key = relationship.key;
	
	    if (!json[key]) {
	      json[key] = [];
	    }
	}
  },

  _isAttachment(attribute) {
    return ['attachment', 'attachments'].indexOf(attribute.type) !== -1;
  },

  serializeAttribute(snapshot, json, key, attribute) {
    this._super(snapshot, json, key, attribute);
    if (this._isAttachment(attribute)) {
      // if provided, use the mapping provided by `attrs` in the serializer
      var payloadKey = this._getMappedKey(key, snapshot.type);
      if (payloadKey === key && this.keyForAttribute) {
        payloadKey = this.keyForAttribute(key, 'serialize');
      }

      // Merge any attachments in this attribute into the `attachments` property.
      // relational-pouch will put these in the special CouchDB `_attachments` property
      // of the document.
      // This will conflict with any 'attachments' attr in the model. Suggest that
      // #toRawDoc in relational-pouch should allow _attachments to be specified
      json.attachments = assign({}, json.attachments || {}, json[payloadKey]); // jshint ignore:line
      json[payloadKey] = keys(json[payloadKey]).reduce((attr, fileName) => {
        attr[fileName] = assign({}, json[payloadKey][fileName]); // jshint ignore:line
        delete attr[fileName].data;
        delete attr[fileName].content_type;
        return attr;
      }, {});
    }
  },

  extractAttributes(modelClass, resourceHash) {
    let attributes = this._super(modelClass, resourceHash);
    let modelAttrs = get(modelClass, 'attributes');
    modelClass.eachTransformedAttribute(key => {
      let attribute = modelAttrs.get(key);
      if (this._isAttachment(attribute)) {
        // put the corresponding _attachments entries from the response into the attribute
        let fileNames = keys(attributes[key]);
        fileNames.forEach(fileName => {
          attributes[key][fileName] = resourceHash.attachments[fileName];
        });
      }
    });
    return attributes;
  },
  
  extractRelationships(modelClass) {
  	let relationships = this._super(...arguments);

  	modelClass.eachRelationship((key, relationshipMeta) => {
  	  if (relationshipMeta.kind === 'hasMany' && this._getDontsave(relationshipMeta) && !!relationshipMeta.options.async) {
  	  	relationships[key] = { links: { related: key } };
  	  }
  	});
  	
  	return relationships;
  }
});

@@ -178,22 +178,24 @@
 n;%0A%0A
-export default
+var Serializer =
  DS.
@@ -218,18 +218,16 @@
 xtend(%7B%0A
-  
 %0A  init:
@@ -270,20 +270,17 @@
 ments);%0A
-  %09
 %0A
+
     let 
@@ -442,19 +442,17 @@
 %5D;%0A  %7D,%0A
-  
 %0A
+
   _getDo
@@ -594,17 +594,16 @@
   %7D,%0A%0A  
-_
 shouldSe
@@ -1047,18 +1047,17 @@
 ments);%0A
-%09
 %0A
+
 %09    con
@@ -1083,17 +1083,16 @@
 ip.key;%0A
-%09
 %0A%09    if
@@ -3023,11 +3023,9 @@
  %7D,%0A
-  
 %0A
+
   ex
@@ -3365,12 +3365,9 @@
 %7D);%0A
-  %09
 %0A
+
   %09r
@@ -3394,9 +3394,476 @@
 %0A  %7D
+,%0A
 %0A%7D);%0A
+%0A// DEPRECATION: The private method _shouldSerializeHasMany has been promoted to the public API%0A// See https://www.emberjs.com/deprecations/ember-data/v2.x/#toc_jsonserializer-shouldserializehasmany%0A if( ! DS.JSONSerializer.prototype.shouldSerializeHasMany ) %7B%0A   Serializer.reopen(%7B%0A     _shouldSerializeHasMany( snapshot, key, relationship )%7B%0A       return this.shouldSerializeHasMany( snapshot, key, relationship );%0A     %7D%0A   %7D);%0A %7D%0A%0A export default Serializer;%0A

Fix _shouldSerializeHasMany deprecation