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 React, { Component } from "react";
import { Icon, Button, Row } from '@whalecloud/fdx';
import AntdInputNumber from "antd/es/input-number";
import PagePoint from "../page-point/pagePointEnhanced";
import { ConfigConsumer } from "../config-provider";
import "antd/es/input-number/style";
import "./index.less";
var SizeType;
(function (SizeType) {
    SizeType["BIG"] = "big";
    SizeType["SMALL"] = "smnall";
})(SizeType || (SizeType = {}));

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

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

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

        _this.state = {
            number: 0,
            minusDisabled: false,
            plusDisabled: false
        };
        _this.setNumAndDisabledToState = function (ret, isInit) {
            var _this$props = _this.props,
                max = _this$props.max,
                min = _this$props.min,
                disabled = _this$props.disabled,
                onChange = _this$props.onChange;

            var minusDisabled = false;
            var plusDisabled = false;
            if (max != null && ret >= max) {
                ret = max;
                plusDisabled = true;
            }
            if (ret < 0) {
                ret = 0;
            }
            if (min != null && ret <= min) {
                ret = min;
                minusDisabled = true;
            }
            if (!isInit) {
                onChange && onChange(ret);
            } else {
                if (disabled) {
                    minusDisabled = true;
                    plusDisabled = true;
                }
            }
            _this.setState({
                number: ret,
                minusDisabled: minusDisabled,
                plusDisabled: plusDisabled
            });
        };
        _this.changeNumber = function (num, isInit) {
            var number = _this.state.number;

            var ret = number + num;
            _this.setNumAndDisabledToState(ret, isInit);
        };
        _this.onAntdInputNumberChange = function (val) {
            if (Number(val)) {
                _this.setNumAndDisabledToState(val, false);
            }
        };
        _this.renderSkuInputNumber = function (eventprops) {
            var _a = _this.props,
                forwardedRef = _a.forwardedRef,
                sizeType = _a.sizeType,
                rest = __rest(_a, ["forwardedRef", "sizeType"]);var disabled = _this.props.disabled;

            var sizeTypeVal = sizeType === SizeType.BIG ? 'sku-big' : 'sku-small';
            var _this$state = _this.state,
                number = _this$state.number,
                minusDisabled = _this$state.minusDisabled,
                plusDisabled = _this$state.plusDisabled;
            // 如果disabled传进来就是不可用就都不可用

            if (disabled) {
                minusDisabled = true;
                plusDisabled = true;
            }
            rest.value = number;
            rest.onChange = function (val) {
                _this.onAntdInputNumberChange(val);
            };
            if (eventprops) {
                return React.createElement(
                    "div",
                    { className: "sku-input-number" },
                    React.createElement(
                        Row,
                        { type: "flex", justify: "start", className: sizeTypeVal },
                        React.createElement(
                            Button,
                            { className: "number-button minus-button", disabled: minusDisabled, onClick: function onClick() {
                                    _this.changeNumber(-1, false);
                                } },
                            React.createElement(Icon, { type: "minus" })
                        ),
                        React.createElement(AntdInputNumber, _extends({ ref: forwardedRef }, rest, eventprops, { "data-ui-role": "InputNumber" })),
                        React.createElement(
                            Button,
                            { className: "number-button plus-button", disabled: plusDisabled, onClick: function onClick() {
                                    _this.changeNumber(1, false);
                                } },
                            React.createElement(Icon, { type: "plus" })
                        )
                    )
                );
            }
            return React.createElement(
                "div",
                { className: "sku-input-number" },
                React.createElement(
                    Row,
                    { type: "flex", justify: "start", className: sizeTypeVal },
                    React.createElement(
                        Button,
                        { className: "number-button minus-button", disabled: minusDisabled, onClick: function onClick() {
                                _this.changeNumber(-1, false);
                            } },
                        React.createElement(Icon, { type: "minus" })
                    ),
                    React.createElement(AntdInputNumber, _extends({ ref: forwardedRef }, rest)),
                    React.createElement(
                        Button,
                        { className: "number-button plus-button", disabled: plusDisabled, onClick: function onClick() {
                                _this.changeNumber(1, false);
                            } },
                        React.createElement(Icon, { type: "plus" })
                    )
                )
            );
        };
        return _this;
    }

    _createClass(FdxInputNumber, [{
        key: "componentDidMount",
        value: function componentDidMount() {
            var _props = this.props,
                value = _props.value,
                defaultValue = _props.defaultValue;

            var num = 1;
            if (value != null) {
                num = value;
            } else if (defaultValue != null) {
                num = defaultValue;
            }
            this.setNumAndDisabledToState(num, true);
        }
    }, {
        key: "componentWillReceiveProps",
        value: function componentWillReceiveProps(nextProps) {
            this.setState({
                number: nextProps.value
            });
        }
    }, {
        key: "render",
        value: function render() {
            var _this2 = this;

            return React.createElement(
                ConfigConsumer,
                null,
                function (_ref) {
                    var pagePoint = _ref.pagePoint;
                    return pagePoint ? React.createElement(PagePoint, _extends({ compType: "InputNumber" }, _this2.props, { render: function render(eventprops) {
                            return _this2.renderSkuInputNumber(eventprops);
                        } })) : _this2.renderSkuInputNumber(null);
                }
            );
        }
    }]);

    return FdxInputNumber;
}(Component);

var SkuInputNumber = React.forwardRef(function (props, ref) {
    return React.createElement(FdxInputNumber, _extends({}, props, { forwardedRef: ref }));
});
export default SkuInputNumber;