var _excluded = ["initMax", "initMin", "max", "min", "onChange", "onInput", "step", "TextComponent"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React, { useEffect, useRef, useState } from "react";
import Flex from "../../templates/flex";
import { TextSmall } from "../../typography";
import { Range, Slider, SliderTrack } from "./styled";
var MultiRangeInput = function MultiRangeInput(_ref) {
  var initMax = _ref.initMax,
    initMin = _ref.initMin,
    _ref$max = _ref.max,
    max = _ref$max === void 0 ? 100 : _ref$max,
    _ref$min = _ref.min,
    min = _ref$min === void 0 ? 0 : _ref$min,
    onChange = _ref.onChange,
    onInput = _ref.onInput,
    _ref$step = _ref.step,
    step = _ref$step === void 0 ? 1 : _ref$step,
    _ref$TextComponent = _ref.TextComponent,
    TextComponent = _ref$TextComponent === void 0 ? TextSmall : _ref$TextComponent,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useState = useState(initMax || max),
    maxValue = _useState[0],
    setMaxValue = _useState[1];
  var _useState2 = useState(initMin || min),
    minValue = _useState2[0],
    setMinValue = _useState2[1];
  var _useState3 = useState(0),
    width = _useState3[0],
    setWidth = _useState3[1];
  var maxValueRef = useRef(null);
  var minValueRef = useRef(null);
  useEffect(function () {
    if (maxValueRef.current) {
      setWidth(maxValueRef.current.getBoundingClientRect().width);
    }
  }, [minValue]);
  useEffect(function () {
    setMaxValue(initMax || max);
    setMinValue(initMin || min);
  }, [max, min]);
  var handleMaxChange = function handleMaxChange(e) {
    if (onChange) onChange({
      max: e.target.value,
      min: minValue
    });
  };
  var handleMinChange = function handleMinChange(e) {
    if (onChange) onChange({
      max: maxValue,
      min: e.target.value
    });
  };
  var handleMaxInput = function handleMaxInput(e) {
    var value = Math.max(+e.target.value, minValue + step);
    setMaxValue(value);
    e.target.value = value.toString();
    if (onInput) onInput({
      max: e.target.value,
      min: minValue
    });
  };
  var handleMinInput = function handleMinInput(e) {
    var value = Math.min(+e.target.value, maxValue - step);
    setMinValue(value);
    e.target.value = value.toString();
    if (onInput) onInput({
      max: maxValue,
      min: e.target.value
    });
  };
  return /*#__PURE__*/React.createElement(Flex, {
    column: true,
    gap: 1,
    flex: true
  }, /*#__PURE__*/React.createElement(Flex, {
    alignItems: "center",
    "data-testid": "multiRangeInput",
    justifyContent: "center",
    position: "relative",
    width: "100%"
  }, /*#__PURE__*/React.createElement(Range, _extends({
    "data-testid": "minRangeInput",
    max: max,
    min: min,
    onChange: handleMinChange,
    onInput: handleMinInput,
    position: "relative",
    ref: minValueRef,
    step: step,
    value: minValue,
    zIndex: 3
  }, rest)), /*#__PURE__*/React.createElement(Range, _extends({
    "data-testid": "maxRangeInput",
    max: max,
    min: min,
    onChange: handleMaxChange,
    onInput: handleMaxInput,
    ref: maxValueRef,
    step: step,
    value: maxValue,
    zIndex: 5
  }, rest)), /*#__PURE__*/React.createElement(Slider, {
    "data-testid": "multiRange-slider"
  }, /*#__PURE__*/React.createElement(SliderTrack, {
    "data-testid": "multiRange-sliderTrack",
    max: max,
    maxValue: maxValue,
    min: min,
    minValue: minValue,
    width: width
  }))), /*#__PURE__*/React.createElement(Flex, {
    "data-testid": "multiRange-values",
    justifyContent: "between"
  }, /*#__PURE__*/React.createElement(TextComponent, {
    "data-testid": "multiRange-minValue"
  }, minValue), /*#__PURE__*/React.createElement(TextComponent, {
    "data-testid": "multiRange-maxValue"
  }, maxValue)));
};
export default MultiRangeInput;