import DS from "ember-data";
import UTF8 from "npm:utf-8";

function bin2String(array) {
  if (array === undefined){
    return "";
  } else {
    return UTF8.getStringFromBytes(array);
  }
}

export default DS.Serializer.extend({
  normalizeResponse: function(store, type, payload, id, requestType) {
    var jsonapiPayload = {};
    if (requestType === "findAll") {
      jsonapiPayload.data = [];
      payload.forEach(function(element) {
        var resource = {};
        resource.id = element['Key'];
        resource.type = "article";
        resource.attributes = {};
        resource.attributes.body = bin2String(element['Body']);
        jsonapiPayload.data.pushObject(resource);
      });
    } else if (requestType === "find") {
      // TODO: Json-Api ify
      payload['body'] = bin2String(payload['Body']);
    }


    return jsonapiPayload;
  }
});

@@ -25,38 +25,8 @@
 ta%22;
-%0Aimport UTF8 from %22npm:utf-8%22;
 %0A%0Afu
@@ -43,17 +43,21 @@
 2String(
-a
+uintA
 rray) %7B%0A
@@ -62,106 +62,154 @@
 %7B%0A  
-if (array === undefined)%7B%0A    return %22%22;%0A  %7D else %7B%0A    return UTF8.getStringFromBytes(array);%0A  %7D
+var encodedString = String.fromCharCode.apply(null, uintArray),%0A      decodedString = decodeURIComponent((encodedString));%0A  return decodedString;
 %0A%7D%0A%0A

rework bin2String function to work with npm test, ie Phantomjs