'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.configConsumerProps = exports.ConfigConsumer = undefined;

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var React = _interopRequireWildcard(_react);

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

var _localeProvider = require('antd/lib/locale-provider');

var _localeProvider2 = _interopRequireDefault(_localeProvider);

var _LocaleReceiver = require('antd/lib/locale-provider/LocaleReceiver');

var _LocaleReceiver2 = _interopRequireDefault(_LocaleReceiver);

var _context = require('antd/lib/config-provider/context');

var _pagePoint = require('../page-point');

var _pagePoint2 = _interopRequireDefault(_pagePoint);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }

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

exports.ConfigConsumer = _context.ConfigConsumer;
var configConsumerProps = exports.configConsumerProps = ['getPopupContainer', 'rootPrefixCls', 'getPrefixCls', 'renderEmpty', 'csp', 'autoInsertSpaceInButton', 'locale', 'pageHeader', 'pagePoint'];

var ConfigProvider = function (_React$Component) {
    (0, _inherits3['default'])(ConfigProvider, _React$Component);

    function ConfigProvider() {
        (0, _classCallCheck3['default'])(this, ConfigProvider);

        var _this = (0, _possibleConstructorReturn3['default'])(this, (ConfigProvider.__proto__ || Object.getPrototypeOf(ConfigProvider)).apply(this, arguments));

        _this.getPrefixCls = function (suffixCls, customizePrefixCls) {
            var _this$props$prefixCls = _this.props.prefixCls,
                prefixCls = _this$props$prefixCls === undefined ? 'ant' : _this$props$prefixCls;

            if (customizePrefixCls) return customizePrefixCls;
            return suffixCls ? prefixCls + '-' + suffixCls : prefixCls;
        };
        _this.renderProvider = function (context, legacyLocale) {
            var _this$props = _this.props,
                children = _this$props.children,
                getPopupContainer = _this$props.getPopupContainer,
                renderEmpty = _this$props.renderEmpty,
                csp = _this$props.csp,
                autoInsertSpaceInButton = _this$props.autoInsertSpaceInButton,
                locale = _this$props.locale,
                form = _this$props.form,
                pageHeader = _this$props.pageHeader,
                pagePoint = _this$props.pagePoint;

            var config = (0, _extends3['default'])((0, _extends3['default'])({}, context), { getPrefixCls: _this.getPrefixCls, csp: csp,
                autoInsertSpaceInButton: autoInsertSpaceInButton,
                pagePoint: pagePoint, locale: locale || legacyLocale });
            var setPagePoint = _pagePoint2['default'].setPagePoint;

            if (getPopupContainer) {
                config.getPopupContainer = getPopupContainer;
            }
            if (renderEmpty) {
                config.renderEmpty = renderEmpty;
            }
            if (pageHeader) {
                config.pageHeader = pageHeader;
            }
            if (typeof pagePoint === 'boolean') {
                setPagePoint({ enable: pagePoint });
            }
            var childNode = children;
            var validateMessages = {};
            if (locale && locale.Form && locale.Form.defaultValidateMessages) {
                validateMessages = locale.Form.defaultValidateMessages;
            }
            if (form && form.validateMessages) {
                validateMessages = (0, _extends3['default'])((0, _extends3['default'])({}, validateMessages), form.validateMessages);
            }
            if (Object.keys(validateMessages).length > 0) {
                childNode = React.createElement(
                    _rcFieldForm.FormProvider,
                    { validateMessages: validateMessages },
                    children
                );
            }
            return React.createElement(
                _context.ConfigContext.Provider,
                { value: config },
                React.createElement(
                    _localeProvider2['default'],
                    { locale: locale || legacyLocale, _ANT_MARK__: _localeProvider.ANT_MARK },
                    childNode
                )
            );
        };
        return _this;
    }

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

            return React.createElement(
                _LocaleReceiver2['default'],
                null,
                function (_, __, legacyLocale) {
                    return React.createElement(
                        _context.ConfigConsumer,
                        null,
                        function (context) {
                            return _this2.renderProvider(context, legacyLocale);
                        }
                    );
                }
            );
        }
    }]);
    return ConfigProvider;
}(React.Component);

exports['default'] = ConfigProvider;