{"size": 3990, "ext": "js", "max_stars_repo_path": "lib/handlers/array.js", "max_stars_repo_name": "okuyiga/joi-generate", "max_stars_repo_stars_event_min_datetime": "2015-10-02T08:28:03.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-16T12:15:23.000Z", "max_issues_repo_path": "lib/handlers/array.js", "max_issues_repo_name": "okuyiga/joi-generate", "max_issues_repo_issues_event_min_datetime": "2016-11-14T16:53:39.000Z", "max_issues_repo_issues_event_max_datetime": "2019-09-10T08:50:03.000Z", "max_forks_repo_path": "lib/handlers/array.js", "max_forks_repo_name": "okuyiga/joi-generate", "max_forks_repo_forks_event_min_datetime": "2015-12-17T04:15:19.000Z", "max_forks_repo_forks_event_max_datetime": "2018-06-27T18:11:32.000Z"}
{"max_stars_count": 16, "max_issues_count": 4, "max_forks_count": 11, "avg_line_length": 32.7049180328, "max_line_length": 102, "alphanum_fraction": 0.4010025063}
'use strict';
var _ = require('lodash');
var Joi = require('joi');
var async = require('async');
var Onion = require('lib-onion');

var ArrayTypeHandler = function(opts) {
    var typeFactory = opts.typeFactory;
    var chance = opts.chance;
    var debug = opts.debug;

    var onion = new Onion('array');

    var Min = function(msg, tests, next) {
        var min = _.find(tests, {
            name: 'min'
        });
        if (min) {
            min = min.arg;
            if (msg.num >= min) {
                // Message is OK as it is
                return next(null, msg, tests);
            }

            msg.num = chance.integer({
                min: min,
                max: 100
            });
        }
        next(null, msg, tests);
    };

    var Max = function(msg, tests, next) {
        /* jshint maxcomplexity: 6 */
        var max = _.find(tests, {
            name: 'max'
        });
        if (max) {
            max = max.arg;
            if (msg.num <= max) {
                // Message is OK as it is
                return next(null, msg, tests);
            }

            var min = _.find(tests, {
                name: 'min'
            });
            if (min) {
                min = min.arg;
                msg.num = chance.integer({
                    min: min,
                    max: max
                });
            } else {
                msg.num = chance.integer({
                    min: 0,
                    max: max
                });
            }
        }
        return next(null, msg, tests);
    };

    onion.add(Min, 'min');
    onion.add(Max, 'max');

    return {
        handle: function(schema, callback) {
            var output = {};

            onion.handle(output, schema._tests, function(err, output) {
                debug('output', output);
                var potentialSchemas = [];

                // For each of the items in the array definition...
                async.eachSeries(schema._inner.items, function(child, callback) {
                    // Loop through each of the children of that defintion
                    if (child._inner.children) {
                        var item = {};
                        async.eachSeries(child._inner.children, function(child, cb) {
                            debug('doing child element', child.key, child.schema._type);
                            item[child.key] = child.schema;
                            cb();
                        }, function(err) {
                            potentialSchemas.push(Joi.object(item));
                            item = {};
                            callback(err);
                        });
                    } else {
                        potentialSchemas.push(child);
                        callback();
                    }
                }, function(err) {
                    if (err) {
                        return callback(err, null);
                    }
                    var schemaGenerators = new Array(output.num);
                    _.fill(schemaGenerators, function(cb) {
                        var randomSchema = potentialSchemas[_.random(0, potentialSchemas.length - 1)];
                        typeFactory.gimme(randomSchema, function(err, childHandler) {
                            if (err) {
                                return cb(err, null);
                            }
                            childHandler.handle(randomSchema, function(err, childResult) {
                                debug('output', randomSchema.key, childResult);
                                cb(err, childResult);
                            });
                        });
                    });

                    async.series(schemaGenerators, function(err, output) {
                        debug('output', output.msg);
                        callback(err, output);
                    });
                });
            });
        }
    };
};

module.exports = ArrayTypeHandler;
