'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; }; }();

var _fsExtra = require('fs-extra');

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _ = require('../../');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var singleton = Symbol();
var singletonEnforcer = Symbol();

var Locales = function () {
  function Locales(enforcer) {
    _classCallCheck(this, Locales);

    if (enforcer != singletonEnforcer) throw 'Cannot construct Json singleton';

    this.i18n = this._getLocales();
  }

  _createClass(Locales, [{
    key: '_reloadLocales',
    value: function _reloadLocales() {
      this.i18n = this._getLocales();
    }
  }, {
    key: '_getLocales',
    value: function _getLocales() {
      var loc = {};

      var extension = '.json';
      var localesFolder = _path2.default.join(_.config.root, 'locales');
      var files = _.coreUtils.file.getFilesSync(localesFolder, true, extension);
      Array.prototype.forEach.call(files, function (file) {
        var name = _path2.default.basename(file, extension);
        loc[name] = _fsExtra2.default.readJsonSync(file);
      });

      return loc;
    }
  }], [{
    key: 'instance',
    get: function get() {
      if (!this[singleton]) {
        this[singleton] = new Locales(singletonEnforcer);
      }
      return this[singleton];
    }
  }]);

  return Locales;
}();

exports.default = Locales;