'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = undefined;

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _class, _temp2;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _formatMessageGenerateId = require('format-message-generate-id');

var _formatMessageGenerateId2 = _interopRequireDefault(_formatMessageGenerateId);

var _LocaleSwitcher = require('quiz-interactions/lib/util/LocaleSwitcher');

var _LocaleSwitcher2 = _interopRequireDefault(_LocaleSwitcher);

var _formatMessage = require('../format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

var generateId = _formatMessageGenerateId2.default.underscored_crc32;

// This is for switching locales in the docs app. Not for production use!
var LocaleSwitcher = (_temp2 = _class = function (_Component) {
  (0, _inherits3.default)(LocaleSwitcher, _Component);

  function LocaleSwitcher() {
    var _ref,
        _this2 = this;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, LocaleSwitcher);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = LocaleSwitcher.__proto__ || Object.getPrototypeOf(LocaleSwitcher)).call.apply(_ref, [this].concat(args))), _this), _this.handleLocaleChange = function () {
      var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(locale) {
        var translations;
        return _regenerator2.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return import('../../../translations/' + locale + '.json');

              case 2:
                translations = _context.sent;

                _formatMessage2.default.setup({
                  generateId: generateId,
                  locale: locale,
                  translations: (0, _defineProperty3.default)({}, locale, translations)
                });

              case 4:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, _this2);
      }));

      return function (_x) {
        return _ref2.apply(this, arguments);
      };
    }(), _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(LocaleSwitcher, [{
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        _LocaleSwitcher2.default,
        {
          onLocaleChange: this.handleLocaleChange
        },
        this.props.children
      );
    }
  }]);
  LocaleSwitcher.displayName = 'LocaleSwitcher'
  ;
  return LocaleSwitcher;
}(_react.Component), _class.propTypes = {
  children: _propTypes2.default.node.isRequired
}, _temp2);
exports.default = LocaleSwitcher;