import React, { forwardRef, useMemo } from "react";
import { numberFormatter } from "../../../NumberFormat";
import { excludingNumberSchema } from "../../../NumberInput/utils";
import { safeUseId } from "../../../../utils";
import { createFormatterArgs, createNormalizedFormatterArgs } from "../../utils";
import { SliderWrapper, StyledProgress, StyledSlider, StyledTrack } from "./Slider.styles";
export const SliderUI = /*#__PURE__*/ forwardRef(({ value, min, max, step, decimalScale, thousandsGroupStyle, thousandSeparator = ' ', decimalSeparator, fixedDecimalScale, disabled, readOnly, hasPointer, // callbacks
onChange, onChangeSlider, setInnerValue }, ref)=>{
    const sliderId = safeUseId();
    const commonFormatterArgs = useMemo(()=>createFormatterArgs({
            decimalScale,
            thousandsGroupStyle,
            thousandSeparator,
            decimalSeparator,
            fixedDecimalScale
        }), [
        decimalScale,
        thousandsGroupStyle,
        thousandSeparator,
        decimalSeparator,
        fixedDecimalScale
    ]);
    const normalizedFormatterArgs = useMemo(()=>createNormalizedFormatterArgs({
            decimalScale,
            thousandsGroupStyle,
            thousandSeparator,
            decimalSeparator,
            fixedDecimalScale
        }), [
        decimalScale,
        thousandsGroupStyle,
        thousandSeparator,
        decimalSeparator,
        fixedDecimalScale
    ]);
    const sliderValue = useMemo(()=>Math.max(min, Math.min(max, Number(numberFormatter(String(value) || '', normalizedFormatterArgs)))), [
        value,
        min,
        max,
        normalizedFormatterArgs
    ]);
    const roundedSliderValue = useMemo(()=>step ? min + Math.round((sliderValue - min) / step) * step : sliderValue, [
        sliderValue,
        min,
        step
    ]);
    const handleChange = (event)=>{
        if (disabled || readOnly) {
            return;
        }
        const { value: rawValue } = event.target;
        const withoutExtraSymbols = rawValue.replace(excludingNumberSchema, '');
        const cleanValue = Number(withoutExtraSymbols);
        setInnerValue(cleanValue);
        const formatted = numberFormatter(String(cleanValue) || '', commonFormatterArgs);
        if (onChange) {
            onChange(event || undefined, {
                raw: cleanValue,
                formatted
            });
        }
        if (onChangeSlider) {
            onChangeSlider({
                raw: cleanValue,
                formatted
            });
        }
    };
    return /*#__PURE__*/ React.createElement(SliderWrapper, null, /*#__PURE__*/ React.createElement(StyledTrack, null), /*#__PURE__*/ React.createElement(StyledProgress, {
        value: roundedSliderValue,
        max: max,
        min: min
    }), /*#__PURE__*/ React.createElement(StyledSlider, {
        id: sliderId,
        ref: ref,
        type: "range",
        value: sliderValue,
        min: min,
        max: max,
        step: step,
        "aria-valuemin": min,
        "aria-valuemax": max,
        hasPointer: hasPointer,
        "aria-orientation": "horizontal",
        disabled: disabled,
        readOnly: readOnly,
        onChange: handleChange
    }));
});
