"use strict";

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _util = require("../../util");

var _balloon = _interopRequireDefault(require("../../balloon"));

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

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

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); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var Tooltip = _balloon["default"].Tooltip;
var noop = _util.func.noop;

function _getStyle(min, max, value, rtl) {
  if (rtl) {
    return {
      left: "".concat((0, _utils.getPercent)(min, max, max + min - value[1]), "%"),
      right: "".concat((0, _utils.getPercent)(min, max, value[0]), "%")
    };
  }

  return {
    left: "".concat((0, _utils.getPercent)(min, max, value[0]), "%"),
    right: "".concat(100 - (0, _utils.getPercent)(min, max, value[1]), "%")
  };
}

function sliderFrag(props) {
  var prefix = props.prefix,
      min = props.min,
      max = props.max,
      value = props.value,
      disabled = props.disabled,
      onMouseEnter = props.onMouseEnter,
      onMouseLeave = props.onMouseLeave,
      onMouseDown = props.onMouseDown,
      rtl = props.rtl;
  var activeClass = !disabled && props.hasMovingClass ? " ".concat(prefix, "range-active") : '';
  return _react["default"].createElement("div", {
    className: "".concat(prefix, "range-frag").concat(activeClass),
    style: _getStyle(min, max, value, rtl),
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    onMouseDown: onMouseDown
  }, _react["default"].createElement("div", {
    className: "".concat(prefix, "range-selected")
  }), _react["default"].createElement("div", {
    className: "".concat(prefix, "range-slider")
  }, _react["default"].createElement("div", {
    className: "".concat(prefix, "range-slider-inner")
  })), _react["default"].createElement("div", {
    className: "".concat(prefix, "range-slider")
  }, _react["default"].createElement("div", {
    className: "".concat(prefix, "range-slider-inner")
  })));
}

sliderFrag.propTypes = {
  prefix: _propTypes["default"].string,
  min: _propTypes["default"].number,
  max: _propTypes["default"].number,
  hasMovingClass: _propTypes["default"].bool,
  onMouseEnter: _propTypes["default"].func,
  onMouseLeave: _propTypes["default"].func,
  onMouseDown: _propTypes["default"].func,
  value: _propTypes["default"].arrayOf(_propTypes["default"].number),
  disabled: _propTypes["default"].bool,
  rtl: _propTypes["default"].bool
};

var FixedSlider =
/*#__PURE__*/
function (_React$Component) {
  _inherits(FixedSlider, _React$Component);

  function FixedSlider(props) {
    var _this;

    _classCallCheck(this, FixedSlider);

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

    _defineProperty(_assertThisInitialized(_this), "_onMouseDown", function () {
      _this.keyState = 'down';

      _this.setState({
        hasMovingClass: true
      });

      _this._addDocumentEvents();
    });

    _defineProperty(_assertThisInitialized(_this), "_onMouseUp", function () {
      if (_this.keyState === 'down') {
        _this.keyState = '';

        _this._removeDocumentEvents();

        _this.setState({
          hasMovingClass: false
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "_addDocumentEvents", function () {
      _this._onMouseUpListener = _util.events.on(document, 'mouseup', _this._onMouseUp);
    });

    _defineProperty(_assertThisInitialized(_this), "_removeDocumentEvents", function () {
      if (_this._onMouseUpListener) {
        _this._onMouseUpListener.off();

        _this._onMouseUpListener = null;
      }
    });

    _this.state = {
      hasMovingClass: false,
      tooltipAnimation: true
    };
    return _this;
  }

  _createClass(FixedSlider, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          hasTip = _this$props.hasTip,
          value = _this$props.value,
          tipRender = _this$props.tipRender,
          tooltipVisible = _this$props.tooltipVisible,
          hasMovingClass = _this$props.hasMovingClass;
      var addedProps = {
        hasMovingClass: hasMovingClass || this.state.hasMovingClass,
        onMouseDown: this._onMouseDown
      };
      return hasTip ? _react["default"].createElement(Tooltip, {
        popupContainer: function popupContainer(target) {
          return target.parentNode;
        },
        popupProps: {
          visible: tooltipVisible || hasMovingClass,
          animation: this.state.tooltipAnimation ? {
            "in": 'expandInUp',
            out: 'expandOutDown'
          } : false
        },
        type: "primary",
        trigger: sliderFrag(_objectSpread({}, this.props, {}, addedProps)),
        align: "t"
      }, tipRender("".concat(value[0], "-").concat(value[1]))) : sliderFrag(_objectSpread({}, this.props, {}, addedProps));
    }
  }]);

  return FixedSlider;
}(_react["default"].Component);

exports["default"] = FixedSlider;

_defineProperty(FixedSlider, "propTypes", {
  hasTip: _propTypes["default"].bool,
  tooltipVisible: _propTypes["default"].bool,
  onTooltipVisibleChange: _propTypes["default"].func,
  tooltipAnimation: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].object]),
  value: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].arrayOf(_propTypes["default"].number)]),
  tipRender: _propTypes["default"].func,
  disabled: _propTypes["default"].bool,
  hasMovingClass: _propTypes["default"].bool,
  rtl: _propTypes["default"].bool
});

_defineProperty(FixedSlider, "defaultProps", {
  disabled: false,
  hasTip: true,
  onChange: noop,
  onProcess: noop,
  tipRender: function tipRender(value) {
    return value;
  },
  reverse: false,
  rtl: false
});