import React from 'react';
import hasNode from "./hasNode";
import splitValue from "./splitValue";

// values ['text', 'variable:0'], variables [1000], replace variable to text1000
function valuesToText(values, variables) {
  return values.map(function (value) {
    // Replace variable
    if (value.startsWith('variable:')) {
      var variableName = value.replace('variable:', '');
      var newValue = variables === null || variables === void 0 ? void 0 : variables[variableName];
      if (typeof newValue === 'number' || typeof newValue === 'boolean') {
        newValue = String(newValue);
      }
      return newValue || '';
    }

    // 如果不是variable, 直接返回
    return value;
  });
}

/**
 * 国际化函数
 * @param {String} remark '共有{0}个商品, 共查到{1}页'
 * @param {String} key resources中的key
 * @param {Array} variables {0: <div><div>}
 * @return {Node} 返回reactDOM
 */
function locale(remark, key, variables) {
  var localeData = window.seedsLocaleData || {};
  var value = localeData[key || ''] || remark;

  // Split value('text{0}') to ['text', 'variable:0']
  var values = splitValue(value);

  // No node, return string
  if (!hasNode(variables)) {
    return valuesToText(values, variables).join('');
  }

  // Has node, return node
  return /*#__PURE__*/React.createElement(React.Fragment, null, valuesToText(values, variables));
}
export default locale;