import { probeBroadcastAlive } from '@/features/live/gateway'

/**
 * 沿滑动方向查找下一个仍在直播的房间
 */
export async function locateBroadcastInDirection(idList, startIndex, direction) {
	if (!idList?.length || startIndex < 0 || startIndex >= idList.length) {
		return null
	}
	const step = direction >= 0 ? 1 : -1
	const firstId = idList[startIndex]
	if (firstId && await probeBroadcastAlive(firstId)) {
		return { index: startIndex, id: String(firstId), skipped: false }
	}
	for (let i = startIndex + step; i >= 0 && i < idList.length; i += step) {
		const candidate = idList[i]
		if (candidate && await probeBroadcastAlive(candidate)) {
			return { index: i, id: String(candidate), skipped: true }
		}
	}
	return null
}
