function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _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; }; return _extends.apply(this, arguments); }

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

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

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

import { Component, Children } from 'react';
import PropTypes from 'prop-types';
import getContextProps from './get-context-props';
import { config, initLocales, setLanguage, setLocale, setDirection, getLocale, getLanguage, getDirection } from './config';
import Consumer from './consumer';
import ErrorBoundary from './error-boundary';
import Cache from './cache';
var childContextCache = new Cache();
/**
 * ConfigProvider
 * @propsExtends false
 */

var ConfigProvider =
/*#__PURE__*/
function (_Component) {
  _inherits(ConfigProvider, _Component);

  /**
   * 传入组件，生成受 ConfigProvider 控制的 HOC 组件
   * @param {Component} Component 组件类
   * @param {Object} options 可选项
   * @returns {Component} HOC
   */

  /**
   * 传入组件的 props 和 displayName，得到和 childContext 计算过的包含有 preifx/locale/pure 的对象，一般用于通过静态方法生成脱离组件树的组件
   * @param {Object} props 组件的 props
   * @param {String} displayName 组件的 displayName
   * @returns {Object} 新的 context props
   */
  function ConfigProvider() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, ConfigProvider);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(ConfigProvider)).call.apply(_getPrototypeOf2, [this].concat(args)));
    childContextCache.add(_assertThisInitialized(_this), _extends({}, childContextCache.get(_assertThisInitialized(_this), {}), _this.getChildContext()));
    return _this;
  }

  _createClass(ConfigProvider, [{
    key: "getChildContext",
    value: function getChildContext() {
      var _this$props = this.props,
          prefix = _this$props.prefix,
          locale = _this$props.locale,
          pure = _this$props.pure,
          warning = _this$props.warning,
          rtl = _this$props.rtl,
          errorBoundary = _this$props.errorBoundary,
          scriptUrl = _this$props.scriptUrl;
      return {
        nextPrefix: prefix,
        nextLocale: locale,
        nextPure: pure,
        nextRtl: rtl,
        nextWarning: warning,
        nextErrorBoundary: errorBoundary,
        nextScriptUrl: scriptUrl
      };
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      this.setMomentLocale(this.props.locale);
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if (this.props.locale !== nextProps.locale) {
        this.setMomentLocale(nextProps.locale);
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      childContextCache.add(this, _extends({}, childContextCache.get(this, {}), this.getChildContext()));
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      childContextCache.remove(this);
    }
  }, {
    key: "setMomentLocale",
    value: function setMomentLocale(locale) {
      var moment;

      try {
        moment = require('moment');
      } catch (e) {// ignore
      }

      if (moment && locale) {
        moment.locale(locale.momentLocale);
      }
    }
  }, {
    key: "render",
    value: function render() {
      return Children.only(this.props.children);
    }
  }]);

  return ConfigProvider;
}(Component);

_defineProperty(ConfigProvider, "propTypes", {
  /**
   * 样式类名的品牌前缀
   */
  prefix: PropTypes.string,

  /**
   * 国际化文案对象，属性为组件的 displayName
   */
  locale: PropTypes.object,

  /**
   * 是否开启错误捕捉 errorBoundary
   * 如需自定义参数，请传入对象 对象接受参数列表如下：
   *
   * fallbackUI `Function(error?: {}, errorInfo?: {}) => Element` 捕获错误后的展示
   * afterCatch `Function(error?: {}, errorInfo?: {})` 捕获错误后的行为, 比如埋点上传
   */
  errorBoundary: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),

  /**
   * 是否开启 Pure Render 模式，会提高性能，但是也会带来副作用
   */
  pure: PropTypes.bool,

  /**
   * 是否在开发模式下显示组件属性被废弃的 warning 提示
   */
  warning: PropTypes.bool,

  /**
   * 是否开启 rtl 模式
   */
  rtl: PropTypes.bool,

  /**
   * 组件树
   */
  children: PropTypes.element,

  /**
   * Icon symbol加载的js资源
   */
  scriptUrl: PropTypes.string
});

_defineProperty(ConfigProvider, "defaultProps", {
  warning: true,
  errorBoundary: false
});

_defineProperty(ConfigProvider, "childContextTypes", {
  nextPrefix: PropTypes.string,
  nextLocale: PropTypes.object,
  nextPure: PropTypes.bool,
  nextRtl: PropTypes.bool,
  nextWarning: PropTypes.bool,
  nextErrorBoundary: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
  nextScriptUrl: PropTypes.string
});

_defineProperty(ConfigProvider, "config", function (Component, options) {
  return config(Component, options);
});

_defineProperty(ConfigProvider, "getContextProps", function (props, displayName) {
  return getContextProps(props, childContextCache.root() || {}, displayName);
});

_defineProperty(ConfigProvider, "initLocales", initLocales);

_defineProperty(ConfigProvider, "setLanguage", setLanguage);

_defineProperty(ConfigProvider, "setLocale", setLocale);

_defineProperty(ConfigProvider, "setDirection", setDirection);

_defineProperty(ConfigProvider, "getLanguage", getLanguage);

_defineProperty(ConfigProvider, "getLocale", getLocale);

_defineProperty(ConfigProvider, "getDirection", getDirection);

_defineProperty(ConfigProvider, "Consumer", Consumer);

_defineProperty(ConfigProvider, "ErrorBoundary", ErrorBoundary);

_defineProperty(ConfigProvider, "getContext", function () {
  var _ref = childContextCache.root() || {},
      nextPrefix = _ref.nextPrefix,
      nextLocale = _ref.nextLocale,
      nextPure = _ref.nextPure,
      nextRtl = _ref.nextRtl,
      nextWarning = _ref.nextWarning,
      nextErrorBoundary = _ref.nextErrorBoundary,
      nextScriptUrl = _ref.nextScriptUrl;

  return {
    prefix: nextPrefix,
    locale: nextLocale,
    pure: nextPure,
    rtl: nextRtl,
    warning: nextWarning,
    errorBoundary: nextErrorBoundary,
    scriptUrl: nextScriptUrl
  };
});

export default ConfigProvider;