"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const acui_1 = require("@Acme Corporation/acui");
const Slider = (0, react_1.forwardRef)(({ name, label, helperText, error: externalError, isRequired = false, isDisabled = false, isReadOnly = false, isInvalid: externalInvalid, size = 'md', min = 0, max = 100, step = 1, showValue = false, showTicks = false, showTooltip = true, formatValue = (value) => value.toString(), className = '', wrapperClassName = '', labelClassName = '', sliderClassName = '', errorClassName = '', helperTextClassName = '', onValueChange, validateOnChange = false, validateOnBlur = true, onChange, onBlur, value, defaultValue, ...props }, ref) => {
    const theme = (0, acui_1.useTheme)();
    const formContext = react_1.default.useContext(FormContext);
    const [localValue, setLocalValue] = react_1.default.useState(value ?? defaultValue ?? min);
    const [isDragging, setIsDragging] = react_1.default.useState(false);
    const sliderRef = react_1.default.useRef(null);
    const combinedRef = react_1.default.useCallback((node) => {
        sliderRef.current = node;
        if (typeof ref === 'function') {
            ref(node);
        }
        else if (ref) {
            ref.current = node;
        }
    }, [ref]);
    const { errors = {}, touched = {}, setFieldValue, setFieldTouched, setFieldError } = formContext || {};
    const error = externalError || errors[name];
    const isInvalid = externalInvalid || (touched[name] && !!error);
    const isControlled = value !== undefined;
    const handleChange = (0, react_1.useCallback)((e) => {
        const newValue = Number(e.target.value);
        if (!isControlled) {
            setLocalValue(newValue);
        }
        onChange?.(e);
        onValueChange?.(newValue);
        if (formContext && validateOnChange) {
            setFieldValue(name, newValue);
        }
    }, [isControlled, onChange, onValueChange, formContext, name, setFieldValue, validateOnChange]);
    const handleBlur = (0, react_1.useCallback)((e) => {
        setIsDragging(false);
        onBlur?.(e);
        if (formContext && validateOnBlur) {
            setFieldTouched(name, true);
        }
    }, [onBlur, formContext, name, setFieldTouched, validateOnBlur]);
    const handleMouseDown = (0, react_1.useCallback)(() => {
        setIsDragging(true);
    }, []);
    const handleMouseUp = (0, react_1.useCallback)(() => {
        setIsDragging(false);
    }, []);
    const currentValue = isControlled ? value : localValue;
    const percentage = ((currentValue - min) / (max - min)) * 100;
    const labelId = react_1.default.useId();
    const errorId = react_1.default.useId();
    const helperTextId = react_1.default.useId();
    const ariaInvalid = isInvalid ? 'true' : undefined;
    const ariaRequired = isRequired ? 'true' : undefined;
    const ariaDescribedBy = error ? errorId : helperText ? helperTextId : undefined;
    return ((0, jsx_runtime_1.jsxs)("div", { className: `bf-slider-wrapper ${wrapperClassName}`, "data-size": size, "data-invalid": isInvalid, "data-disabled": isDisabled, "data-readonly": isReadOnly, children: [label && ((0, jsx_runtime_1.jsxs)("label", { id: labelId, htmlFor: name, className: `bf-slider-label ${labelClassName}`, children: [label, isRequired && (0, jsx_runtime_1.jsx)("span", { className: "bf-required-mark", "aria-hidden": "true", children: "*" }), showValue && ((0, jsx_runtime_1.jsx)("span", { className: "bf-slider-value", "aria-hidden": "true", children: formatValue(currentValue) }))] })), (0, jsx_runtime_1.jsxs)("div", { className: "bf-slider-container", children: [(0, jsx_runtime_1.jsx)("input", { ref: combinedRef, type: "range", id: name, name: name, min: min, max: max, step: step, value: currentValue, onChange: handleChange, onBlur: handleBlur, onMouseDown: handleMouseDown, onMouseUp: handleMouseUp, disabled: isDisabled, readOnly: isReadOnly, required: isRequired, "aria-labelledby": label ? labelId : undefined, "aria-invalid": ariaInvalid, "aria-required": ariaRequired, "aria-describedby": ariaDescribedBy, "aria-valuemin": min, "aria-valuemax": max, "aria-valuenow": currentValue, "aria-valuetext": formatValue(currentValue), className: `bf-slider ${sliderClassName} ${className}`, style: {
                            '--slider-percentage': `${percentage}%`,
                            '--slider-value': formatValue(currentValue),
                        }, ...props }), showTicks && ((0, jsx_runtime_1.jsx)("div", { className: "bf-slider-ticks", "aria-hidden": "true", children: Array.from({ length: (max - min) / step + 1 }, (_, i) => ((0, jsx_runtime_1.jsx)("span", { className: "bf-slider-tick", style: { left: `${(i * step / (max - min)) * 100}%` } }, i))) })), showTooltip && isDragging && ((0, jsx_runtime_1.jsx)("div", { className: "bf-slider-tooltip", style: { left: `${percentage}%` }, role: "tooltip", "aria-hidden": "true", children: formatValue(currentValue) }))] }), error && ((0, jsx_runtime_1.jsx)("div", { id: errorId, className: `bf-slider-error ${errorClassName}`, role: "alert", "aria-live": "assertive", children: error })), helperText && !error && ((0, jsx_runtime_1.jsx)("div", { id: helperTextId, className: `bf-slider-helper-text ${helperTextClassName}`, role: "status", children: helperText }))] }));
});
Slider.displayName = 'Slider';
exports.default = (0, react_1.memo)(Slider);
//# sourceMappingURL=Slider.js.map