/**
 * Multi-image helpers.
 *
 * These are pure functions so they can be tested without a DOM or React.
 * The caller (page.tsx) owns state; these functions own the "which files
 * are new" logic.
 */

/**
 * Returns a stable deduplication key for a File.
 * Key = `name:size:lastModified` — cheap, deterministic, and catches
 * re-selections of the exact same file while keeping clipboard fallback files
 * distinct when they share a generated name and byte size.
 *
 * Two different files that happen to share a generated fallback name but have
 * different timestamps will not collapse to the same key.
 */
export function imageFileKey(file: File): string {
	return `${file.name}:${file.size}:${file.lastModified}`;
}

/**
 * Appends `incoming` files to `existing`, skipping any file whose key
 * already appears in the combined list.
 *
 * Returns a new array — never mutates `existing`.
 */
export function appendImagesDeduped(
	existing: File[],
	incoming: File[],
): File[] {
	const seen = new Set(existing.map(imageFileKey));
	const result = [...existing];

	for (const file of incoming) {
		const key = imageFileKey(file);
		if (!seen.has(key)) {
			seen.add(key);
			result.push(file);
		}
	}

	return result;
}
