"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.$L = $L;
exports.clearResBundle = clearResBundle;
exports.createResBundle = createResBundle;
exports["default"] = void 0;
exports.setResBundle = setResBundle;
var _resBundle = require("@enact/i18n/src/resBundle");
var _ResBundle = _interopRequireDefault(require("ilib/lib/ResBundle"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /* global ILIB_AGATE_PATH */
// The ilib.ResBundle for the active locale used by $L
var resBundle;

/**
 * Returns the current ilib.ResBundle
 *
 * @returns {ilib.ResBundle} Current ResBundle
 */
function getResBundle() {
  return resBundle;
}

/**
 * Creates a new ilib.ResBundle for string translation
 *
 * @param  {ilib.Locale} locale Locale for ResBundle
 *
 * @returns {Promise|ResBundle} Resolves with a new ilib.ResBundle
 */
function createResBundle() {
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var opts = options;
  if (typeof ILIB_AGATE_PATH !== 'undefined') {
    opts.basePath = ILIB_AGATE_PATH;
  }
  if (!opts.onLoad) return;

  // eslint-disable-next-line no-new
  new _ResBundle["default"](_objectSpread(_objectSpread({}, opts), {}, {
    onLoad: function onLoad(bundle) {
      opts.onLoad(bundle || null);
    }
  }));
}

/**
 * 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;
}

/**
 * Set the locale for the strings that $L loads. This may reload the
 * string resources if necessary.
 *
 * @param {string} spec the locale specifier
 * @returns {ilib.ResBundle} Current ResBundle
 */
function setResBundle(bundle) {
  return resBundle = bundle;
}
function toIString(str) {
  var rb = getResBundle();
  if (!rb) {
    createResBundle({
      sync: true,
      onLoad: setResBundle
    });
    rb = getResBundle();
  }
  return (0, _resBundle.getIStringFromBundle)(str, rb);
}

/**
 * Maps a string or key/value object to a translated string for the current locale.
 *
 * @function
 * @memberof i18n/$L
 * @param  {String|Object} str Source string
 *
 * @returns {String} The translated string
 */
function $L(str) {
  return String(toIString(str));
}
var _default = exports["default"] = $L;