'use strict';

angular.module('app.filters', [])
  // see i18n.js for i18n filter
  .filter('upper', function() {
    return function(s) {
      return angular.uppercase(s);
    };
  })
  .filter('badgeCount', function() {
    return function(str, max) {
      var count = parseInt(str), max = max || 9;
      return count > max ? max + '+' : count;
    };
  })
  .filter('notRejected', function(FRIEND_STATUS) {
    return function(friends) {
      return _.reject(friends, {status: FRIEND_STATUS.rejected});
    };
  })
  .filter('noNullIsland', function() {
    return function(peers) {
      return _.reject(peers, function (peer) {
        return peer.lat === 0.0 && peer.lon === 0.0;
      });
    };
  })
  .filter('prettyUser', function() {
    return function(obj) {
      if (!obj) return obj;
      if (obj.email && obj.name)
        return obj.name + ' <' + obj.email + '>'; // XXX i18n?
      return obj.email;
    };
  })
  .filter('prettyBytes', function($filter) {
    return function(nbytes, dimensionInput, showUnits) {
      if (_.isNaN(nbytes)) return nbytes;
      if (_.isUndefined(dimensionInput)) dimensionInput = nbytes;
      if (_.isUndefined(showUnits)) showUnits = true;
      var dimBase = byteDimension(dimensionInput),
          dim = dimBase.dim,
          base = dimBase.base,
          quotient = $filter('number')(nbytes / base, 1);
      return showUnits ? quotient+' '+dim // XXX i18n?
                       : quotient;
    };
  })
  .filter('prettyBps', function($filter) {
    return function(nbytes, dimensionInput, showUnits) {
      if (_.isNaN(nbytes)) return nbytes;
      if (_.isUndefined(showUnits)) showUnits = true;
      var bytes = $filter('prettyBytes')(nbytes, dimensionInput, showUnits);
      return showUnits ? bytes+'/'+'s' // XXX i18n?
                       : bytes;
    };
  })
  .filter('reportedState', function() {
    return function(model) {
      var state = _.cloneDeep(model);

      // omit these fields
      state = _.omit(state, 'mock', 'countries', 'global');

      // only include these fields from the user's profile
      if (state.profile) {
        state.profile = {email: state.profile.email, name: state.profile.name};
      }

      // replace these array fields with their lengths
      _.each(['/roster', '/settings/proxiedSites', '/friends/current', '/friends/pending'], function(path) {
        var len = (getByPath(state, path) || []).length;
        if (len) applyPatch(state, [{op: 'replace', path: path, value: len}]);
      });

      var peers = getByPath(state, '/peers');
      if (peers) {
        peers = _.map(peers, function(peer) {
          return _.omit(peer, 'email', 'peerid', 'ip', 'lat', 'lon');
        });
        applyPatch(state, [{op: 'replace', path: '/peers', value: peers}]);
      }

      return state;
    };
  })
  .filter('version', function() {
    return function(versionObj, tag, git) {
      if (!versionObj) return versionObj;
      var components = [versionObj.major, versionObj.minor, versionObj.patch],
          versionStr = components.join('.');
      if (!tag) return versionStr;
      if (versionObj.tag) versionStr += '-'+versionObj.tag;
      if (!git) return versionStr;
      if (versionObj.git) versionStr += ' ('+versionObj.git.substring(0, 7)+')';
      return versionStr;
    };
  });

@@ -2323,36 +2323,8 @@
 ends
-/current', '/friends/pending
 '%5D, 
@@ -2336,24 +2336,24 @@
 ion(path) %7B%0A
+
         var 
@@ -2543,42 +2543,14 @@
     
-if (peers) %7B%0A        peers = _.map
+_.each
 (pee
@@ -2557,24 +2557,25 @@
 rs, function
+ 
 (peer) %7B%0A   
@@ -2583,165 +2583,84 @@
     
-  return _.omit(peer, 'email', 'peerid', 'ip', 'lat', 'lon');%0A        %7D);%0A        applyPatch(state, %5B%7Bop: 'replace', path: '/peers', value: peers%7D%5D);
+if (peer.rosterEntry) %7B%0A          peer.rosterEntry = true;%0A        %7D
 %0A      %7D
 %0A%0A  
@@ -2647,32 +2647,34 @@
        %7D%0A      %7D
+);
 %0A%0A      return s

update reportedState filter to schema changes