"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _utils = require("../Slider/utils");
var _RangeSliderTrack = require("./RangeSliderTrack");
var _SliderTrackHighlight = require("../Slider/SliderTrackHighlight");
var _utils2 = require("../../common/utils");
var _SliderKnobHandler = _interopRequireDefault(require("./SliderKnobHandler"));
var _constants = require("./constants");
var _utils3 = require("./utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var Container = _styledComponents.default.div.withConfig({
  displayName: "RangeSlider__Container",
  componentId: "sc-omt3f0-0"
})(["display:flex;align-items:center;height:", ";margin:0 ", ";"], function (p) {
  return "".concat((0, _utils.getKnobSize)(p.$variant), "px");
}, function (p) {
  return "".concat((0, _utils.getKnobSize)(p.$variant) / 2, "px");
});
var RangeSlider = function RangeSlider(_ref) {
  var defaultHighValue = _ref.defaultHighValue,
    defaultLowValue = _ref.defaultLowValue,
    disabledProp = _ref.disabled,
    max = _ref.max,
    min = _ref.min,
    onChange = _ref.onChange,
    readOnly = _ref.readOnly,
    sliderColor = _ref.sliderColor,
    step = _ref.step,
    _ref$variant = _ref.variant,
    variant = _ref$variant === void 0 ? 'big' : _ref$variant;
  var trackRef = (0, _react.useRef)(null);
  var _useState = (0, _react.useState)(false),
    _useState2 = _slicedToArray(_useState, 2),
    initialized = _useState2[0],
    setInitialized = _useState2[1];
  var _useState3 = (0, _react.useState)(_constants.KnobType.LOW),
    _useState4 = _slicedToArray(_useState3, 2),
    activeHandle = _useState4[0],
    setActiveHandle = _useState4[1];
  var disabled = disabledProp || min > max || step <= 0 || !(0, _utils2.isNumber)(defaultLowValue) || !(0, _utils2.isNumber)(defaultHighValue) || defaultLowValue > defaultHighValue;
  (0, _react.useEffect)(function () {
    if (!initialized) {
      setInitialized(true);
    }
  }, [initialized]);
  var lowHandleRef = (0, _react.useRef)(null);
  var highHandleRef = (0, _react.useRef)(null);
  var _useState5 = (0, _react.useState)((0, _utils2.isNumber)(defaultLowValue) ? defaultLowValue : min || 0),
    _useState6 = _slicedToArray(_useState5, 2),
    lowValue = _useState6[0],
    setLowValue = _useState6[1];
  var minTrackPercent = (0, _utils3.valueToPercent)(lowValue, min, max);
  var _useState7 = (0, _react.useState)((0, _utils2.isNumber)(defaultHighValue) ? defaultHighValue : max || 0),
    _useState8 = _slicedToArray(_useState7, 2),
    highValue = _useState8[0],
    setHighValue = _useState8[1];
  var maxTrackPercent = (0, _utils3.valueToPercent)(highValue, min, max);
  var updateValue = function updateValue(newValue) {
    var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _constants.KnobType.LOW;
    switch (type) {
      case _constants.KnobType.LOW:
        if (newValue > highValue) {
          return;
        }
        setLowValue(newValue);
        if ((0, _utils2.isFunction)(onChange)) {
          onChange({
            low: newValue,
            high: highValue
          });
        }
        break;
      case _constants.KnobType.HIGH:
        if (newValue < lowValue) {
          return;
        }
        setHighValue(newValue);
        if ((0, _utils2.isFunction)(onChange)) {
          onChange({
            low: lowValue,
            high: newValue
          });
        }
        break;
      default:
        break;
    }
  };
  var handleTrackClick = function handleTrackClick(event) {
    var pointerPosition = event.clientX;
    if (!disabled && !readOnly) {
      if (!trackRef.current || !pointerPosition) {
        return null;
      }
      var _trackRef$current$get = trackRef.current.getBoundingClientRect(),
        left = _trackRef$current$get.left,
        width = _trackRef$current$get.width;
      var diff = pointerPosition - left;
      var percent = diff / width;
      var newValue = (0, _utils3.percentToValue)(percent, min, max);
      var difflowValue = Math.abs(newValue - lowValue);
      var diffhighValue = Math.abs(newValue - highValue);
      var newValueRounded = (0, _utils3.roundValueToStep)(newValue, step, difflowValue < diffhighValue ? lowValue : highValue);
      var clampedValue = (0, _utils3.clamp)(newValueRounded, min, max);
      if (clampedValue !== null) {
        if (difflowValue < diffhighValue || clampedValue < lowValue && activeHandle === _constants.KnobType.LOW) {
          var _lowHandleRef$current;
          updateValue(clampedValue, _constants.KnobType.LOW);
          (_lowHandleRef$current = lowHandleRef.current) === null || _lowHandleRef$current === void 0 || _lowHandleRef$current.focus();
        } else if (difflowValue > diffhighValue || clampedValue > highValue && activeHandle === _constants.KnobType.HIGH) {
          var _highHandleRef$curren;
          updateValue(clampedValue, _constants.KnobType.HIGH);
          (_highHandleRef$curren = highHandleRef.current) === null || _highHandleRef$curren === void 0 || _highHandleRef$curren.focus();
        }
      }
    }
    return null;
  };
  return _react.default.createElement(Container, {
    $disabled: disabled,
    $sliderColor: sliderColor,
    $variant: variant,
    onClick: handleTrackClick,
    ref: trackRef,
    tabIndex: -1
  }, _react.default.createElement(_RangeSliderTrack.RangeSliderTrack, {
    variant: variant,
    sliderColor: sliderColor,
    readOnly: readOnly,
    disabled: disabled
  }, _react.default.createElement(_SliderTrackHighlight.SliderTrackHighlight, {
    disabled: disabled,
    sliderColor: sliderColor,
    startValue: minTrackPercent,
    value: maxTrackPercent,
    variant: variant
  }), !readOnly && _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_SliderKnobHandler.default, {
    disabled: disabled,
    handleRef: lowHandleRef,
    knobType: _constants.KnobType.LOW,
    max: max,
    min: min,
    onChange: function onChange(value) {
      if (value <= highValue) {
        updateValue(value, _constants.KnobType.LOW);
      }
    },
    readOnly: readOnly,
    setActiveHandle: setActiveHandle,
    sliderColor: sliderColor,
    step: step,
    trackRef: trackRef,
    value: lowValue,
    variant: variant,
    zIndex: activeHandle === _constants.KnobType.LOW ? 2 : 1
  }), _react.default.createElement(_SliderKnobHandler.default, {
    disabled: disabled,
    handleRef: highHandleRef,
    knobType: _constants.KnobType.HIGH,
    max: max,
    min: min,
    onChange: function onChange(value) {
      if (value >= lowValue) {
        updateValue(value, _constants.KnobType.HIGH);
      }
    },
    readOnly: readOnly,
    setActiveHandle: setActiveHandle,
    sliderColor: sliderColor,
    step: step,
    trackRef: trackRef,
    value: highValue,
    variant: variant,
    zIndex: activeHandle === _constants.KnobType.HIGH ? 2 : 1
  }))));
};
var _default = exports.default = RangeSlider;