// Generated by CoffeeScript 1.12.5
import Ember from 'ember';

import DS from 'ember-data';

export default Ember.Component.extend({
  user: null,
  project_specific: false,
  session: Ember.inject.service('session'),
  classNames: ['project-aggregate-statistics', 'project-aggregate-user-statistics'],
  statistics: Ember.computed('statistics_data.isFulfilled', 'project_specific', function() {
    var count, d, project, project_id, project_specific, ref, state, states, stats, target;
    d = this.get('statistics_data');
    if (!d.isFulfilled) {
      return {};
    }
    target = {};
    states = this.get('globals.project_instance_action.states');
    project_specific = this.get('project_specific');
    if (!project_specific) {
      states.forEach(function(state) {
        return target[state] = 0;
      });
    }
    ref = d.content.data;
    for (project_id in ref) {
      stats = ref[project_id];
      if (project_specific) {
        project = d.content.projects.findBy('id', project_id);
        target[project_id] = {
          project: project,
          states: []
        };
        states.forEach(function(state) {
          return target[project_id].states.push({
            state: state,
            count: stats[state] || 0
          });
        });
      } else {
        for (state in stats) {
          count = stats[state];
          target[state] += count;
        }
      }
    }
    return target;
  }),
  statistics_data: Ember.computed(function() {
    var base_url, model_name, request, session, store, url, user_id;
    if (!(user_id = this.get('user.id'))) {
      return {};
    }
    model_name = 'user';
    base_url = this.store.adapterFor(model_name).buildURL(model_name);
    url = base_url + "/" + user_id + "/statistics1";
    session = this.get('session');
    store = this.store;
    request = {
      url: url,
      contentType: "application/json",
      dataType: "json"
    };
    if (Ember.get(session, 'isAuthenticated')) {
      session.authorize('authorizer:devise', function(key, authorization) {
        var obj;
        request.crossDomain = true;
        return request.headers = (
          obj = {},
          obj["" + key] = authorization,
          obj
        );
      });
    }
    return DS.PromiseObject.create({
      promise: Ember.RSVP.hash({
        data: Ember.$.ajax(request),
        projects: store.findAll('panda/project', {
          backgroundReload: false
        })
      })
    });
  })
});
