"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.$L = $L;
exports.getResBundle = getResBundle;
exports.clearResBundle = clearResBundle;
exports.default = void 0;

var _ResBundle = _interopRequireDefault(require("@enact/i18n/ilib/lib/ResBundle"));

var _Locale = _interopRequireDefault(require("@enact/i18n/ilib/lib/Locale"));

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

// The ilib.ResBundle for the active locale used by $L
var resBundle;
/**
 * Creates a new ilib.ResBundle for string translation
 *
 * @returns {ilib.ResBundle} New ilib.ResBundle
 */

function getResBundle() {
  var currLoc = new _Locale.default();

  if (typeof ILIB_MOONSTONE_PATH === 'string' && (!resBundle || currLoc.getSpec() !== resBundle.getLocale().getSpec())) {
    resBundle = new _ResBundle.default({
      loadParams: {
        root: ILIB_MOONSTONE_PATH
      }
    });
  }

  return resBundle;
}
/**
 * Deletes the current bundle object of strings and clears the cache.
 * @returns {undefined}
 */


function clearResBundle() {
  delete _ResBundle.default.strings;
  delete _ResBundle.default.sysres;
  resBundle = null;
}
/**
* Localized internal Moonstone strings from iLib translations.
*
* @param {String} str - String to be localized.
* @returns {String} Localized string.
*/


function $L(str) {
  var rb = getResBundle();
  var isObject = _typeof(str) === 'object';

  if (!rb) {
    return isObject ? str.value : str;
  }

  return isObject ? rb.getString(str.value, str.key).toString() : rb.getString(str).toString();
}

var _default = $L;
exports.default = _default;