'use strict';

exports.__esModule = true;
exports.default = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _class, _temp;

var _react = require('react');

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var ConfigProvider = (_temp = _class = function (_Component) {
  _inherits(ConfigProvider, _Component);

  function ConfigProvider() {
    _classCallCheck(this, ConfigProvider);

    return _possibleConstructorReturn(this, _Component.apply(this, arguments));
  }

  ConfigProvider.prototype.getChildContext = function getChildContext() {
    var _props = this.props,
        _props$locale = _props.locale,
        locale = _props$locale === undefined ? {} : _props$locale,
        language = _props.language,
        portal = _props.portal,
        onChange = _props.onChange;

    var data = null;
    // 根据语言名称获取locale数据
    function initLanguageLocale() {
      var seedsuiLocale = {};
      var result = require('./../locale/' + language + '.js');
      if (!result) {
        console.error('seedsui\u4E2D, ' + language + '\u8BED\u8A00\u5305\u4E0D\u5B58\u5728');
        data = locale;
        return;
      }
      // 防止没有用babel-plugin-add-module-exports自动加上default
      if (result.default) seedsuiLocale = result.default;
      data = _extends({}, seedsuiLocale, locale);
    }
    // 有language属性时, 读取seedsui内部国际化文件
    if (language) {
      initLanguageLocale();
    } else {
      data = locale;
    }
    if (data) localStorage.setItem('_seedsui_locale', JSON.stringify(data));
    return {
      locale: function locale(remark, key, variable) {
        if (!data) return remark || key || '';
        // 获取key的值
        if (key) {
          var value = data[key] || '';
          if (value && variable && Array.isArray(variable) && variable.length) {
            for (var i = 0; i < variable.length; i++) {
              if (typeof variable[i] !== 'number' && typeof variable[i] !== 'boolean' && typeof variable[i] !== 'string') continue;
              value = value.replace(new RegExp('\\{' + i + '\\}', 'g'), variable[i]);
            }
          }
          return value || remark || key;
        }
        // 如果有remark, 没有key, 则返回remark
        if (remark) {
          return remark;
        }
        return data;
      },
      language: language,
      portal: portal,
      onChange: onChange
    };
  };

  ConfigProvider.prototype.render = function render() {
    return this.props.children;
  };

  return ConfigProvider;
}(_react.Component), _class.childContextTypes = {
  locale: _propTypes2.default.object,
  language: _propTypes2.default.string,
  portal: _propTypes2.default.object,
  onChange: _propTypes2.default.func
}, _temp);
exports.default = ConfigProvider;
ConfigProvider.propTypes = process.env.NODE_ENV !== "production" ? {
  locale: _propTypes2.default.object,
  language: _propTypes2.default.string,
  portal: _propTypes2.default.object,
  onChange: _propTypes2.default.func,
  children: _propTypes2.default.any
} : {};
module.exports = exports['default'];