'use strict';

function contributions() {
  'ngInject';

  // Get edits from Editlog API ?
  //if (NpolarApiSecurity.isAuthenticated()) {
  //  $scope.user = NpolarApiSecurity.getUser();
  //  Editlog.facets({q:'', 'filter-request.username': $scope.user.email}, r => {
  //    $scope.contributions = r.find(f => f.facet === 'endpoint').terms.map(t => {
  //      return { text: t.term, href: `${t.term}?q=${$scope.user.name.split(' ').join('+')}`, count: t.count }; });
  //  });
  //}
  return {

    restrict: 'E',

    scope: {
      facets: '='
    },
    template: require('./contributions.html'),
    controller: function ContributionsController($scope, $location, NpolarApiSecurity) {
      'ngInject';

      let ctrl = this;
      this._user;
      this.getUser = () => {
        if ($location.search()['filter-created_by']) {
          let email = name = $location.search()['filter-created_by'];
          return { name, email, count: 0 };
        }
        return ctrl._user || NpolarApiSecurity.getUser();
      }
      this.isAuthenticated = () => NpolarApiSecurity.isAuthenticated();

      this.count = (field,term) => {
        let facet = ctrl.getFacet(field);

        if (!term && facet) {
          return facet.length;
        } else if (term && facet) {
          let found = facet.find(f => f.term === term);
          if (found && found.count) {
            return found.count;
          } else {
            return 0;
          }
        } else {
          return 0;
        }
      };

      this.getFacet = (key, facets=$scope.facets) => {
        if (!facets) {
          return;
        }
        let contribs = facets.filter(f => {
          return Object.keys(f).includes(key) && f[key]
        }).map(c => {
          return c[key];
        })[0];
        return contribs;
      };

      this.setUser = (email, count, name=email) => {
        ctrl._user = { name, email, count };
      };

    },
    controllerAs: 'ctrl'

  };
}
module.exports = contributions;
@@ -727,16 +727,17 @@
 = this;%0A
+%0A
       th
@@ -744,18 +744,26 @@
 is._user
+ = null
 ;%0A
+%0A
       th
@@ -863,15 +863,8 @@
 il =
- name =
  $lo
@@ -919,22 +919,16 @@
 return %7B
- name,
  email, 
@@ -1007,23 +1007,25 @@
 User();%0A
-
       %7D
+;%0A
 %0A      t
@@ -1670,16 +1670,17 @@
  return 
+(
 Object.k
@@ -1709,16 +1709,18 @@
 & f%5Bkey%5D
+);
 %0A       

Remove name