/**
 * Get themes data.
 * @function task.worker._flattenThemesData
 * @param {object} choices
 * @returns {Array} - Themes data.
 * @protected
 * @ignore
 *
 */
var object = require('../../lib/object'),
    deepCopy = object.deepCopy,
    _themeNameForIndex = require('./_theme_name_for_index');

module.exports = function (choices) {
    var result = [];
    Object.keys(choices).forEach(function (name) {
        var current = [].concat(result);
        result = choices[name]
            .map(function (value) {
                var data = {};
                data[name] = value;
                if (!!current.length) {
                    return deepCopy(current, []).map(function (data) {
                        data[name] = value;
                        return data;
                    });
                } else {
                    return [data];
                }
            })
            .reduce(function (a, b) {
                return [].concat(a, b);
            });
    });
    result.forEach(function (data, i) {
        data.themeName = _themeNameForIndex(i);
        data.themeIndex = i + 1;
    });
    return result;
};