{"size": 1876, "ext": "js", "max_stars_repo_path": "app/collections/repos.js", "max_stars_repo_name": "duckbrain/prose", "max_stars_repo_stars_event_min_datetime": "2016-07-25T09:59:35.000Z", "max_stars_repo_stars_event_max_datetime": "2017-08-24T14:14:48.000Z", "max_issues_repo_path": "app/collections/repos.js", "max_issues_repo_name": "duckbrain/prose", "max_issues_repo_issues_event_min_datetime": "2016-06-15T22:51:33.000Z", "max_issues_repo_issues_event_max_datetime": "2016-09-12T14:38:23.000Z", "max_forks_repo_path": "app/collections/repos.js", "max_forks_repo_name": "duckbrain/prose", "max_forks_repo_forks_event_min_datetime": "2017-02-25T20:46:38.000Z", "max_forks_repo_forks_event_max_datetime": "2020-07-05T10:29:08.000Z"}
{"max_stars_count": 17, "max_issues_count": 6, "max_forks_count": 7, "avg_line_length": 23.746835443, "max_line_length": 70, "alphanum_fraction": 0.5442430704}
var _ = require('underscore');
var url = require('url');
var Backbone = require('backbone');
var Repo = require('../models/repo');

var auth = require('../config');
var cookie = require('../cookie');

var util = require('../util');

module.exports = Backbone.Collection.extend({
  model: Repo,

  initialize: function(models, options) {
    this.user = options.user;

    this.comparator = function(repo) {
      return -(new Date(repo.get('updated_at')).getTime());
    };

    this.api = util.getApiFlavor();
  },

  /*
   * TODO look at headers for next pagination links.
  fetch: function(opts) {
    var options = _.clone(opts) || {};

    var cb = options.success;

    var success = (function(res, statusText, xhr) {
      this.add(res);
      util.parseLinkHeader(xhr, {
        success: success,
        complete: cb
      });
    }).bind(this);

    Backbone.Collection.prototype.fetch.call(this, _.extend(options, {
      api: this.api, // pass api flavor to models
      success: (function(model, res, options) {
        util.parseLinkHeader(options.xhr, {
          success: success,
          error: cb
        });
      }).bind(this)
    }));
  },
  */

  url: function() {
    var id = cookie.get('id');
    var type = this.user.get('type');

    if (this.api === 'gitlab') {
      var scope = cookie.get('scope');
      var query = {};
      if (scope === 'public_repo') {
        query.scope = 'public';
      }
      return auth.api + '/projects' + url.format({query: query});
    }

    else {
      var path;
      switch(type) {
        case 'User':
          path = (id && this.user.get('id') === id) ? '/user' :
            ('/users/' + this.user.get('login'))
          break;
        case 'Organization':
          path = '/orgs/' + this.user.get('login');
          break;
      }
      return auth.api + path + '/repos?per_page=100';
    }
  }
});
