/** 动态多图网格布局（最多展示 9 张） */
export const GRID_MAX = 9

export function gridColumnCount(count) {
	if (count <= 1) return 1
	if (count === 2 || count === 4) return 2
	return 3
}

export function gridDisplayImages(images = [], max = GRID_MAX) {
	const list = Array.isArray(images) ? images.filter(Boolean) : []
	if (list.length <= max) {
		return { visible: list, overflow: 0 }
	}
	return {
		visible: list.slice(0, max),
		overflow: list.length - max
	}
}

/** 构建详情页 URL */
export function detailPageUrl(id, opts = {}) {
	const q = [`id=${id}`]
	if (opts.focus === 'comments') q.push('focus=comments')
	if (opts.img != null && Number(opts.img) > 0) q.push(`img=${Number(opts.img)}`)
	return `/pages/dynamics/detail?${q.join('&')}`
}

/** 竖滑视频详情页 */
export function videoDetailUrl(id, opts = {}) {
	const q = [`id=${encodeURIComponent(String(id))}`]
	const labelId = opts.labelId ?? opts.label_id
	if (labelId != null && labelId !== '') {
		q.push(`label_id=${encodeURIComponent(String(labelId))}`)
	}
	const listType = opts.type
	if (listType != null && listType !== '') {
		q.push(`type=${encodeURIComponent(String(listType))}`)
	}
	const userNo = opts.user_no ?? opts.userNo
	if (userNo != null && userNo !== '') {
		q.push(`user_no=${encodeURIComponent(String(userNo))}`)
	}
	const page = opts.page
	if (page != null && String(page) !== '' && String(page) !== '1') {
		q.push(`page=${encodeURIComponent(String(page))}`)
	}
	if (opts.compId != null && opts.compId !== '') {
		q.push(`compID=${encodeURIComponent(String(opts.compId))}`)
	}
	const orderType = opts.orderType ?? opts.order_type
	if (orderType != null && orderType !== '') {
		q.push(`order_type=${encodeURIComponent(String(orderType))}`)
	}
	const isHotDian = opts.isHotDian ?? opts.is_hot_dian
	if (isHotDian != null && isHotDian !== '') {
		q.push(`is_hot_dian=${encodeURIComponent(String(isHotDian))}`)
	}
	const search = opts.search
	if (search != null && String(search) !== '') {
		q.push(`search=${encodeURIComponent(String(search))}`)
	}
	return `/pages/feed/reel?${q.join('&')}`
}

export function absolutizePreviewUrls(images, ossFn) {
	return (images || []).map((src) => (typeof ossFn === 'function' ? ossFn(src, 750, 0) : src))
}
