import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import "core-js/modules/es.array.for-each.js";
import "core-js/modules/es.object.keys.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/es.regexp.exec.js";
import "core-js/modules/es.string.replace.js";
import "core-js/modules/esnext.iterator.constructor.js";
import "core-js/modules/esnext.iterator.for-each.js";
import "core-js/modules/web.dom-collections.for-each.js";
import i18n from 'i18next';
import isEmpty from 'lodash/isEmpty';
import { initReactI18next, useTranslation } from 'react-i18next';
var _i18n = i18n;
var _i18nInitPromise = null;
export var useI18n = function useI18n() {
  var _useTranslation = useTranslation(),
    translate = _useTranslation.t,
    i18n = _useTranslation.i18n;
  return function (text, options) {
    if (!i18n.isInitialized) return '';
    var content = translate(text);
    if (!isEmpty(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(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;
};
export var addResourceBundle = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(lang, resourceBundle) {
    var namespace,
      _args = arguments;
    return _regeneratorRuntime.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);
  };
}();
export var changeLanguage = /*#__PURE__*/function () {
  var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(lang) {
    return _regeneratorRuntime.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);
  };
}();
export var getLanguage = function getLanguage() {
  return window.$$agora_language || 'en';
};
export var transI18n = function transI18n(text, options) {
  var content = _i18n.t(text);
  if (!isEmpty(options)) {
    Object.keys(options).forEach(function (k) {
      content = content.replace("{".concat(k, "}"), options[k] || '');
    });
  }
  return content;
};
export var getI18n = function getI18n() {
  return _i18n;
};