"use strict";

require("core-js/modules/es.object.define-property.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useI18n = exports.transI18n = exports.getLanguage = exports.getI18n = exports.changeLanguage = exports.addResourceBundle = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
require("core-js/modules/es.array.for-each.js");
require("core-js/modules/es.object.keys.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.string.replace.js");
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.for-each.js");
require("core-js/modules/web.dom-collections.for-each.js");
var _i18next = _interopRequireDefault(require("i18next"));
var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));
var _reactI18next = require("react-i18next");
var _i18n = _i18next["default"];
var _i18nInitPromise = null;
var useI18n = exports.useI18n = function useI18n() {
  var _useTranslation = (0, _reactI18next.useTranslation)(),
    translate = _useTranslation.t,
    i18n = _useTranslation.i18n;
  return function (text, options) {
    if (!i18n.isInitialized) return '';
    var content = translate(text);
    if (!(0, _isEmpty["default"])(options)) {
      Object.keys(options).forEach(function (k) {
        content = content.replace("{".concat(k, "}"), options[k] || '');
      });
    }
    return content;
  };
};
var initI18n = function initI18n() {
  if (!_i18nInitPromise) {
    // passes i18n down to react-i18next
    _i18nInitPromise = _i18n.use(_reactI18next.initReactI18next).init({
      lng: getLanguage(),
      initImmediate: false,
      // keySeparator: false, // we do not use keys in form messages.welcome
      interpolation: {
        escapeValue: false // react already safes from xss
      }
    });
  }
  return _i18nInitPromise;
};
var addResourceBundle = exports.addResourceBundle = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee(lang, resourceBundle) {
    var namespace,
      _args = arguments;
    return _regenerator["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          namespace = _args.length > 2 && _args[2] !== undefined ? _args[2] : 'translation';
          if (_i18n.isInitialized) {
            _context.next = 1;
            break;
          }
          _context.next = 1;
          return initI18n();
        case 1:
          _i18n.addResourceBundle(lang, namespace, resourceBundle, true, true);
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return function addResourceBundle(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();
var changeLanguage = exports.changeLanguage = /*#__PURE__*/function () {
  var _ref2 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2(lang) {
    return _regenerator["default"].wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          window.$$agora_language = lang;
          if (_i18n.isInitialized) {
            _context2.next = 1;
            break;
          }
          _context2.next = 1;
          return initI18n();
        case 1:
          _context2.next = 2;
          return _i18n.changeLanguage(lang);
        case 2:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
  return function changeLanguage(_x3) {
    return _ref2.apply(this, arguments);
  };
}();
var getLanguage = exports.getLanguage = function getLanguage() {
  return window.$$agora_language || 'en';
};
var transI18n = exports.transI18n = function transI18n(text, options) {
  var content = _i18n.t(text);
  if (!(0, _isEmpty["default"])(options)) {
    Object.keys(options).forEach(function (k) {
      content = content.replace("{".concat(k, "}"), options[k] || '');
    });
  }
  return content;
};
var getI18n = exports.getI18n = function getI18n() {
  return _i18n;
};