"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _message = require("../message");
var _rangeSlider = require("../range-slider");
var _input = require("../input");
var _tooltip = require("../tooltip");
var _fontAwesomeIcon = require("../font-awesome-icon");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const navigationKeys = new Set(['ArrowDown', 'ArrowUp', 'ArrowLeft', 'ArrowRight', 'Enter', 'Escape', 'End', 'Home', 'Delete', 'Backspace', 'Tab']);
/**
 * @uxr-only-supported
 * @midnight-gray-supported
 * @alpine-light-supported
 * */
function InputSlider({
  label,
  labelIcon = null,
  labelIconTooltipText = '',
  labelIconTestId = '',
  helperText,
  validity,
  testId,
  inputProps = {},
  sanitize,
  value,
  onChange,
  step,
  ...props
}) {
  const multiplier = !!step && step < 1 ? 1 / step : 1;
  const valueOrMin = value ?? props.min;
  const valueOrEmpty = value ?? '';
  const rangeSliderComputedProps = (0, _react.useMemo)(() => !!step ? {
    min: props.min * multiplier,
    max: props.max * multiplier,
    onValuesUpdated: ({
      values
    }) => {
      onChange(values[0] / multiplier);
    },
    values: [valueOrMin * multiplier]
  } : {
    onValuesUpdated: ({
      values
    }) => onChange(values[0]),
    values: [valueOrMin * multiplier]
  }, [props.max, props.min, step, multiplier, valueOrMin, onChange]);
  const negativeAllowed = props.min < 0;
  const currentValue = value ?? props.min;
  const prevValue = currentValue.toString();
  const sliderTestId = testId ? `${testId}-range-slider` : 'range-slider';
  const onKeyDownHandler = (0, _react.useCallback)(event => {
    if (event.metaKey || navigationKeys.has(event.key)) {
      return;
    }
    if (!!sanitize && !sanitize.onKeyDown({
      value: event.key,
      prevValue: event.target.value,
      negativeAllowed
    })) {
      event.preventDefault();
    }
  }, [sanitize, negativeAllowed]);
  const onChangeHandler = (0, _react.useCallback)(event => {
    const {
      target
    } = event;
    if (sanitize) {
      const sanitizedValue = sanitize.onChange({
        value: target.value,
        prevValue,
        negativeAllowed
      });
      const nextValue = sanitizedValue ? Number(sanitizedValue) : currentValue;
      onChange(nextValue);
    } else {
      const nextValueAsNumber = Number(target.valueAsNumber);
      if (Number.isNaN(nextValueAsNumber)) {
        onChange(undefined);
      } else {
        onChange(nextValueAsNumber);
      }
    }
  }, [sanitize?.onChange, currentValue]);
  const onPasteHandler = (0, _react.useCallback)(event => {
    if (!event.clipboardData.types.includes('text/plain')) {
      return;
    }
    const nextValue = event.clipboardData.getData('text/plain');
    if (!nextValue) {
      return;
    }
    if (sanitize) {
      const sanitizedText = sanitize.onChange({
        value: nextValue,
        prevValue,
        negativeAllowed
      });
      const sanitizedValue = sanitizedText ? Number(sanitizedText) : currentValue;
      event.preventDefault();
      onChange(sanitizedValue);
    } else {
      const nextValueAsNumber = Number(nextValue);
      onChange(Number.isNaN(nextValueAsNumber) ? undefined : nextValueAsNumber);
    }
  }, [sanitize, negativeAllowed, prevValue, multiplier, currentValue]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    "test-id": testId,
    className: (0, _classnames.default)('input-slider', props.isDisabled && 'disabled'),
    children: [label && /*#__PURE__*/(0, _jsxRuntime.jsxs)("label", {
      id: `${inputProps.id}-label`,
      htmlFor: inputProps.id,
      className: "input-slider-label",
      children: [label, labelIcon && /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
        content: labelIconTooltipText,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
            icon: labelIcon,
            "test-id": labelIconTestId,
            className: "margin-left-1"
          })
        })
      })]
    }), helperText && /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
      className: "input-slider-helper-text",
      children: helperText
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "input-slider-container",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_rangeSlider.RangeSlider, {
        ...props,
        ...rangeSliderComputedProps,
        testId: sliderTestId,
        ariaLabelledBy: `${inputProps.id}-label`
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_input.Input, {
        type: "number",
        fieldTestId: "range-slider-input",
        isDisabled: props.isDisabled,
        validity: validity,
        onChange: onChangeHandler,
        onPaste: !!sanitize ? onPasteHandler : undefined,
        onKeyDown: !!sanitize ? onKeyDownHandler : undefined,
        value: valueOrEmpty,
        min: props.min,
        max: props.max,
        step: step,
        ...inputProps,
        className: (0, _classnames.default)('range-slider-input', inputProps.className)
      })]
    }), validity?.value?.map(text => /*#__PURE__*/(0, _jsxRuntime.jsx)(_message.Message, {
      type: validity.type,
      size: "sm",
      children: text
    }, text))]
  });
}
var _default = exports.default = InputSlider;