import { _ as _typeof } from './typeof-498dd2b1.js';

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
// 实现 get 方法
// 支持对象和数组/支持默认值/支持路径/支持字符串模版变量/支持jsx/支持.运算符和[]运算符
var get = function get(obj, path, defaultValue) {
  var pathArr = path.replace(/\[(\d+)\]/g, '.$1').split('.');
  var result = obj;
  var _iterator = _createForOfIteratorHelper(pathArr),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var p = _step.value;
      result = Object(result)[p];
      if (result === undefined) {
        return defaultValue;
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return result;
};
/**
 * 替换字符串模版变量
 * @param str
 * @param vars
 */
var replaceStrTemplateVars = function replaceStrTemplateVars(str, vars) {
  if (!vars) return str;
  str = str.replace(/\"/g, '\\"');
  // 模版字符串正则
  var strTemplateReg = /({[^{}]+})/g;
  var parts = str.split(strTemplateReg);
  return parts.map(function (part) {
    if (part.match(strTemplateReg)) {
      var key = part.replace(/({|})/g, '');
      var val = get(vars, key);
      return val === undefined ? key : val;
    }
    return part;
  });
};
/**
 * 国际化获取方法
 * @param ops   国际化取值key参数
 * @param i18nData  国际化数据对象
 * @param params    国际化字符变量对象
 * @param lang      指定国际化语言
 * @param defI18nData   默认国际化数据对象
 */
var i18nIntl = function i18nIntl(props) {
  var _a, _b;
  var ops = props.ops,
    i18nData = props.i18nData,
    lang = props.lang,
    params = props.params,
    defI18nData = props.defI18nData;
  var text, key;
  if (typeof ops === 'string') {
    key = ops;
  } else if (_typeof(ops) === 'object') {
    if (typeof ops.id !== 'string') return 'i18n intl object "id" is missing';
    key = ops.id;
  }
  if (key !== undefined) {
    text = (_a = i18nData[lang]) === null || _a === void 0 ? void 0 : _a[key];
    if (text === undefined && defI18nData) {
      // 尝试从默认国际化数据获取
      text = (_b = defI18nData[lang]) === null || _b === void 0 ? void 0 : _b[key];
    }
  }
  if (typeof text !== 'string') return "{".concat(key, "}");
  return replaceStrTemplateVars(text, params);
};

export { i18nIntl as i };
