import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var __rest = this && this.__rest || function (s, e) {
    var t = {};
    for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
    }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
        if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
    }
    return t;
};
import * as React from 'react';
import classNames from 'classnames';
import Icon from '../../icon';
import RCPicker from 'rc-picker';
import enUS from '../locale/en_US';
import { getPlaceholder } from '../util';
import devWarning from 'antd/es/_util/warning';
import { ConfigContext } from 'antd/es/config-provider/context';
import LocaleReceiver from 'antd/es/locale-provider/LocaleReceiver';
import PagePoint from "../../page-point";
import { getTimeProps, Components } from '.';
var sendPagePoint = PagePoint.sendPagePoint,
    regKey = PagePoint.pagePointConfig.regKey,
    clickType = PagePoint.EventType.clickType;

export default function generatePicker(generateConfig) {
    function getPicker(picker, displayName) {
        var Picker = function (_React$Component) {
            _inherits(Picker, _React$Component);

            function Picker(props) {
                _classCallCheck(this, Picker);

                var _this = _possibleConstructorReturn(this, (Picker.__proto__ || Object.getPrototypeOf(Picker)).call(this, props));

                _this.pickerRef = React.createRef();
                _this.focus = function () {
                    if (_this.pickerRef.current) {
                        _this.pickerRef.current.focus();
                    }
                };
                _this.blur = function () {
                    if (_this.pickerRef.current) {
                        _this.pickerRef.current.blur();
                    }
                };
                _this.getDefaultLocale = function () {
                    var locale = _this.props.locale;

                    var result = _extends(_extends({}, enUS), locale);
                    result.lang = _extends(_extends({}, result.lang), (locale || {}).lang);
                    return result;
                };
                _this.renderPicker = function (locale) {
                    var _classNames;

                    var _this$context = _this.context,
                        getPrefixCls = _this$context.getPrefixCls,
                        getPopupContainer = _this$context.getPopupContainer,
                        pagePoint = _this$context.pagePoint;
                    var _a = _this.props,
                        customizePrefixCls = _a.prefixCls,
                        customizeGetPopupContainer = _a.getPopupContainer,
                        getCalendarContainer = _a.getCalendarContainer,
                        monthCellContentRender = _a.monthCellContentRender,
                        customizeMonthCellRender = _a.monthCellRender,
                        className = _a.className,
                        onChange = _a.onChange,
                        customizeSize = _a.size,
                        _a$bordered = _a.bordered,
                        bordered = _a$bordered === undefined ? true : _a$bordered,
                        placeholder = _a.placeholder,
                        showDateInput = _a.showDateInput,
                        inputReadOnly = _a.inputReadOnly,
                        _b = regKey,
                        dataRegId = _a[_b],
                        restProps = __rest(_a, ["prefixCls", "getPopupContainer", "getCalendarContainer", "monthCellContentRender", "monthCellRender", "className", "onChange", "size", "bordered", "placeholder", "showDateInput", "inputReadOnly", typeof _b === "symbol" ? _b : _b + ""]);var _this$props = _this.props,
                        format = _this$props.format,
                        showTime = _this$props.showTime;

                    var prefixCls = getPrefixCls('picker', customizePrefixCls);
                    var additionalProps = {
                        showToday: true
                    };
                    var newChange = onChange;
                    if (pagePoint && dataRegId) {
                        newChange = function newChange(dateMomnet, dateString) {
                            var _sendPagePoint;

                            onChange && onChange(dateMomnet, dateString);
                            var dateStr = dateString;
                            // rangepicker为数组
                            if (Array.isArray(dateString)) {
                                dateStr = JSON.stringify(dateString);
                            }
                            // change 发送 click
                            sendPagePoint((_sendPagePoint = {
                                eventType: clickType,
                                tagname: "datetimePicker",
                                value: dateStr
                            }, _defineProperty(_sendPagePoint, regKey, dataRegId), _defineProperty(_sendPagePoint, 'origValue', _this.origValue), _sendPagePoint));
                            _this.origValue = dateStr;
                        };
                    }
                    var inputRO = showDateInput === false ? inputReadOnly || true : inputReadOnly;
                    var additionalOverrideProps = {};
                    if (picker) {
                        additionalOverrideProps.picker = picker;
                    }
                    var mergedPicker = picker || _this.props.picker;
                    additionalOverrideProps = _extends(_extends(_extends({}, additionalOverrideProps), showTime ? getTimeProps(_extends({ format: format, picker: mergedPicker }, showTime)) : {}), mergedPicker === 'time' ? getTimeProps(_extends(_extends({ format: format }, _this.props), { picker: mergedPicker })) : {});
                    var mergedSize = customizeSize;
                    return React.createElement(RCPicker, _extends({ ref: _this.pickerRef, placeholder: getPlaceholder(mergedPicker, locale, placeholder), suffixIcon: mergedPicker === 'time' ? React.createElement(Icon, { type: 'clock-circle' }) : React.createElement(Icon, { type: 'calendar' }), clearIcon: React.createElement(Icon, { type: 'close-circle' }), allowClear: true
                        //  transitionName="slide-up"
                    }, additionalProps, restProps, additionalOverrideProps, { locale: locale.lang, className: classNames(className, (_classNames = {}, _defineProperty(_classNames, prefixCls + '-' + mergedSize, mergedSize), _defineProperty(_classNames, prefixCls + '-borderless', !bordered), _classNames)), prefixCls: prefixCls, getPopupContainer: customizeGetPopupContainer || getCalendarContainer || getPopupContainer, monthCellRender: customizeMonthCellRender || monthCellContentRender, generateConfig: generateConfig, prevIcon: React.createElement('span', { className: prefixCls + '-prev-icon' }), nextIcon: React.createElement('span', { className: prefixCls + '-next-icon' }), superPrevIcon: React.createElement('span', { className: prefixCls + '-super-prev-icon' }), superNextIcon: React.createElement('span', { className: prefixCls + '-super-next-icon' }), components: Components, onChange: newChange, inputReadOnly: inputRO }));
                };
                devWarning(picker !== 'quarter', displayName, 'DatePicker.' + displayName + ' is legacy usage. Please use DatePicker[picker=\'' + picker + '\'] directly.');
                return _this;
            }

            _createClass(Picker, [{
                key: 'render',
                value: function render() {
                    return React.createElement(
                        LocaleReceiver,
                        { componentName: 'DatePicker', defaultLocale: this.getDefaultLocale },
                        this.renderPicker
                    );
                }
            }]);

            return Picker;
        }(React.Component);

        Picker.componentName = 'Picker';
        Picker.contextType = ConfigContext;
        if (displayName) {
            Picker.displayName = displayName;
        }
        return Picker;
    }
    var DatePicker = getPicker();
    var WeekPicker = getPicker('week', 'WeekPicker');
    var MonthPicker = getPicker('month', 'MonthPicker');
    var YearPicker = getPicker('year', 'YearPicker');
    var TimePicker = getPicker('time', 'TimePicker');
    var QuarterPicker = getPicker('quarter', 'QuarterPicker');
    return { DatePicker: DatePicker, WeekPicker: WeekPicker, MonthPicker: MonthPicker, YearPicker: YearPicker, TimePicker: TimePicker, QuarterPicker: QuarterPicker };
}