"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "ConfigConsumer", {
  enumerable: true,
  get: function get() {
    return _context.ConfigConsumer;
  }
});
Object.defineProperty(exports, "ConfigContext", {
  enumerable: true,
  get: function get() {
    return _context.ConfigContext;
  }
});
exports["default"] = exports.configConsumerProps = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var React = _interopRequireWildcard(require("react"));

var _rcFieldForm = require("rc-field-form");

var _localeProvider = _interopRequireWildcard(require("../locale-provider"));

var _LocaleReceiver = _interopRequireDefault(require("../locale-provider/LocaleReceiver"));

var _context = require("./context");

var _SizeContext = require("./SizeContext");

// TODO: remove this lint
// SFC has specified a displayName, but not worked.

/* eslint-disable react/display-name */
var configConsumerProps = ['getTargetContainer', 'getPopupContainer', 'rootPrefixCls', 'getPrefixCls', 'renderEmpty', 'csp', 'autoInsertSpaceInButton', 'locale', 'pageHeader'];
exports.configConsumerProps = configConsumerProps;

var ConfigProvider = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(ConfigProvider, _React$Component);

  var _super = (0, _createSuper2["default"])(ConfigProvider);

  function ConfigProvider() {
    var _this;

    (0, _classCallCheck2["default"])(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 = (0, _extends2["default"])((0, _extends2["default"])({}, 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 = (0, _extends2["default"])((0, _extends2["default"])({}, validateMessages), form.validateMessages);
      }

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

      return /*#__PURE__*/React.createElement(_SizeContext.SizeContextProvider, {
        size: componentSize
      }, /*#__PURE__*/React.createElement(_context.ConfigContext.Provider, {
        value: config
      }, /*#__PURE__*/React.createElement(_localeProvider["default"], {
        locale: locale || legacyLocale,
        _ANT_MARK__: _localeProvider.ANT_MARK
      }, childNode)));
    };

    return _this;
  }

  (0, _createClass2["default"])(ConfigProvider, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      return /*#__PURE__*/React.createElement(_LocaleReceiver["default"], null, function (_, __, legacyLocale) {
        return /*#__PURE__*/React.createElement(_context.ConfigConsumer, null, function (context) {
          return _this2.renderProvider(context, legacyLocale);
        });
      });
    }
  }]);
  return ConfigProvider;
}(React.Component);

var _default = ConfigProvider;
exports["default"] = _default;