import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _get from "@babel/runtime/helpers/get";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["index"];
import React from 'react';
import classNames from 'classnames';
import noop from 'lodash/noop';
import addEventListener from '../../../_util/addEventListener';
import warning from '../../../_util/warning';
import Steps from './Steps';
import Marks from './Marks';
import Handle from '../Handle';
import * as utils from '../utils';
export default function createSlider(Component) {
  var _class;

  return _class = /*#__PURE__*/function (_Component) {
    _inherits(ComponentEnhancer, _Component);

    var _super = _createSuper(ComponentEnhancer);

    function ComponentEnhancer(props) {
      var _this;

      _classCallCheck(this, ComponentEnhancer);

      _this = _super.call(this, props);

      _defineProperty(_assertThisInitialized(_this), "onMouseDown", function (e) {
        if (e.button !== 0) {
          return;
        }

        var isVertical = _this.props.vertical;
        var position = utils.getMousePosition(isVertical, e);

        if (!utils.isEventFromHandle(e, _this.handlesRefs)) {
          _this.dragOffset = 0;
        } else {
          var handlePosition = utils.getHandleCenterPosition(isVertical, e.target);
          _this.dragOffset = position - handlePosition;
          position = handlePosition;
        }

        _this.removeDocumentEvents();

        _this.onStart(position);

        _this.addDocumentMouseEvents();
      });

      _defineProperty(_assertThisInitialized(_this), "onTouchStart", function (e) {
        if (utils.isNotTouchEvent(e)) return;
        var isVertical = _this.props.vertical;
        var position = utils.getTouchPosition(isVertical, e);

        if (!utils.isEventFromHandle(e, _this.handlesRefs)) {
          _this.dragOffset = 0;
        } else {
          var handlePosition = utils.getHandleCenterPosition(isVertical, e.target);
          _this.dragOffset = position - handlePosition;
          position = handlePosition;
        }

        _this.onStart(position);

        _this.addDocumentTouchEvents();

        utils.pauseEvent(e);
      });

      _defineProperty(_assertThisInitialized(_this), "onFocus", function (e) {
        var _this$props = _this.props,
            onFocus = _this$props.onFocus,
            vertical = _this$props.vertical;

        if (utils.isEventFromHandle(e, _this.handlesRefs)) {
          var handlePosition = utils.getHandleCenterPosition(vertical, e.target);
          _this.dragOffset = 0;

          _this.onStart(handlePosition);

          utils.pauseEvent(e);

          if (onFocus) {
            onFocus(e);
          }
        }
      });

      _defineProperty(_assertThisInitialized(_this), "onBlur", function (e) {
        var onBlur = _this.props.onBlur;

        _this.onEnd(e);

        if (onBlur) {
          onBlur(e);
        }
      });

      _defineProperty(_assertThisInitialized(_this), "onMouseUp", function () {
        if (_this.handlesRefs[_this.prevMovedHandleIndex]) {
          _this.handlesRefs[_this.prevMovedHandleIndex].clickFocus();
        }

        _this.draggedFlag = true;
      });

      _defineProperty(_assertThisInitialized(_this), "onMouseMove", function (e) {
        _this.draggedFlag = true;

        if (!_this.sliderRef) {
          _this.onEnd();

          return;
        }

        var position = utils.getMousePosition(_this.props.vertical, e);

        _this.onMove(e, position - _this.dragOffset);
      });

      _defineProperty(_assertThisInitialized(_this), "onTouchMove", function (e) {
        if (utils.isNotTouchEvent(e) || !_this.sliderRef) {
          _this.onEnd();

          return;
        }

        var position = utils.getTouchPosition(_this.props.vertical, e);

        _this.onMove(e, position - _this.dragOffset);
      });

      _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
        if (_this.sliderRef && utils.isEventFromHandle(e, _this.handlesRefs)) {
          _this.onKeyboard(e);
        }
      });

      _defineProperty(_assertThisInitialized(_this), "saveSlider", function (slider) {
        _this.sliderRef = slider;
      });

      _defineProperty(_assertThisInitialized(_this), "onClickMarkLabel", function (e, value) {
        e.stopPropagation();

        _this.onChange({
          value: value
        });
      });

      if (process.env.NODE_ENV !== 'production') {
        var step = props.step,
            max = props.max,
            min = props.min;
        warning(step && Math.floor(step) === step ? (max - min) % step === 0 : true, 'Slider[max] - Slider[min] (%s) should be a multiple of Slider[step] (%s)', max - min, step);
      }

      _this.handlesRefs = {};
      _this.draggedFlag = false;
      return _this;
    }

    _createClass(ComponentEnhancer, [{
      key: "componentWillUnmount",
      value: function componentWillUnmount() {
        if (_get(_getPrototypeOf(ComponentEnhancer.prototype), "componentWillUnmount", this)) _get(_getPrototypeOf(ComponentEnhancer.prototype), "componentWillUnmount", this).call(this);
        this.removeDocumentEvents();
      }
    }, {
      key: "componentDidMount",
      value: function componentDidMount() {
        // Snapshot testing cannot handle refs, so be sure to null-check this.
        this.document = this.sliderRef && this.sliderRef.ownerDocument;
      }
    }, {
      key: "addDocumentTouchEvents",
      value: function addDocumentTouchEvents() {
        // just work for Chrome iOS Safari and Android Browser
        this.onTouchMoveListener = addEventListener(this.document, 'touchmove', this.onTouchMove);
        this.onTouchUpListener = addEventListener(this.document, 'touchend', this.onEnd);
      }
    }, {
      key: "addDocumentMouseEvents",
      value: function addDocumentMouseEvents() {
        this.onMouseMoveListener = addEventListener(this.document, 'mousemove', this.onMouseMove);
        this.onMouseUpListener = addEventListener(this.document, 'mouseup', this.onEnd);
      }
    }, {
      key: "removeDocumentEvents",
      value: function removeDocumentEvents() {
        if (this.draggedFlag) {
          /* eslint-disable no-unused-expressions */
          this.onTouchMoveListener && this.onTouchMoveListener.remove();
          this.onTouchUpListener && this.onTouchUpListener.remove();
          this.onMouseMoveListener && this.onMouseMoveListener.remove();
          this.onMouseUpListener && this.onMouseUpListener.remove();
          /* eslint-enable no-unused-expressions */

          this.draggedFlag = false;
        }
      }
    }, {
      key: "focus",
      value: function focus() {
        if (!this.props.disabled) {
          this.handlesRefs[0].focus();
        }
      }
    }, {
      key: "blur",
      value: function blur() {
        if (!this.props.disabled) {
          this.handlesRefs[0].blur();
        }
      }
    }, {
      key: "getSliderStart",
      value: function getSliderStart() {
        var slider = this.sliderRef;
        var rect = slider.getBoundingClientRect();
        return this.props.vertical ? rect.top : rect.left;
      }
    }, {
      key: "getSliderLength",
      value: function getSliderLength() {
        var slider = this.sliderRef;

        if (!slider) {
          return 0;
        }

        var coords = slider.getBoundingClientRect();
        return this.props.vertical ? coords.height : coords.width;
      }
    }, {
      key: "calcValue",
      value: function calcValue(offset) {
        var _this$props2 = this.props,
            vertical = _this$props2.vertical,
            min = _this$props2.min,
            max = _this$props2.max;
        var ratio = Math.abs(Math.max(offset, 0) / this.getSliderLength());
        var value = vertical ? (1 - ratio) * (max - min) + min : ratio * (max - min) + min;
        return value;
      }
    }, {
      key: "calcValueByPos",
      value: function calcValueByPos(position) {
        var pixelOffset = position - this.getSliderStart();
        var nextValue = this.trimAlignValue(this.calcValue(pixelOffset));
        return nextValue;
      }
    }, {
      key: "calcOffset",
      value: function calcOffset(value) {
        var _this$props3 = this.props,
            min = _this$props3.min,
            max = _this$props3.max;
        var ratio = (value - min) / (max - min);
        return ratio * 100;
      }
    }, {
      key: "saveHandle",
      value: function saveHandle(index, handle) {
        this.handlesRefs[index] = handle;
      }
    }, {
      key: "render",
      value: function render() {
        var _classNames;

        var _this$props4 = this.props,
            prefixCls = _this$props4.prefixCls,
            className = _this$props4.className,
            marks = _this$props4.marks,
            dots = _this$props4.dots,
            step = _this$props4.step,
            included = _this$props4.included,
            disabled = _this$props4.disabled,
            vertical = _this$props4.vertical,
            min = _this$props4.min,
            max = _this$props4.max,
            children = _this$props4.children,
            maximumTrackStyle = _this$props4.maximumTrackStyle,
            style = _this$props4.style,
            railStyle = _this$props4.railStyle,
            dotStyle = _this$props4.dotStyle,
            activeDotStyle = _this$props4.activeDotStyle;

        var _get$call = _get(_getPrototypeOf(ComponentEnhancer.prototype), "render", this).call(this),
            tracks = _get$call.tracks,
            handles = _get$call.handles;

        var sliderClassName = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-with-marks"), Object.keys(marks).length), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames, "".concat(prefixCls, "-vertical"), vertical), _defineProperty(_classNames, className, className), _classNames));
        return /*#__PURE__*/React.createElement("div", {
          ref: this.saveSlider,
          className: sliderClassName,
          onTouchStart: disabled ? noop : this.onTouchStart,
          onMouseDown: disabled ? noop : this.onMouseDown,
          onMouseUp: disabled ? noop : this.onMouseUp,
          onKeyDown: disabled ? noop : this.onKeyDown,
          onFocus: disabled ? noop : this.onFocus,
          onBlur: disabled ? noop : this.onBlur,
          style: style
        }, /*#__PURE__*/React.createElement("div", {
          className: "".concat(prefixCls, "-rail"),
          style: _objectSpread(_objectSpread({}, maximumTrackStyle), railStyle)
        }), tracks, /*#__PURE__*/React.createElement(Steps, {
          prefixCls: prefixCls,
          vertical: vertical,
          marks: marks,
          dots: dots,
          step: step,
          included: included,
          lowerBound: this.getLowerBound(),
          upperBound: this.getUpperBound(),
          max: max,
          min: min,
          dotStyle: dotStyle,
          activeDotStyle: activeDotStyle
        }), handles, /*#__PURE__*/React.createElement(Marks, {
          className: "".concat(prefixCls, "-mark"),
          onClickLabel: disabled ? noop : this.onClickMarkLabel,
          vertical: vertical,
          marks: marks,
          included: included,
          lowerBound: this.getLowerBound(),
          upperBound: this.getUpperBound(),
          max: max,
          min: min
        }), children);
      }
    }]);

    return ComponentEnhancer;
  }(Component), _defineProperty(_class, "displayName", "ComponentEnhancer(".concat(Component.displayName, ")")), _defineProperty(_class, "defaultProps", _objectSpread(_objectSpread({}, Component.defaultProps), {}, {
    prefixCls: 'rc-slider',
    className: '',
    min: 0,
    max: 100,
    step: 1,
    marks: {},
    handle: function handle(_ref) {
      var index = _ref.index,
          restProps = _objectWithoutProperties(_ref, _excluded);

      delete restProps.dragging;
      return /*#__PURE__*/React.createElement(Handle, _extends({}, restProps, {
        key: index
      }));
    },
    onBeforeChange: noop,
    onChange: noop,
    onAfterChange: noop,
    included: true,
    disabled: false,
    dots: false,
    vertical: false,
    trackStyle: [{}],
    handleStyle: [{}],
    railStyle: {},
    dotStyle: {},
    activeDotStyle: {}
  })), _class;
}
//# sourceMappingURL=createSlider.js.map
