/** VIP 点播页虚拟下单跑马灯：文案与时间偏移生成 */

const PREFIX_POOL =
	'用花张林李王刘陈杨赵黄周吴徐孙胡朱高郭何罗蒋沈韩喻柏窦彭潘袁邓';

function randInt(min, max) {
	return min + Math.floor(Math.random() * (max - min + 1));
}

/** 脱敏片段：4～6 位掩码 + 末位随机数字或字母 */
export function randomMaskedIdTail() {
	const maskLen = randInt(4, 6);
	const chars =
		'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	const suffix = chars[randInt(0, chars.length - 1)];
	return '*'.repeat(maskLen) + suffix;
}

/** 时间偏移文案：均在 10 分钟内；30% 秒 / 70% 分钟 */
export function randomTimeOffsetPhrase() {
	const roll = Math.random();
	if (roll < 0.3) {
		const sec = randInt(5, 59);
		return `${sec}秒前`;
	}
	const min = randInt(1, 10);
	return `${min}分钟前`;
}

/** 生成一条虚拟下单文案：[一字昵称]+[脱敏ID]+[时间]已下单 */
export function generateFakeOrderLine() {
	const payload = generateFakeOrderPayload();
	return `${payload.nickname}${payload.maskedId}${payload.timeOffset}${payload.suffix}`;
}

/** 结构化虚拟文案：便于前端分段单独控制样式 */
export function generateFakeOrderPayload() {
	return {
		type: 'virtual',
		nickname: PREFIX_POOL[randInt(0, PREFIX_POOL.length - 1)],
		maskedId: randomMaskedIdTail(),
		timeOffset: randomTimeOffsetPhrase(),
		suffix: '已下单',
	};
}

/** 初始化临时下单池（50～100 条，尽量不重复） */
export function buildFakeOrderPool() {
	const targetSize = randInt(50, 100);
	const pool = [];
	const seen = new Set();
	let guard = 0;
	while (pool.length < targetSize && guard < targetSize * 20) {
		guard++;
		const line = generateFakeOrderPayload();
		const key = `${line.nickname}${line.maskedId}${line.timeOffset}${line.suffix}`;
		if (seen.has(key)) continue;
		seen.add(key);
		pool.push(line);
	}
	while (pool.length < Math.min(targetSize, 50)) {
		pool.push(generateFakeOrderPayload());
	}
	return pool;
}
