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 var SliderUI = /*#__PURE__*/ forwardRef(function(param, ref) {
    var value = param.value, min = param.min, max = param.max, step = param.step, decimalScale = param.decimalScale, thousandsGroupStyle = param.thousandsGroupStyle, _param_thousandSeparator = param.thousandSeparator, thousandSeparator = _param_thousandSeparator === void 0 ? ' ' : _param_thousandSeparator, decimalSeparator = param.decimalSeparator, fixedDecimalScale = param.fixedDecimalScale, disabled = param.disabled, readOnly = param.readOnly, hasPointer = param.hasPointer, // callbacks
    onChange = param.onChange, onChangeSlider = param.onChangeSlider, setInnerValue = param.setInnerValue;
    var sliderId = safeUseId();
    var commonFormatterArgs = useMemo(function() {
        return createFormatterArgs({
            decimalScale: decimalScale,
            thousandsGroupStyle: thousandsGroupStyle,
            thousandSeparator: thousandSeparator,
            decimalSeparator: decimalSeparator,
            fixedDecimalScale: fixedDecimalScale
        });
    }, [
        decimalScale,
        thousandsGroupStyle,
        thousandSeparator,
        decimalSeparator,
        fixedDecimalScale
    ]);
    var normalizedFormatterArgs = useMemo(function() {
        return createNormalizedFormatterArgs({
            decimalScale: decimalScale,
            thousandsGroupStyle: thousandsGroupStyle,
            thousandSeparator: thousandSeparator,
            decimalSeparator: decimalSeparator,
            fixedDecimalScale: fixedDecimalScale
        });
    }, [
        decimalScale,
        thousandsGroupStyle,
        thousandSeparator,
        decimalSeparator,
        fixedDecimalScale
    ]);
    var sliderValue = useMemo(function() {
        return Math.max(min, Math.min(max, Number(numberFormatter(String(value) || '', normalizedFormatterArgs))));
    }, [
        value,
        min,
        max,
        normalizedFormatterArgs
    ]);
    var roundedSliderValue = useMemo(function() {
        return step ? min + Math.round((sliderValue - min) / step) * step : sliderValue;
    }, [
        sliderValue,
        min,
        step
    ]);
    var handleChange = function handleChange(event) {
        if (disabled || readOnly) {
            return;
        }
        var _event_target = event.target, rawValue = _event_target.value;
        var withoutExtraSymbols = rawValue.replace(excludingNumberSchema, '');
        var cleanValue = Number(withoutExtraSymbols);
        setInnerValue(cleanValue);
        var formatted = numberFormatter(String(cleanValue) || '', commonFormatterArgs);
        if (onChange) {
            onChange(event || undefined, {
                raw: cleanValue,
                formatted: formatted
            });
        }
        if (onChangeSlider) {
            onChangeSlider({
                raw: cleanValue,
                formatted: 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
    }));
});
