text: 'use strict';
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  
  var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  
  /**
   * DataJsonOptimizer is a small utility to merge all data.json files from the webpack application
   * and create following final data json file.
   *
   * a) game-data.json => It has all keys in hyphenated.
   * b) game-data-in-camel-case.json => It has all keys in camelcase to access in js.
   *
   * Why this?
   * Application has too many data.json files which is carrying the all game properties and
   * keys are in hyphenated. In implementation, we are using camelcase keys. For this, if we
   * start converting the all keys on the fly on client side then it slows down the whole application.
   * So we are creating the final json files on application compile/build time only.
   *
   * @author Amitesh Kumar
   * @module DataJsonOptimizer
   *
   */
  
  var _ = require('lodash');
  var glob = require('glob');
  var path = require('path');
  var fs = require('fs');
  
  /**
   * Sort the all file paths as per the deep nesting
   * @param {Array.<string>} files List of file paths
   *
   * @return {Array.<string>} Sorted list of file paths
   *
   * @example ['a/b/c', 'a/b', 'a/b/c/d'] => ['a/b', 'a/b/c', 'a/b/c/d']
   */
  function sortFiles(files) {
    var sorted = _.sortBy(files, function (o) {
      return (o.match(/\//g) || []).length;
    });
    return sorted;
  }
  
  /**
   * Method to merge all data.json files and create the final game data json file
   * @param appPath
   *
   * Steps:
   * - Get all data.json files
   * - Sort all files as per the deep nesting
   * - Merge the properties in way that child values should not change
   * - Convert all hyphenated keys to camelcase
   * - Create/write game-data.json and game-data-in-camel-case.json file
   */
  function createFinalDataJsonFiles(appPath) {
    console.log("[Start] Creating game data json files");
    console.time("game data json");
    try {
      (function () {
        var gameData = void 0,
            gameDataInCamelCase = void 0;
        var dataFiles = glob.sync(path.join(appPath, "components/**/data.json"));
  
        var files = sortFiles(dataFiles);
  
        _.forEach(files, function (file) {
          gameData = _.merge({}, gameData, require(file));
        });
  
        gameDataInCamelCase = formatKeyAsCamelCase(_.cloneDeep(gameData));
  
        writeFile(path.join(appPath, 'data', 'game-data.json'), JSON.stringify(gameData, null, '\t'));
        writeFile(path.join(appPath, 'data', 'game-data-in-camel-case.json'), JSON.stringify(gameDataInCamelCase, null, '\t'));
      })();
    } catch (e) {
      console.error(e);
    }
  
    console.log("[End] Created game data json files");
    console.timeEnd("game data json");
  }
  
  /**
   * Write the content to the given file path
   * @param outputFile
   * @param content
   */
  function writeFile(outputFile, content) {
    if (fs.existsSync(path.resolve(outputFile))) {
      fs.unlinkSync(path.resolve(outputFile));
    }
    fs.writeFile(path.resolve(outputFile), content, 'utf8');
  }
  
  /**
   * Set key to only hyphenated key and value. But it will be a problem
   * with array type and retrieval of values through chaining
   *
   * @param  {[type]} collection [description]
   * @return {[type]}            [description]
   */
  function formatKeyAsCamelCase(collection) {
    return _.each(collection, function (o, key) {
      var ccKey = _.isNumber(key) ? key : _.camelCase(key || '');
      var oldValue = collection[ccKey];
  
      if (_.isObject(o)) {
        var formattedValue = formatKeyAsCamelCase(o);
        delete collection[key];
        collection[ccKey] = oldValue ? formatKeyAsCamelCase(oldValue) : formattedValue;
      } else {
        delete collection[key];
        collection[ccKey] = oldValue ? oldValue : o;
      }
    });
  }
  
  /**
   * A module to optimize the data.json files
   */
  
  var DataJsonOptimizer = function () {
    /**
     * Create new instance of DataJsonOptimizer
     * @param options
     * @constructor
     */
    function DataJsonOptimizer() {
      var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  
      _classCallCheck(this, DataJsonOptimizer);
    }
  
    /**
     * Register the plugin with webpack event hooks
     * @param compiler
     */
  
  
    _createClass(DataJsonOptimizer, [{
      key: 'apply',
      value: function apply(compiler) {
        /**
         * On compile event of webpack build process,
         * Convert the data.json files to final file.
         */
        compiler.plugin('compile', function (params) {
          var appPath = compiler.context;
          createFinalDataJsonFiles(appPath);
        });
      }
    }]);
  
    return DataJsonOptimizer;
  }();
  
  exports.default = DataJsonOptimizer;
  module.exports = exports['default'];