import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
// TODO: remove this lint
// SFC has specified a displayName, but not worked.

/* eslint-disable react/display-name */
import * as React from 'react';
import { FormProvider as RcFormProvider } from 'rc-field-form';
import LocaleProvider, { ANT_MARK } from '../locale-provider';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import { ConfigConsumer, ConfigContext } from './context';
import { SizeContextProvider } from './SizeContext';
export { ConfigContext, ConfigConsumer };
export var configConsumerProps = ['getTargetContainer', 'getPopupContainer', 'rootPrefixCls', 'getPrefixCls', 'renderEmpty', 'csp', 'autoInsertSpaceInButton', 'locale', 'pageHeader'];

var ConfigProvider = /*#__PURE__*/function (_React$Component) {
  _inherits(ConfigProvider, _React$Component);

  var _super = _createSuper(ConfigProvider);

  function ConfigProvider() {
    var _this;

    _classCallCheck(this, ConfigProvider);

    _this = _super.apply(this, arguments);

    _this.getPrefixClsWrapper = function (context) {
      return function (suffixCls, customizePrefixCls) {
        var prefixCls = _this.props.prefixCls;
        if (customizePrefixCls) return customizePrefixCls;
        var mergedPrefixCls = prefixCls || context.getPrefixCls('');
        return suffixCls ? "".concat(mergedPrefixCls, "-").concat(suffixCls) : mergedPrefixCls;
      };
    };

    _this.renderProvider = function (context, legacyLocale) {
      var _this$props = _this.props,
          children = _this$props.children,
          getTargetContainer = _this$props.getTargetContainer,
          getPopupContainer = _this$props.getPopupContainer,
          renderEmpty = _this$props.renderEmpty,
          csp = _this$props.csp,
          autoInsertSpaceInButton = _this$props.autoInsertSpaceInButton,
          form = _this$props.form,
          input = _this$props.input,
          locale = _this$props.locale,
          pageHeader = _this$props.pageHeader,
          componentSize = _this$props.componentSize,
          direction = _this$props.direction,
          space = _this$props.space;

      var config = _extends(_extends({}, context), {
        getPrefixCls: _this.getPrefixClsWrapper(context),
        csp: csp,
        autoInsertSpaceInButton: autoInsertSpaceInButton,
        locale: locale || legacyLocale,
        direction: direction,
        space: space
      });

      if (getTargetContainer) {
        config.getTargetContainer = getTargetContainer;
      }

      if (getPopupContainer) {
        config.getPopupContainer = getPopupContainer;
      }

      if (renderEmpty) {
        config.renderEmpty = renderEmpty;
      }

      if (pageHeader) {
        config.pageHeader = pageHeader;
      }

      if (input) {
        config.input = input;
      }

      var childNode = children; // Additional Form provider

      var validateMessages = {};

      if (locale && locale.Form && locale.Form.defaultValidateMessages) {
        validateMessages = locale.Form.defaultValidateMessages;
      }

      if (form && form.validateMessages) {
        validateMessages = _extends(_extends({}, validateMessages), form.validateMessages);
      }

      if (Object.keys(validateMessages).length > 0) {
        childNode = /*#__PURE__*/React.createElement(RcFormProvider, {
          validateMessages: validateMessages
        }, children);
      }

      return /*#__PURE__*/React.createElement(SizeContextProvider, {
        size: componentSize
      }, /*#__PURE__*/React.createElement(ConfigContext.Provider, {
        value: config
      }, /*#__PURE__*/React.createElement(LocaleProvider, {
        locale: locale || legacyLocale,
        _ANT_MARK__: ANT_MARK
      }, childNode)));
    };

    return _this;
  }

  _createClass(ConfigProvider, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      return /*#__PURE__*/React.createElement(LocaleReceiver, null, function (_, __, legacyLocale) {
        return /*#__PURE__*/React.createElement(ConfigConsumer, null, function (context) {
          return _this2.renderProvider(context, legacyLocale);
        });
      });
    }
  }]);

  return ConfigProvider;
}(React.Component);

export default ConfigProvider;