import { FieldSet, NumberInput, Slider } from "@wix/design-system";
import React, { useContext, useRef, useState } from "react";
import { ZIndexContext } from "./../../ricos-context/index.js";
const SliderWithInput = ({
  min,
  max,
  onChange,
  label,
  sliderDataHook,
  inputDataHook,
  value,
  tooltipTextKey,
  tooltipAppendTo,
  disabled,
  normalizeMaxValue = true,
  logBi,
  suffix
}) => {
  const zIndexService = useContext(ZIndexContext);
  const inputRef = useRef(null);
  const [inputKey, setInputKey] = useState(0);
  const [inputValue, setInputValue] = useState(value);
  React.useEffect(() => {
    if (value !== inputValue) {
      setInputValue(value);
    }
  }, [value]);
  const handleInputWithinBounds = (data) => {
    const numberValue = Math.round(Number(data));
    const valueWithinBounds = isNaN(numberValue) || numberValue < min ? min : normalizeMaxValue && numberValue > max ? max : numberValue;
    setInputValue(valueWithinBounds);
    if (value !== valueWithinBounds) {
      onChange(valueWithinBounds);
      return;
    }
    setInputKey((prev) => prev + 1);
  };
  return /* @__PURE__ */ React.createElement(
    FieldSet,
    {
      legend: label,
      legendSize: "small",
      legendPlacement: "top",
      columns: suffix ? "auto 72px" : "auto 60px",
      infoContent: tooltipTextKey,
      infoTooltipProps: {
        appendTo: tooltipAppendTo ?? ((el) => Boolean(el.getAttribute("data-hook")?.includes("ricos-portal"))),
        zIndex: zIndexService.getZIndex("TOOLTIP")
      }
    },
    /* @__PURE__ */ React.createElement(
      Slider,
      {
        disabled,
        onChange: (value2) => {
          const sliderValue = typeof value2 === "number" ? value2 : value2[0];
          logBi?.({ dataHook: sliderDataHook, value: sliderValue });
          onChange(sliderValue);
          setInputValue(sliderValue);
        },
        min,
        max,
        value,
        displayMarks: false,
        dataHook: sliderDataHook,
        displayTooltip: false
      }
    ),
    /* @__PURE__ */ React.createElement(
      NumberInput,
      {
        disabled,
        key: inputKey,
        inputRef,
        value: inputValue,
        min,
        max,
        onChange: (value2, stringValue) => {
          if (stringValue === "") {
            setInputValue("");
            return;
          }
          if (typeof value2 === "number") {
            logBi?.({ dataHook: inputDataHook, value: value2 });
            onChange(value2);
            setInputValue(value2);
          }
        },
        size: "small",
        hideStepper: true,
        dataHook: inputDataHook,
        onBlur: (e) => handleInputWithinBounds(e.target.value),
        onKeyDown: (e) => {
          if (["Enter", "Escape"].includes(e.key)) {
            handleInputWithinBounds(inputRef.current?.value);
          }
        },
        suffix
      }
    )
  );
};
export {
  SliderWithInput
};
//# sourceMappingURL=slider-with-input.js.map
