"use strict";

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

var _resBundle = require("@enact/i18n/src/resBundle");

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

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

// 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 = $L;
exports["default"] = _default;