const form = document.querySelector("#forgot-form");
const message = document.querySelector("#message");

form.addEventListener("submit", async (event) => {
	event.preventDefault();
	const submitButton = form.querySelector("button[type=submit]");
	submitButton.disabled = true;
	try {
		const response = await fetch("/api/auth/forgot", {
			method: "POST",
			headers: { "content-type": "application/json" },
			body: JSON.stringify(Object.fromEntries(new FormData(form))),
		});
		if (response.status === 429) {
			message.textContent = "Too many requests. Try again in a few minutes.";
			message.className = "auth-message error";
			return;
		}
		// Always show the same message, whether or not the account exists.
		message.textContent =
			"If that email has an account, a reset link is on its way.";
		message.className = "auth-message info";
		form.reset();
	} catch {
		message.textContent = "Could not reach the server. Try again shortly.";
		message.className = "auth-message error";
	} finally {
		submitButton.disabled = false;
	}
});
