/**
 * Bounds-checked array access
 *
 * An out-of-bounds access is considered an error.
 *
 * @param index The index
 * @param array The array
 * @returns Value at index
 */
export function itemAt<T>(index: number, array: readonly T[]): T {
	const value = array[index];
	if (value === undefined) {
		wsi4.throwError(`Out of bounds access: ${array.length} vs ${index}`);
	}
	return value;
}

/**
 * Bounds-checked array access to first element
 *
 * An out-of-bounds access is considered an error.
 *
 * @param array The array
 * @returns First value of array
 */
export function front<T>(array: readonly T[]): T {
	if (array.length === 0) {
		wsi4.throwError("front() called for empty array");
	}
	return array[0]!;
}

/**
 * Bounds-checked array access to last element
 *
 * An out-of-bounds access is considered an error.
 *
 * @param array The array
 * @returns Last value of array
 */
export function back<T>(array: readonly T[]): T {
	if (array.length === 0) {
		wsi4.throwError("back() called for empty array");
	}
	return array[array.length - 1]!;
}
