"use strict";

const config = require('config')
    , moment = require('moment')
    , _ = require('underscore')
    ;


module.exports = {

    /**
     * @returns {obj}
     */
    getDefault: function () {

        let start = config.has('start') ? config.get('start') : moment.utc().subtract(32, 'days').unix();
        let end   = config.has('end') ? config.get('end') : moment.utc().unix();

        return _.clone({
            'method': 'POST',
            'auth': {
                'user': process.env.AUTH_USER || (config.has('index.default.auth.username') ? config.get('index.default.auth.username') : ""),
                'pass': process.env.AUTH_KEY || (config.has('index.default.auth.api_key') ? config.get('index.default.auth.api_key') : ""),
            },
            'uri': process.env.ANALYZE_URI || (config.has('app.analyze_uri') ? config.get('app.analyze_uri') : "https://api.datasift.com/v1/pylon/analyze"),
            'json': {
                'hash': process.env.HASH || (config.has('index.default.hash') ? config.get('index.default.hash') : ""),
                'start': start,
                'end': end,
                'parameters': {
                    'analysis_type': 'freqDist',
                    'parameters': {}
                }
            }
        });
    },


    /**
     * Check string is a valid target
     *
     * @param "str"
     * @returns {boolean}
     */
    isValidKey: function(str){
        let arr = ["filter", "interval", "threshold", "target", "id", "name", "index"];
        return (arr.indexOf(str) != -1);
    },


    /**
     * Set index auth and hash if specified within
     * the task config using the "index" property.
     *
     * @param {task}
     * @param {reqObj}
     * @returns {reqObj}
     */
    getIndexCreds: function(task, reqObj){
        if(task.index){
            let index = task.index.toString();
            reqObj.auth.user =  (config.has('index.' + index + '.auth.username') ? config.get('index.' + index + '.auth.username') : "");
            reqObj.auth.pass =  (config.has('index.' + index + '.auth.api_key') ? config.get('index.' + index + '.auth.api_key') : "");
            reqObj.json.hash =  (config.has('index.' + index + '.hash') ? config.get('index.' + index + '.hash') : "");
            delete (reqObj.json.parameters.parameters.index);
        }
        return reqObj;
    },


    /**
     * Returns a copy of the array with all falsy
     * values removed. In JavaScript, false, null,
     * 0, "", undefined and NaN are all falsy.
     *
     * @param {obj}
     */
    compact: function(obj){
        return _.compact(obj)
    }

};

@@ -2392,16 +2392,350 @@
 *%0A     *
+ Remove expected %22undefined%22 values and array nesting%0A     * caused by non returned promises due to recursive call%0A     * within promise chain.%0A     *%0A     * Flatten - Flattens a nested array (the nesting%0A     * can be to any depth). If you pass shallow,%0A     * the array will only be flattened a single level.%0A     *%0A     * Compact -
  Returns
@@ -2770,16 +2770,16 @@
 l falsy%0A
-
      * v
@@ -2927,24 +2927,66 @@
 ction(obj)%7B%0A
+        let flat = _.flatten(obj, false);%0A
         retu
@@ -2998,19 +2998,20 @@
 compact(
-obj
+flat
 )%0A    %7D%0A

Handle unresolved promise responses.