// Lalaluxe booking — booking/ui/inspo-upload.jsx (load before app.jsx — see docs/architecture.md, "The no-build multi-file Babel contract")
// The inspo photos: the picker the look screen and checkout share, and the previews in it.

/* ============== Inspo photos ============== */
function ShareImagePreviewList({ files, onRemove }) {
	const [previews, setPreviews] = useState([]);
	useEffect(() => {
		const urls = files.map((file, index) => ({
			file,
			index,
			url: URL.createObjectURL(file),
		}));
		setPreviews(urls);
		return () => {
			urls.forEach((item) => {
				URL.revokeObjectURL(item.url);
			});
		};
	}, [files]);

	if (!files.length) return null;

	return (
		<>
			{previews.map((item) => (
				<div className="share-preview" key={item.url}>
					<img src={item.url} alt={`Inspiration photo ${item.index + 1}`} />
					<button
						type="button"
						onClick={() => onRemove(item.index)}
						aria-label={`Remove inspiration photo ${item.index + 1}`}
					>
						<Ico name="x" />
					</button>
				</div>
			))}
		</>
	);
}

// Inspo image picker, shared by the look screen and checkout. Reads/writes the SAME
// s.shareImages, so a photo attached on either screen flows to the booking once.
function InspoUpload({ s, set }) {
	const files = s.shareImages || [];
	const [busy, setBusy] = useState(false);
	const [error, setError] = useState("");
	const pick = async (e) => {
		const list = e.target.files;
		if (!list || list.length === 0) return;
		setBusy(true);
		set({ uploadBusy: true });
		setError("");
		try {
			// Merge with what's already attached (the remove buttons handle de-selection);
			// only prepare what fits so over-cap picks aren't resized then discarded.
			const room = Math.max(0, SHARE_IMAGE_LIMIT - files.length);
			const prepared = await prepareShareImages(Array.from(list).slice(0, room));
			set({ shareImages: [...files, ...prepared] });
			if (files.length + list.length > SHARE_IMAGE_LIMIT)
				setError(`${prepared.length === 0 ? "No photos" : prepared.length === 1 ? "1 photo" : `${prepared.length} photos`} added. The limit is ${SHARE_IMAGE_LIMIT} photos.`);
		} catch (err) {
			setError(err.message || "Photos couldn’t be added. Try again.");
		} finally {
			setBusy(false);
			set({ uploadBusy: false });
			e.target.value = "";
		}
	};
	const full = files.length >= SHARE_IMAGE_LIMIT;
	return (
		<div className="inspo">
			<div className={"inspo-grid" + (files.length ? " has-files" : "")}>
				<ShareImagePreviewList
					files={files}
					onRemove={(i) => set({ shareImages: files.filter((_, idx) => idx !== i) })}
				/>
				{!full && (
					<label className={"upload-box" + (busy ? " busy" : "")}>
						<input
							type="file"
							accept="image/jpeg,image/png,image/webp"
							multiple
							onChange={pick}
							disabled={busy}
						/>
						<span className="upload-ico">
							<Ico name={busy ? "clock" : "image"} />
						</span>
						<span className="upload-main">{busy ? "Preparing…" : files.length ? "Add More" : "Add Inspo Photos"}</span>
						<span className="upload-sub">
							{files.length
								? `${files.length} of ${SHARE_IMAGE_LIMIT}`
								: `Screenshots welcome · up to ${SHARE_IMAGE_LIMIT}`}
						</span>
					</label>
				)}
			</div>
			{busy && (
				<div className="upload-help sr-only" role="status">
					Preparing photos…
				</div>
			)}
			{error && (
				<div className="upload-error" role="alert">
					{error}
				</div>
			)}
		</div>
	);
}
