<commit_before>/* global Ember,DS */
var get = Ember.get;
var isNone = Ember.isNone;
var HOST = /(^https?:\/\/.*?)(\/.*)/;

DS.JsonApiSerializer = DS.RESTSerializer.extend({

  shouldDasherizeKeys: true,

  primaryRecordKey: 'data',
  sideloadedRecordsKey: 'included',
  relationshipKey: 'self',
  relatedResourceKey: 'related',

  keyForAttribute: function(key) {
    return this.shouldDasherizeKeys ? Ember.String.dasherize(key) : key;
  },
  keyForRelationship: function(key) {
    return this.shouldDasherizeKeys ? Ember.String.dasherize(key) : key;
  },
  keyForSnapshot: function(snapshot) {
    return this.shouldDasherizeKeys ? Ember.String.dasherize(snapshot.typeKey) : snapshot.typeKey;
  },

  /**
   * Flatten links
   */
  normalize: function(type, hash, prop) {
    var json = {};
    for (var key in hash) {
      // This is already normalized
      if (key === 'links') {
        json[key] = hash[key];
        continue;
      }

      if (key === 'attributes') {
        for (var attributeKey in hash[key]) {
          var camelizedKey = Ember.String.camelize(attributeKey);
          json[camelizedKey] = hash[key][attributeKey];
        }
        continue;
      }
      var camelizedKey = Ember.String.camelize(key);
      json[camelizedKey] = hash[key];
    }

    return this._super(type, json, prop);
  },

  /**
   * Extract top-level "meta" & "links" before normalizing.
   */
  normalizePayload: function(payload) {
    if(!payload) { return {}; }
    var data = payload[this.primaryRecordKey];
    if (data) {
      if(Ember.isArray(data)) {
        this.extractArrayData(data, payload);
      } else {
        this.extractSingleData(data, payload);
      }
      delete payload[this.primaryRecordKey];
    }
    if (payload.meta) {
      this.extractMeta(payload.meta);
      delete payload.meta;
    }
    if (payload.links) {
      // FIXME Need to handle top level links, like pagination
      //this.extractRelationships(payload.links, payload);
      delete payload.links;
    }
    if (payload[this.sideloadedRecordsKey]) {
      this.extractSideloaded(payload[this.sideloadedRecordsKey]);
      delete payload[this.sideloadedRecordsKey];
    }

    return payload;
  },

  extractArray: function(store, type, arrayPayload, id, requestType) {
    if(Ember.isEmpty(arrayPayload[this.primaryRecordKey])) { return Ember.A(); }
    return this._super(store, type, arrayPayload, id, requestType);
  },

  /**
   * Extract top-level "data" containing a single primary data<commit_msg>Support extractMeta to store the meta and the links<commit_after>/* global Ember,DS */
var get = Ember.get;
var isNone = Ember.isNone;
var HOST = /(^https?:\/\/.*?)(\/.*)/;

DS.JsonApiSerializer = DS.RESTSerializer.extend({

  shouldDasherizeKeys: true,
  shouldStoreLinksInMeta: true,

  primaryRecordKey: 'data',
  sideloadedRecordsKey: 'included',
  relationshipKey: 'self',
  relatedResourceKey: 'related',

  keyForAttribute: function(key) {
    return this.shouldDasherizeKeys ? Ember.String.dasherize(key) : key;
  },
  keyForRelationship: function(key) {
    return this.shouldDasherizeKeys ? Ember.String.dasherize(key) : key;
  },
  keyForSnapshot: function(snapshot) {
    return this.shouldDasherizeKeys ? Ember.String.dasherize(snapshot.typeKey) : snapshot.typeKey;
  },

  /**
   * Flatten links
   */
  normalize: function(type, hash, prop) {
    var json = {};
    for (var key in hash) {
      // This is already normalized
      if (key === 'links') {
        json[key] = hash[key];
        continue;
      }

      if (key === 'attributes') {
        for (var attributeKey in hash[key]) {
          var camelizedKey = Ember.String.camelize(attributeKey);
          json[camelizedKey] = hash[key][attributeKey];
        }
        continue;
      }
      var camelizedKey = Ember.String.camelize(key);
      json[camelizedKey] = hash[key];
    }

    return this._super(type, json, prop);
  },

  /**
   * Extract top-level "meta" & "links" before normalizing.
   */
  normalizePayload: function(payload) {
    if(!payload) { return {}; }
    var data = payload[this.primaryRecordKey];
    if (data) {
      if(Ember.isArray(data)) {
        this.extractArrayData(data, payload);
      } else {
        this.extractSingleData(data, payload);
      }
      delete payload[this.primaryRecordKey];
    }
    if (payload.meta) {
      delete payload.meta;
    }
    if (payload.links) {
      delete payload.links;
    }
    if (payload[this.sideloadedRecordsKey]) {
      this.extractSideloaded(payload[this.sideloadedRecordsKey]);
      delete payload[this.sideloadedRecordsKey];
    }

    return payload;
  },

  /**
  * Extract meta. Also store links into the meta data for type if requested.
  */
  extractMeta: function(store, typeClass, payload) {
    if (!payload) {
      return;
    }

    let metaForType = store.metadataFor(typeClass);
    if (payload.meta) {
      Ember.merge(metaForType, payload.meta);
    }
    if (this.shouldStoreLinksInMeta && payload.links) {
      metaForType.links = {};
      Ember.merge(metaForType.links, payload.links);
    }
    store.setMetadataFor(typeClass, metaForType);
  },

  extractArray: function(store, type, arrayPayload, id, requestType) {
    if(Ember.isEmpty(arrayPayload[this.primaryRecordKey])) { return Ember.A(); }
    return this._super(store, type, arrayPayload, id, requestType);
  },

  /**
   * Extract top-level "data" containing a single primary data
{'fraction_non_alphanumeric': 0.1335169880624426, 'fraction_numerical': 0.0, 'mean_word_length': 3.0014695077149156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5365542', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1735, 'n_words': 354}