var _excluded = ["domain", "step", "hasLabels", "hasMarkers", "tracksLeft", "tracksRight", "tickCount", "IconLeft", "IconRight", "isReversed", "isDisabled", "values", "onChange", "onUpdate", "thresholds", "displayUnit", "formatValue", "mode", "onSlideStart", "onSlideEnd"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) { ; } } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useCallback, useState, createRef } from 'react';
import intersection from 'lodash/intersection';
import { Rail, Handles, Tracks, Ticks } from 'react-compound-slider';
import { Handle, Track, ThresholdTrack, ThresholdRail, Tick } from '.';
import { StyledSlider } from './partials/StyledSlider';
import { StyledIconLeft } from './partials/StyledIconLeft';
import { StyledIconRight } from './partials/StyledIconRight';
import { StyledRangeSlider } from './partials/StyledRangeSlider';
import { StyledRail } from './partials/StyledRail';
import { StyledRailInner } from './partials/StyledRailInner';
import { StyledTicks } from './partials/StyledTicks';
export var RangeSlider = function RangeSlider(_ref) {
  var domain = _ref.domain,
    step = _ref.step,
    hasLabels = _ref.hasLabels,
    hasMarkers = _ref.hasMarkers,
    _ref$tracksLeft = _ref.tracksLeft,
    tracksLeft = _ref$tracksLeft === void 0 ? false : _ref$tracksLeft,
    _ref$tracksRight = _ref.tracksRight,
    tracksRight = _ref$tracksRight === void 0 ? false : _ref$tracksRight,
    _ref$tickCount = _ref.tickCount,
    tickCount = _ref$tickCount === void 0 ? 10 : _ref$tickCount,
    IconLeft = _ref.IconLeft,
    IconRight = _ref.IconRight,
    isReversed = _ref.isReversed,
    isDisabled = _ref.isDisabled,
    values = _ref.values,
    onChange = _ref.onChange,
    onUpdate = _ref.onUpdate,
    thresholds = _ref.thresholds,
    _ref$displayUnit = _ref.displayUnit,
    displayUnit = _ref$displayUnit === void 0 ? '' : _ref$displayUnit,
    formatValue = _ref.formatValue,
    mode = _ref.mode,
    onSlideStart = _ref.onSlideStart,
    onSlideEnd = _ref.onSlideEnd,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useState = useState(values),
    _useState2 = _slicedToArray(_useState, 2),
    sliderValues = _useState2[0],
    setSliderValues = _useState2[1];
  var handleRefs = values.map(function () {
    return /*#__PURE__*/createRef();
  });
  var focusHandle = useCallback(function (newValues) {
    var staticValues = intersection(sliderValues, newValues);

    // Single handle
    if (sliderValues.length === 1) {
      var _handleRefs$0$current;
      (_handleRefs$0$current = handleRefs[0].current) === null || _handleRefs$0$current === void 0 ? void 0 : _handleRefs$0$current.focus();
    }

    // Multiple handles
    if (sliderValues.length === 2 && staticValues.length === 1) {
      var _handleRefs$refId$cur;
      var refId = sliderValues.indexOf(staticValues[0]) === 1 ? 0 : 1;
      (_handleRefs$refId$cur = handleRefs[refId].current) === null || _handleRefs$refId$cur === void 0 ? void 0 : _handleRefs$refId$cur.focus();
    }
  }, [sliderValues, handleRefs]);
  var onUpdateHandler = useCallback(function (newValues) {
    focusHandle(newValues);
    setSliderValues(newValues);
    if (onUpdate) {
      onUpdate(newValues);
    }
  }, [onUpdate, setSliderValues, focusHandle]);
  var formatValueDefault = useCallback(function (_ref2) {
    var value = _ref2.value;
    return "".concat(Math.floor(value)).concat(displayUnit);
  }, [displayUnit]);
  return /*#__PURE__*/React.createElement(StyledRangeSlider, _extends({
    $isReversed: isReversed,
    $isDisabled: isDisabled
  }, rest), IconLeft && /*#__PURE__*/React.createElement(StyledIconLeft, {
    "aria-hidden": true
  }, /*#__PURE__*/React.createElement(IconLeft, {
    size: 22
  })), /*#__PURE__*/React.createElement(StyledSlider, {
    domain: domain,
    reversed: isReversed,
    disabled: isDisabled,
    values: values,
    step: step,
    mode: mode,
    onChange: onChange,
    onUpdate: onUpdateHandler,
    onSlideStart: onSlideStart,
    onSlideEnd: onSlideEnd
  }, /*#__PURE__*/React.createElement(Rail, null, function (_ref3) {
    var getRailProps = _ref3.getRailProps;
    return /*#__PURE__*/React.createElement(StyledRail, getRailProps(), /*#__PURE__*/React.createElement(StyledRailInner, null, thresholds && /*#__PURE__*/React.createElement(ThresholdRail, {
      thresholds: thresholds
    })));
  }), /*#__PURE__*/React.createElement(Handles, null, function (_ref4) {
    var handles = _ref4.handles,
      getHandleProps = _ref4.getHandleProps;
    return /*#__PURE__*/React.createElement("div", null, handles.map(function (handle, index) {
      return /*#__PURE__*/React.createElement(Handle, {
        ref: handleRefs[index],
        key: handle.id,
        handle: handle,
        domain: domain,
        getHandleProps: getHandleProps,
        formatValue: formatValue !== null && formatValue !== void 0 ? formatValue : formatValueDefault
      });
    }));
  }), /*#__PURE__*/React.createElement(Tracks, {
    left: tracksLeft,
    right: tracksRight
  }, function (_ref5) {
    var tracks = _ref5.tracks,
      getTrackProps = _ref5.getTrackProps;
    return /*#__PURE__*/React.createElement("div", null, tracks.map(function (_ref6) {
      var id = _ref6.id,
        source = _ref6.source,
        target = _ref6.target;
      if (thresholds) {
        return /*#__PURE__*/React.createElement(ThresholdTrack, {
          id: id,
          key: id,
          source: source,
          target: target,
          getTrackProps: getTrackProps,
          thresholds: thresholds
        });
      }
      return /*#__PURE__*/React.createElement(Track, {
        id: id,
        key: id,
        source: source,
        target: target,
        getTrackProps: getTrackProps
      });
    }));
  }), tickCount && /*#__PURE__*/React.createElement(Ticks, {
    count: tickCount
  }, function (_ref7) {
    var ticks = _ref7.ticks;
    return /*#__PURE__*/React.createElement(StyledTicks, null, ticks.map(function (tick) {
      return /*#__PURE__*/React.createElement(Tick, {
        key: tick.id,
        tick: tick,
        count: ticks.length,
        hasMarkers: hasMarkers,
        hasLabels: hasLabels,
        values: sliderValues,
        domain: domain,
        isReversed: isReversed,
        thresholds: thresholds,
        tracksLeft: tracksLeft,
        tracksRight: tracksRight
      });
    }));
  })), IconRight && /*#__PURE__*/React.createElement(StyledIconRight, {
    "aria-hidden": true
  }, /*#__PURE__*/React.createElement(IconRight, {
    size: 22
  })));
};
RangeSlider.displayName = 'RangeSlider';