import _extends from "babel-runtime/helpers/extends";
import _defineProperty from "babel-runtime/helpers/defineProperty";
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";
import React, { Component } from "react";
import hoistNonReactStatic from "hoist-non-react-statics";
import PagePoint from "./index";
var sendPagePoint = PagePoint.sendPagePoint,
    regKey = PagePoint.pagePointConfig.regKey;

export default function PagePointHOC(WrappedComponent, componentType) {
    var enableForwardRef = ["MonthPicker", "RangePicker", "WeekPicker"];

    var WithPagePoint = function (_Component) {
        _inherits(WithPagePoint, _Component);

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

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

            var _this$props = _this.props,
                dataRegId = _this$props[regKey],
                onChange = _this$props.onChange;

            var newChange = void 0;
            _this.composedHandler = {};
            _this.origValue = "";
            switch (componentType) {
                case "CheckboxGroup":
                    if (dataRegId) {
                        newChange = function newChange(value) {
                            var _sendPagePoint;

                            onChange && onChange(value);
                            sendPagePoint(null, (_sendPagePoint = {
                                eventType: "change",
                                tagName: "checkboxGroup",
                                value: value
                            }, _defineProperty(_sendPagePoint, regKey, dataRegId), _defineProperty(_sendPagePoint, "origValue", _this.origValue), _sendPagePoint));
                            _this.origValue = value;
                        };
                        _this.composedHandler = {
                            onChange: newChange
                        };
                    }
                    break;
                case "RadioGroup":
                    if (dataRegId) {
                        newChange = function newChange(e) {
                            var _sendPagePoint2;

                            onChange && onChange(e);
                            sendPagePoint(e.nativeEvent, (_sendPagePoint2 = {
                                eventType: "change",
                                tagName: "radioGroup"
                            }, _defineProperty(_sendPagePoint2, regKey, dataRegId), _defineProperty(_sendPagePoint2, "value", e.target.value), _defineProperty(_sendPagePoint2, "origValue", _this.origValue), _sendPagePoint2));
                            _this.origValue = e.target.value;
                        };
                        _this.composedHandler = {
                            onChange: newChange
                        };
                    }
                    break;
                case "MonthPicker":
                case "RangePicker":
                case "WeekPicker":
                    if (dataRegId) {
                        newChange = function newChange(dateMomnet, dateString) {
                            var _sendPagePoint3;

                            onChange && onChange(dateMomnet, dateString);
                            sendPagePoint(null, (_sendPagePoint3 = {
                                eventType: "change",
                                tagName: "monthPicker",
                                value: dateString
                            }, _defineProperty(_sendPagePoint3, regKey, dataRegId), _defineProperty(_sendPagePoint3, "origValue", _this.origValue), _sendPagePoint3));
                            _this.origValue = dateString;
                        };
                        _this.composedHandler = {
                            onChange: newChange
                        };
                    }
                    break;
                default:
                    break;
            }
            return _this;
        }

        _createClass(WithPagePoint, [{
            key: "render",
            value: function render() {
                var forwardedRef = this.props.forwardedRef;

                return React.createElement(WrappedComponent, _extends({ ref: forwardedRef }, this.props, this.composedHandler));
            }
        }]);

        return WithPagePoint;
    }(Component);

    hoistNonReactStatic(WithPagePoint, WrappedComponent);
    return enableForwardRef.indexOf(componentType) > -1 ? React.forwardRef(function (props, ref) {
        return React.createElement(WithPagePoint, _extends({}, props, { forwardedRef: ref }));
    }) : WithPagePoint;
}