/**
 * @license Copyright (c) 2017-2025, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md.
 */

/**
 * Wraps a value around the specified range boundaries.
 * When value exceeds max, it wraps around to min.
 * When value is below min, it wraps around to max.
 *
 * @param min - Minimum boundary
 * @param max - Maximum boundary
 * @param value - The value to wrap around
 * @returns The wrapped value
 *
 * @example
 * // Returns 0 (6 exceeds max by 1, wraps to min)
 * wrapAround(0, 5, 6);
 *
 * @example
 * // Returns 2 (7 exceeds max by 2, wraps to min+1)
 * wrapAround(0, 5, 7);
 *
 * @example
 * // Returns 5 (-1 is below min by 1, wraps to max)
 * wrapAround(0, 5, -1);
 */
export function wrapAround( min, max, value ) {
	const range = max - min + 1;

	// Adjust the value to be relative to min
	const relativeValue = value - min;

	// Use modulo to wrap around, adding range to handle negative values
	const wrappedValue = ( ( relativeValue % range ) + range ) % range;

	// Add min back to get the result in the original range
	return wrappedValue + min;
}
