import { clamp } from "./clamp"; import { replaceArrayItemAtIndex } from "./replace-array-item-at-index"; /** * Resolves a candidate slider value for the thumb at `index`, clamping it * into `[min, max]` and — for range sliders — into the window bounded by the * thumb's immediate neighbours. The return type is scalar or array * depending on the `range` flag (matches the React source). */ export function getSliderValue( valueInput: number, index: number, min: number, max: number, range: boolean, values: readonly number[], ): number | number[] { let newValue: number | number[] = valueInput; newValue = clamp(newValue, min, max); if (range) { newValue = replaceArrayItemAtIndex( values, index, // Bound the new value to the thumb's neighbours. clamp( newValue, values[index - 1] || Number.NEGATIVE_INFINITY, values[index + 1] || Number.POSITIVE_INFINITY, ), ); } return newValue; }