"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

require("maycur-antd/lib/button/style/css");

var _button = _interopRequireDefault(require("maycur-antd/lib/button"));

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

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

require("maycur-antd/lib/message/style/css");

var _message2 = _interopRequireDefault(require("maycur-antd/lib/message"));

require("maycur-antd/lib/input/style/css");

var _input = _interopRequireDefault(require("maycur-antd/lib/input"));

var _react = _interopRequireWildcard(require("react"));

var _lodash = _interopRequireDefault(require("lodash"));

var _utils = _interopRequireDefault(require("../../../utils/utils"));

var _i18next = _interopRequireDefault(require("i18next"));

var _mkTranslate = _interopRequireDefault(require("../../../utils/mkTranslate"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

var prefix = _utils["default"].prefixCls;
var InputGroup = _input["default"].Group;

var InputRange =
/*#__PURE__*/
function (_Component) {
  _inherits(InputRange, _Component);

  function InputRange(props) {
    var _this;

    _classCallCheck(this, InputRange);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(InputRange).call(this, props));

    _this.localeProcess = function () {
      _mkTranslate["default"].getNameSpace('MkTable').then(function (t) {
        _this.setState({
          t: t
        });
      });
    };

    _this.onBlurMinAmount = function (e) {
      var maxAmount = _this.state.maxAmount;
      var _this$props = _this.props,
          showNumberRangeOkBtn = _this$props.showNumberRangeOkBtn,
          setSelectedKeys = _this$props.setSelectedKeys,
          confirm = _this$props.confirm;
      var value = e.target.value;
      var nextMaxAmount = maxAmount;

      if ((nextMaxAmount === undefined || nextMaxAmount === null) && value && value >= 0) {
        nextMaxAmount = value;
      }

      _this.setState({
        maxAmount: nextMaxAmount
      }, function () {
        if (!showNumberRangeOkBtn) {
          setSelectedKeys("".concat(_this.state.minAmount, "~").concat(_this.state.maxAmount));
          confirm();
        }
      });
    };

    _this.onChangeMinAmount = function (value) {
      _this.setState({
        minAmount: value
      });
    };

    _this.onBlurMaxAmount = function (e) {
      var minAmount = _this.state.minAmount;
      var _this$props2 = _this.props,
          showNumberRangeOkBtn = _this$props2.showNumberRangeOkBtn,
          setSelectedKeys = _this$props2.setSelectedKeys,
          confirm = _this$props2.confirm;
      var value = e.target.value;
      var nextMinAmount = minAmount;

      if ((nextMinAmount === undefined || nextMinAmount === null) && value && value >= 0) {
        nextMinAmount = value;
      }

      _this.setState({
        minAmount: nextMinAmount
      }, function () {
        if (!showNumberRangeOkBtn) {
          setSelectedKeys("".concat(_this.state.minAmount, "~").concat(_this.state.maxAmount));
          confirm();
        }
      });
    };

    _this.onChangeMaxAmount = function (value) {
      _this.setState({
        maxAmount: value
      });
    };

    _this.atLastValue = function () {
      var _this$props3 = _this.props,
          confirm = _this$props3.confirm,
          setSelectedKeys = _this$props3.setSelectedKeys;
      var _this$state = _this.state,
          minAmount = _this$state.minAmount,
          maxAmount = _this$state.maxAmount,
          t = _this$state.t;
      var result = true;

      if ((minAmount || !minAmount && minAmount === 0) && (maxAmount || !maxAmount && maxAmount === 0)) {
        if (Number(maxAmount) < Number(minAmount)) {
          result = false;

          _message2["default"].warn(t('numberRangeFilter.warn'));
        }
      }

      if (result) {
        setSelectedKeys("".concat(!minAmount && minAmount !== 0 ? '' : minAmount, "~").concat(!maxAmount && maxAmount !== 0 ? '' : maxAmount));
        confirm();
      }
    };

    _this.state = {
      minAmount: undefined,
      maxAmount: undefined,
      t: _mkTranslate["default"].noop()
    };

    _this.localeProcess();

    return _this;
  }

  _createClass(InputRange, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var selectedKeys = this.props.selectedKeys;

      if (selectedKeys && !_lodash["default"].isEmpty(selectedKeys)) {
        this.setState({
          minAmount: !selectedKeys.minAmount && selectedKeys.minAmount !== 0 ? undefined : selectedKeys.minAmount,
          maxAmount: !selectedKeys.maxAmount && selectedKeys.maxAmount !== 0 ? undefined : selectedKeys.maxAmount
        });
      }
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if (this.props.selectedKeys !== nextProps.selectedKeys && typeof nextProps.selectedKeys === 'string') {
        var selectedKeys = nextProps.selectedKeys;

        var _selectedKeys$split = selectedKeys.split('~'),
            _selectedKeys$split2 = _slicedToArray(_selectedKeys$split, 2),
            minAmount = _selectedKeys$split2[0],
            maxAmount = _selectedKeys$split2[1];

        this.setState({
          minAmount: minAmount,
          maxAmount: maxAmount
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var t = this.state.t;
      return _react["default"].createElement("div", {
        className: "".concat(prefix, "-input-range-wraper")
      }, _react["default"].createElement(InputGroup, {
        compact: true
      }, _react["default"].createElement(_inputNumber["default"], {
        style: {
          textAlign: 'left'
        },
        placeholder: t('numberRangeFilter.min'),
        value: this.state.minAmount,
        onChange: this.onChangeMinAmount,
        onBlur: this.onBlurMinAmount
      }), _react["default"].createElement(_input["default"], {
        style: {
          width: 30,
          borderLeft: 0,
          pointerEvents: 'none',
          backgroundColor: '#fff'
        },
        placeholder: "~",
        disabled: true
      }), _react["default"].createElement(_inputNumber["default"], {
        style: {
          textAlign: 'left',
          borderLeft: 0
        },
        placeholder: t('numberRangeFilter.max'),
        value: this.state.maxAmount,
        onChange: this.onChangeMaxAmount,
        onBlur: this.onBlurMaxAmount
      }), this.props.showNumberRangeOkBtn && _react["default"].createElement(_button["default"], {
        size: "small",
        type: "primary",
        style: {
          marginLeft: 5
        },
        onClick: this.atLastValue
      }, _i18next["default"].t('MkGlobal:ok'))));
    }
  }]);

  return InputRange;
}(_react.Component);

InputRange.defaultProps = {
  showNumberRangeOkBtn: true
};
var _default = InputRange;
exports["default"] = _default;