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

export default DS.JSONSerializer.extend({

  _shouldSerializeHasMany: function (snapshot, key, relationship) {
    var relationshipType = snapshot.type.determineRelationshipType(relationship, this.store);
    if (this._mustSerialize(key)) {
      return true;
    }
    return this._canSerialize(key) &&
      (relationshipType === 'manyToNone' ||
        relationshipType === 'manyToMany' ||
        relationshipType === 'manyToOne');
  },  

  // Omit the unknown hasMany relationships of pushed record
  // (see https://github.com/emberjs/data/issues/3760)
  // TODO: this override will be unecessary after merge of the following PR:
  // https://github.com/emberjs/data/pull/3765
  serializeHasMany: function(snapshot, json, relationship) {
    var key = relationship.key;

    if (this._shouldSerializeHasMany(snapshot, key, relationship)) {
      var hasMany = snapshot.hasMany(key, { ids: true });
      if (hasMany !== undefined) {
        // if provided, use the mapping provided by `attrs` in
        // the serializer
        var payloadKey = this._getMappedKey(key);
        if (payloadKey === key && this.keyForRelationship) {
          payloadKey = this.keyForRelationship(key, "hasMany", "serialize");
        }

        json[payloadKey] = hasMany;
        // TODO support for polymorphic manyToNone and manyToMany relationships
      }
    }
  },

  // Remove the undefined hasMany relationships which will fail at normalization
  // (see https://github.com/emberjs/data/issues/3736)
  // TODO: this override will be unecessary after merge of the following PR:
  // https://github.com/emberjs/data/pull/3747
  extractRelationships: function(modelClass, resourceHash) {
    let relationships = {};

    modelClass.eachRelationship((key, relationshipMeta) => {
      let relationship = null;
      let relationshipKey = this.keyForRelationship(key, relationshipMeta.kind, 'deserialize');
      if (resourceHash.hasOwnProperty(relationshipKey)) {
        let data = null;
        let relationshipHash = resourceHash[relationshipKey];
        if (relationshipMeta.kind === 'belongsTo') {
          data = this.extractRelationship(relationshipMeta.type, relationshipHash);
        } else if (relationshipMeta.kind === 'hasMany') {
          data = Ember.isNone(relationshipHash) ? null : relationshipHash.map((item) => this.extractRelationship(relationshipMeta.type, item));
        }
        relationship = { data };
      }

      let linkKey = this.keyForLink(key, relationshipMeta.kind);
      if (resourceHash.links && resourceHash.links.hasOwnProperty(linkKey)) {
        let related = resourceHash.links[linkKey];
        relationship = relationship || {};
        relationship.links = { related };
      }

      if (relationship) {
        relationships[key] = relationship;
      }
    });

    return relationships;
  }
});

@@ -493,2410 +493,10 @@
 %0A  %7D
-,
   
-%0A%0A  // Omit the unknown hasMany relationships of pushed record%0A  // (see https://github.com/emberjs/data/issues/3760)%0A  // TODO: this override will be unecessary after merge of the following PR:%0A  // https://github.com/emberjs/data/pull/3765%0A  serializeHasMany: function(snapshot, json, relationship) %7B%0A    var key = relationship.key;%0A%0A    if (this._shouldSerializeHasMany(snapshot, key, relationship)) %7B%0A      var hasMany = snapshot.hasMany(key, %7B ids: true %7D);%0A      if (hasMany !== undefined) %7B%0A        // if provided, use the mapping provided by %60attrs%60 in%0A        // the serializer%0A        var payloadKey = this._getMappedKey(key);%0A        if (payloadKey === key && this.keyForRelationship) %7B%0A          payloadKey = this.keyForRelationship(key, %22hasMany%22, %22serialize%22);%0A        %7D%0A%0A        json%5BpayloadKey%5D = hasMany;%0A        // TODO support for polymorphic manyToNone and manyToMany relationships%0A      %7D%0A    %7D%0A  %7D,%0A%0A  // Remove the undefined hasMany relationships which will fail at normalization%0A  // (see https://github.com/emberjs/data/issues/3736)%0A  // TODO: this override will be unecessary after merge of the following PR:%0A  // https://github.com/emberjs/data/pull/3747%0A  extractRelationships: function(modelClass, resourceHash) %7B%0A    let relationships = %7B%7D;%0A%0A    modelClass.eachRelationship((key, relationshipMeta) =%3E %7B%0A      let relationship = null;%0A      let relationshipKey = this.keyForRelationship(key, relationshipMeta.kind, 'deserialize');%0A      if (resourceHash.hasOwnProperty(relationshipKey)) %7B%0A        let data = null;%0A        let relationshipHash = resourceHash%5BrelationshipKey%5D;%0A        if (relationshipMeta.kind === 'belongsTo') %7B%0A          data = this.extractRelationship(relationshipMeta.type, relationshipHash);%0A        %7D else if (relationshipMeta.kind === 'hasMany') %7B%0A          data = Ember.isNone(relationshipHash) ? null : relationshipHash.map((item) =%3E this.extractRelationship(relationshipMeta.type, item));%0A        %7D%0A        relationship = %7B data %7D;%0A      %7D%0A%0A      let linkKey = this.keyForLink(key, relationshipMeta.kind);%0A      if (resourceHash.links && resourceHash.links.hasOwnProperty(linkKey)) %7B%0A        let related = resourceHash.links%5BlinkKey%5D;%0A        relationship = relationship %7C%7C %7B%7D;%0A        relationship.links = %7B related %7D;%0A      %7D%0A%0A      if (relationship) %7B%0A        relationships%5Bkey%5D = relationship;%0A      %7D%0A    %7D);%0A%0A    return relationships;%0A  %7D
 %0A%7D);

Remove workarounds