"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SingleSlider", {
    enumerable: true,
    get: function() {
        return SingleSlider;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
const _Slidertokens = require("../../Slider.tokens");
const _useRangeHandlers = require("../../hooks/useRangeHandlers");
const _getTickStyle = require("../../utils/getTickStyle");
const _getSingleSliderLayout = require("../../utils/getSingleSliderLayout");
const _Singlestyles = require("./Single.styles");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
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 clamp = (value, min, max)=>Math.max(min, Math.min(value, max));
const SingleSlider = ({ // value
min, max, value: outerValue, defaultValue, name, step = 1, // label
label, labelContentLeft, labelContent, labelPlacement = 'top', labelReversed, // scale
showScale, showRangeValues, scaleAlign = 'bottom', hideMinValueDiff, hideMaxValueDiff, // pointer
pointerSize = 'small', pointerVisibility = 'always', showCurrentValue, currentValueVisibility = 'always', // layout
orientation = 'horizontal', sliderAlign = 'left', reversed, valuePlacement, multipleStepSize = 10, // deprecated
rangeValuesPlacement: rangeValuesPlacementOld, // state
disabled, // events
onChange, onChangeCommitted, // a11y
ariaLabel, // scale
scaleTicks, // остальные HTMLAttributes идут на SliderContainer
...rest })=>{
    const isVertical = orientation === 'vertical';
    const [isHovered, setIsHovered] = (0, _react.useState)(false);
    const clampedOuterValue = outerValue !== undefined ? clamp(outerValue, min, max) : undefined;
    const clampedDefaultValue = defaultValue !== undefined ? clamp(defaultValue, min, max) : undefined;
    const [dragValue, setDragValue] = (0, _react.useState)(clampedDefaultValue ?? min);
    const value = clampedOuterValue ?? dragValue;
    // Округляем значение, если оно не кратно новому шагу
    (0, _react.useEffect)(()=>{
        const remainder = (value - min) % step;
        if (Math.abs(remainder) > 0.000001) {
            const roundedValue = Math.round((value - min) / step) * step + min;
            const validValue = Math.max(min, Math.min(max, roundedValue));
            setDragValue(validValue);
            if (outerValue !== undefined) {
                emitChange(validValue);
            }
        }
    }, [
        step,
        min,
        max,
        value,
        outerValue
    ]);
    const { innerShowScale, innerShowCurrentValue, hasLabelContent, labelPlacementClass, rangeValuesPlacement, scaleAlignClass, labelVerticalPlacement, showPointer, currentValueStyle, progressSizeStyle, rangeMinValueOpacity, rangeMaxValueOpacity } = (0, _getSingleSliderLayout.getSingleSliderLayout)({
        value,
        min,
        max,
        isVertical,
        isHovered,
        reversed,
        scaleTicks,
        showRangeValues,
        showScale,
        showCurrentValue,
        currentValueVisibility,
        label,
        labelContentLeft,
        labelContent,
        labelPlacement,
        sliderAlign,
        rangeValuesPlacementOld,
        scaleAlign,
        pointerSize,
        pointerVisibility,
        valuePlacement
    });
    const emitChange = (newValue)=>{
        if (!onChange) return;
        if (outerValue !== undefined || !name) {
            onChange(newValue);
            return;
        }
        onChange({
            target: {
                value: newValue,
                name
            }
        });
    };
    const handleTickClick = (tick)=>{
        if (disabled) {
            return;
        }
        const clamped = clamp(tick, min, max);
        if (outerValue === undefined) {
            setDragValue(clamped);
        }
        emitChange(clamped);
        if (onChangeCommitted) {
            onChangeCommitted(clamped);
        }
    };
    const { handleChange, handleChangeCommitted, handleKeyDown } = (0, _useRangeHandlers.useRangeHandlers)({
        min,
        max,
        value,
        step,
        multipleStepSize,
        isControlled: outerValue !== undefined,
        onSetValue: setDragValue,
        onEmit: emitChange,
        onCommit: onChangeCommitted
    });
    return /*#__PURE__*/ _react.default.createElement(_Singlestyles.SingleWrapper, {
        className: (0, _classnames.default)(labelPlacementClass, scaleAlignClass, {
            [_Slidertokens.classes.verticalOrientation]: isVertical,
            [_Slidertokens.classes.labelContentReversed]: labelReversed,
            [_Slidertokens.classes.labelPlacementBottom]: labelVerticalPlacement === 'bottom',
            [_Slidertokens.classes.labelAlignRight]: sliderAlign === 'left',
            [_Slidertokens.classes.labelAlignLeft]: sliderAlign === 'right',
            [_Slidertokens.classes.labelAlignCenter]: (sliderAlign === 'center' || sliderAlign === 'none') && !scaleTicks,
            [_Slidertokens.classes.scalePlacementTop]: scaleAlign === 'top',
            [_Slidertokens.classes.pointerHidden]: !showPointer
        }),
        hasTicks: Boolean(scaleTicks),
        onPointerEnter: ()=>setIsHovered(true),
        onPointerLeave: ()=>setIsHovered(false)
    }, hasLabelContent && /*#__PURE__*/ _react.default.createElement(_Singlestyles.LabelWrapper, null, (labelContentLeft || labelContent) && /*#__PURE__*/ _react.default.createElement(_Singlestyles.LabelContent, null, labelContent), label && /*#__PURE__*/ _react.default.createElement(_Singlestyles.Label, null, label)), /*#__PURE__*/ _react.default.createElement(_Singlestyles.SliderBaseWrapper, {
        className: (0, _classnames.default)({
            [_Slidertokens.classes.verticalOrientation]: isVertical,
            [_Slidertokens.classes.rangeValuesPlacementInner]: !isVertical && rangeValuesPlacement !== 'outer',
            [_Slidertokens.classes.rangeValuesPlacementOuter]: !isVertical && rangeValuesPlacement === 'outer',
            [_Slidertokens.classes.scalePlacementTop]: scaleAlign === 'top'
        })
    }, innerShowScale && /*#__PURE__*/ _react.default.createElement(_Singlestyles.StyledRangeValue, {
        className: (0, _classnames.default)({
            [_Slidertokens.classes.hideMinValue]: showCurrentValue && (reversed ? hideMaxValueDiff && max - value <= hideMaxValueDiff : hideMinValueDiff && value - min <= hideMinValueDiff),
            [_Slidertokens.classes.activeRangeValue]: value === min
        }),
        style: {
            opacity: rangeMinValueOpacity
        }
    }, reversed ? max : min), /*#__PURE__*/ _react.default.createElement(_Singlestyles.SliderContainer, {
        className: (0, _classnames.default)(isVertical && _Slidertokens.classes.verticalOrientation, reversed && _Slidertokens.classes.reversed, {
            [_Slidertokens.classes.valuePlacementNone]: valuePlacement === 'none',
            [_Slidertokens.classes.valuePlacementLeft]: valuePlacement === 'left',
            [_Slidertokens.classes.valuePlacementRight]: valuePlacement === 'right',
            [_Slidertokens.classes.valuePlacementTop]: valuePlacement === 'top',
            [_Slidertokens.classes.valuePlacementBottom]: valuePlacement === 'bottom'
        }),
        ...rest
    }, /*#__PURE__*/ _react.default.createElement(_Singlestyles.StyledTrack, null), /*#__PURE__*/ _react.default.createElement(_Singlestyles.StyledProgress, {
        style: progressSizeStyle
    }), /*#__PURE__*/ _react.default.createElement(_Singlestyles.StyledRange, {
        type: "range",
        name: name,
        value: value,
        min: min,
        max: max,
        step: step,
        disabled: disabled,
        showPointer: showPointer,
        datatype: "slider-single",
        "aria-label": ariaLabel,
        "aria-valuemin": min,
        "aria-valuemax": max,
        "aria-valuenow": value,
        "aria-orientation": orientation,
        onChange: handleChange,
        onMouseUp: handleChangeCommitted,
        onTouchEnd: handleChangeCommitted,
        onKeyUp: handleChangeCommitted,
        onKeyDown: handleKeyDown
    }), innerShowCurrentValue && /*#__PURE__*/ _react.default.createElement(_Singlestyles.StyledCurrentValue, {
        style: currentValueStyle
    }, value)), innerShowScale && /*#__PURE__*/ _react.default.createElement(_Singlestyles.StyledRangeValue, {
        className: (0, _classnames.default)(_Slidertokens.classes.maxRangeValue, {
            [_Slidertokens.classes.hideMaxValue]: showCurrentValue && (reversed ? hideMinValueDiff && value - min <= hideMinValueDiff : hideMaxValueDiff && max - value <= hideMaxValueDiff),
            [_Slidertokens.classes.activeRangeValue]: value === max
        }),
        style: {
            opacity: rangeMaxValueOpacity
        }
    }, reversed ? min : max), scaleTicks && /*#__PURE__*/ _react.default.createElement(_Singlestyles.ScaleTicksWrapper, {
        isVertical: isVertical,
        reversed: reversed
    }, scaleTicks.map((tick)=>{
        const tickStyle = (0, _getTickStyle.getTickStyle)({
            tick,
            min,
            max,
            isVertical,
            reversed: reversed ?? false
        });
        return /*#__PURE__*/ _react.default.createElement(_Singlestyles.ScaleTick, {
            key: tick,
            tickValue: tick,
            isVertical: isVertical,
            sliderAlign: sliderAlign,
            scaleAlign: scaleAlign,
            style: tickStyle,
            onClick: ()=>handleTickClick(tick)
        }, /*#__PURE__*/ _react.default.createElement(_Singlestyles.ScaleTickDot, {
            isVertical: isVertical,
            filled: tick <= value,
            reversed: reversed,
            scaleAlign: scaleAlign,
            sliderAlign: sliderAlign
        }));
    }))));
};
