"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var slider_with_input_exports = {};
__export(slider_with_input_exports, {
  SliderWithInput: () => SliderWithInput
});
module.exports = __toCommonJS(slider_with_input_exports);
var import_design_system = require("@wix/design-system");
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
const SliderWithInput = ({
  min,
  max,
  onChange,
  label,
  sliderDataHook,
  inputDataHook,
  value,
  tooltipTextKey,
  tooltipAppendTo,
  disabled,
  normalizeMaxValue = true,
  logBi,
  suffix
}) => {
  const zIndexService = (0, import_react.useContext)(import_ricos_context.ZIndexContext);
  const inputRef = (0, import_react.useRef)(null);
  const [inputKey, setInputKey] = (0, import_react.useState)(0);
  const [inputValue, setInputValue] = (0, import_react.useState)(value);
  import_react.default.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__ */ import_react.default.createElement(
    import_design_system.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__ */ import_react.default.createElement(
      import_design_system.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__ */ import_react.default.createElement(
      import_design_system.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
      }
    )
  );
};
//# sourceMappingURL=slider-with-input.js.map
