"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});

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 _react2 = _interopRequireDefault(_react);

var _fdx = require("@whalecloud/fdx");

var _inputNumber = require("antd/lib/input-number");

var _inputNumber2 = _interopRequireDefault(_inputNumber);

var _pagePointEnhanced = require("../page-point/pagePointEnhanced");

var _pagePointEnhanced2 = _interopRequireDefault(_pagePointEnhanced);

var _configProvider = require("../config-provider");

require("antd/lib/input-number/style");

require("./index.less");

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

var __rest = undefined && undefined.__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;
};

var SizeType;
(function (SizeType) {
    SizeType["BIG"] = "big";
    SizeType["SMALL"] = "smnall";
})(SizeType || (SizeType = {}));

var FdxInputNumber = function (_Component) {
    (0, _inherits3["default"])(FdxInputNumber, _Component);

    function FdxInputNumber(props) {
        (0, _classCallCheck3["default"])(this, FdxInputNumber);

        var _this = (0, _possibleConstructorReturn3["default"])(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 _react2["default"].createElement(
                    "div",
                    { className: "sku-input-number" },
                    _react2["default"].createElement(
                        _fdx.Row,
                        { type: "flex", justify: "start", className: sizeTypeVal },
                        _react2["default"].createElement(
                            _fdx.Button,
                            { className: "number-button minus-button", disabled: minusDisabled, onClick: function onClick() {
                                    _this.changeNumber(-1, false);
                                } },
                            _react2["default"].createElement(_fdx.Icon, { type: "minus" })
                        ),
                        _react2["default"].createElement(_inputNumber2["default"], (0, _extends3["default"])({ ref: forwardedRef }, rest, eventprops, { "data-ui-role": "InputNumber" })),
                        _react2["default"].createElement(
                            _fdx.Button,
                            { className: "number-button plus-button", disabled: plusDisabled, onClick: function onClick() {
                                    _this.changeNumber(1, false);
                                } },
                            _react2["default"].createElement(_fdx.Icon, { type: "plus" })
                        )
                    )
                );
            }
            return _react2["default"].createElement(
                "div",
                { className: "sku-input-number" },
                _react2["default"].createElement(
                    _fdx.Row,
                    { type: "flex", justify: "start", className: sizeTypeVal },
                    _react2["default"].createElement(
                        _fdx.Button,
                        { className: "number-button minus-button", disabled: minusDisabled, onClick: function onClick() {
                                _this.changeNumber(-1, false);
                            } },
                        _react2["default"].createElement(_fdx.Icon, { type: "minus" })
                    ),
                    _react2["default"].createElement(_inputNumber2["default"], (0, _extends3["default"])({ ref: forwardedRef }, rest)),
                    _react2["default"].createElement(
                        _fdx.Button,
                        { className: "number-button plus-button", disabled: plusDisabled, onClick: function onClick() {
                                _this.changeNumber(1, false);
                            } },
                        _react2["default"].createElement(_fdx.Icon, { type: "plus" })
                    )
                )
            );
        };
        return _this;
    }

    (0, _createClass3["default"])(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 _react2["default"].createElement(
                _configProvider.ConfigConsumer,
                null,
                function (_ref) {
                    var pagePoint = _ref.pagePoint;
                    return pagePoint ? _react2["default"].createElement(_pagePointEnhanced2["default"], (0, _extends3["default"])({ compType: "InputNumber" }, _this2.props, { render: function render(eventprops) {
                            return _this2.renderSkuInputNumber(eventprops);
                        } })) : _this2.renderSkuInputNumber(null);
                }
            );
        }
    }]);
    return FdxInputNumber;
}(_react.Component);

var SkuInputNumber = _react2["default"].forwardRef(function (props, ref) {
    return _react2["default"].createElement(FdxInputNumber, (0, _extends3["default"])({}, props, { forwardedRef: ref }));
});
exports["default"] = SkuInputNumber;
module.exports = exports['default'];