{"size": 1415, "ext": "js", "max_stars_repo_path": "static/js/services/download-url.js", "max_stars_repo_name": "binokaryg/cht-core-old", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "static/js/services/download-url.js", "max_issues_repo_name": "binokaryg/cht-core-old", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "static/js/services/download-url.js", "max_forks_repo_name": "binokaryg/cht-core-old", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.1194029851, "max_line_length": 71, "alphanum_fraction": 0.5561837456}
var moment = require('moment');

(function () {

  'use strict';

  var inboxServices = angular.module('inboxServices');

  var getUrl = function(BaseUrlService, options) {
    var url = [BaseUrlService()];
    url.push('export');
    if (options.messages) {
      url.push('messages');
    } else {
      url.push('forms');
      if (options.form && options.form.code) {
        url.push(options.form.code);
      }
    }
    return url.join('/');
  };

  var getKey = function(options, date) {
    var key = [];

    // district
    if (options.district) {
      key.push(options.district);
    }

    // form
    key.push(getFormKey(options));

    // date
    key.push(date);

    return JSON.stringify(key);
  };

  var getFormKey = function(options) {
    if (options.messages) {
      return 'null_form';
    }
    if (options.form && options.form.code) {
      return options.form.code;
    }
    return '*';
  };

  inboxServices.factory('DownloadUrl', ['BaseUrlService',
    function(BaseUrlService) {
      return function(options) {
        options = options || {};
        var params = {
          startkey: getKey(options, 9999999999999),
          endkey: getKey(options, 0),
          tz: options.tz || moment().zone(),
          format: options.format || 'xml',
          reduce: false
        };
        return getUrl(BaseUrlService, options) + '?' + $.param(params);
      };
    }
  ]);

}()); 
