"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SliderUI", {
    enumerable: true,
    get: function() {
        return SliderUI;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _NumberFormat = require("../../../NumberFormat");
const _utils = require("../../../NumberInput/utils");
const _utils1 = require("../../../../utils");
const _utils2 = require("../../utils");
const _Sliderstyles = require("./Slider.styles");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const SliderUI = /*#__PURE__*/ (0, _react.forwardRef)(({ value, min, max, step, decimalScale, thousandsGroupStyle, thousandSeparator = ' ', decimalSeparator, fixedDecimalScale, disabled, readOnly, hasPointer, // callbacks
onChange, onChangeSlider, setInnerValue }, ref)=>{
    const sliderId = (0, _utils1.safeUseId)();
    const commonFormatterArgs = (0, _react.useMemo)(()=>(0, _utils2.createFormatterArgs)({
            decimalScale,
            thousandsGroupStyle,
            thousandSeparator,
            decimalSeparator,
            fixedDecimalScale
        }), [
        decimalScale,
        thousandsGroupStyle,
        thousandSeparator,
        decimalSeparator,
        fixedDecimalScale
    ]);
    const normalizedFormatterArgs = (0, _react.useMemo)(()=>(0, _utils2.createNormalizedFormatterArgs)({
            decimalScale,
            thousandsGroupStyle,
            thousandSeparator,
            decimalSeparator,
            fixedDecimalScale
        }), [
        decimalScale,
        thousandsGroupStyle,
        thousandSeparator,
        decimalSeparator,
        fixedDecimalScale
    ]);
    const sliderValue = (0, _react.useMemo)(()=>Math.max(min, Math.min(max, Number((0, _NumberFormat.numberFormatter)(String(value) || '', normalizedFormatterArgs)))), [
        value,
        min,
        max,
        normalizedFormatterArgs
    ]);
    const roundedSliderValue = (0, _react.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(_utils.excludingNumberSchema, '');
        const cleanValue = Number(withoutExtraSymbols);
        setInnerValue(cleanValue);
        const formatted = (0, _NumberFormat.numberFormatter)(String(cleanValue) || '', commonFormatterArgs);
        if (onChange) {
            onChange(event || undefined, {
                raw: cleanValue,
                formatted
            });
        }
        if (onChangeSlider) {
            onChangeSlider({
                raw: cleanValue,
                formatted
            });
        }
    };
    return /*#__PURE__*/ _react.default.createElement(_Sliderstyles.SliderWrapper, null, /*#__PURE__*/ _react.default.createElement(_Sliderstyles.StyledTrack, null), /*#__PURE__*/ _react.default.createElement(_Sliderstyles.StyledProgress, {
        value: roundedSliderValue,
        max: max,
        min: min
    }), /*#__PURE__*/ _react.default.createElement(_Sliderstyles.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
    }));
});
