"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _flex = _interopRequireDefault(require("../../templates/flex"));
var _typography = require("../../typography");
var _styled = require("./styled");
var _excluded = ["initMax", "initMin", "max", "min", "onChange", "onInput", "step", "TextComponent"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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 && {}.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 _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; }
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 ? _typography.TextSmall : _ref$TextComponent,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useState = (0, _react.useState)(initMax || max),
    maxValue = _useState[0],
    setMaxValue = _useState[1];
  var _useState2 = (0, _react.useState)(initMin || min),
    minValue = _useState2[0],
    setMinValue = _useState2[1];
  var _useState3 = (0, _react.useState)(0),
    width = _useState3[0],
    setWidth = _useState3[1];
  var maxValueRef = (0, _react.useRef)(null);
  var minValueRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(function () {
    if (maxValueRef.current) {
      setWidth(maxValueRef.current.getBoundingClientRect().width);
    }
  }, [minValue]);
  (0, _react.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["default"].createElement(_flex["default"], {
    column: true,
    gap: 1,
    flex: true
  }, /*#__PURE__*/_react["default"].createElement(_flex["default"], {
    alignItems: "center",
    "data-testid": "multiRangeInput",
    justifyContent: "center",
    position: "relative",
    width: "100%"
  }, /*#__PURE__*/_react["default"].createElement(_styled.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["default"].createElement(_styled.Range, _extends({
    "data-testid": "maxRangeInput",
    max: max,
    min: min,
    onChange: handleMaxChange,
    onInput: handleMaxInput,
    ref: maxValueRef,
    step: step,
    value: maxValue,
    zIndex: 5
  }, rest)), /*#__PURE__*/_react["default"].createElement(_styled.Slider, {
    "data-testid": "multiRange-slider"
  }, /*#__PURE__*/_react["default"].createElement(_styled.SliderTrack, {
    "data-testid": "multiRange-sliderTrack",
    max: max,
    maxValue: maxValue,
    min: min,
    minValue: minValue,
    width: width
  }))), /*#__PURE__*/_react["default"].createElement(_flex["default"], {
    "data-testid": "multiRange-values",
    justifyContent: "between"
  }, /*#__PURE__*/_react["default"].createElement(TextComponent, {
    "data-testid": "multiRange-minValue"
  }, minValue), /*#__PURE__*/_react["default"].createElement(TextComponent, {
    "data-testid": "multiRange-maxValue"
  }, maxValue)));
};
var _default = exports["default"] = MultiRangeInput;