"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SliderUI", {
    enumerable: true,
    get: function() {
        return SliderUI;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _NumberFormat = require("../../../NumberFormat");
var _utils = require("../../../NumberInput/utils");
var _utils1 = require("../../../../utils");
var _utils2 = require("../../utils");
var _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;
}
var SliderUI = /*#__PURE__*/ (0, _react.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 = (0, _utils1.safeUseId)();
    var commonFormatterArgs = (0, _react.useMemo)(function() {
        return (0, _utils2.createFormatterArgs)({
            decimalScale: decimalScale,
            thousandsGroupStyle: thousandsGroupStyle,
            thousandSeparator: thousandSeparator,
            decimalSeparator: decimalSeparator,
            fixedDecimalScale: fixedDecimalScale
        });
    }, [
        decimalScale,
        thousandsGroupStyle,
        thousandSeparator,
        decimalSeparator,
        fixedDecimalScale
    ]);
    var normalizedFormatterArgs = (0, _react.useMemo)(function() {
        return (0, _utils2.createNormalizedFormatterArgs)({
            decimalScale: decimalScale,
            thousandsGroupStyle: thousandsGroupStyle,
            thousandSeparator: thousandSeparator,
            decimalSeparator: decimalSeparator,
            fixedDecimalScale: fixedDecimalScale
        });
    }, [
        decimalScale,
        thousandsGroupStyle,
        thousandSeparator,
        decimalSeparator,
        fixedDecimalScale
    ]);
    var sliderValue = (0, _react.useMemo)(function() {
        return Math.max(min, Math.min(max, Number((0, _NumberFormat.numberFormatter)(String(value) || '', normalizedFormatterArgs))));
    }, [
        value,
        min,
        max,
        normalizedFormatterArgs
    ]);
    var roundedSliderValue = (0, _react.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(_utils.excludingNumberSchema, '');
        var cleanValue = Number(withoutExtraSymbols);
        setInnerValue(cleanValue);
        var formatted = (0, _NumberFormat.numberFormatter)(String(cleanValue) || '', commonFormatterArgs);
        if (onChange) {
            onChange(event || undefined, {
                raw: cleanValue,
                formatted: formatted
            });
        }
        if (onChangeSlider) {
            onChangeSlider({
                raw: cleanValue,
                formatted: 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
    }));
});
