import { dialogs } from "../../../svelte-dialogs.config.js";
import { addNotification } from "#components/Notifications/shared.js";
import { api } from "astro-typesafe-api/client";

export async function spawnSignupPrompt() {
	const result = await dialogs.prompt(
		[
			{
				label: "Vorname",
				type: "text",
				required: true,
				placeholder: "Vorname",
				name: "vorname"
			},
			{
				label: "Name",
				type: "text",
				required: true,
				placeholder: "Name",
				name: "name"
			},
			{
				label: "Email",
				type: "email",
				required: true,
				placeholder: "Email",
				name: "email"
			},
			{
				label: "Passwort",
				type: "password",
				name: "passwort",
				required: true,
				placeholder: "********",
			},
		],
		{
			title: "Registrieren",
			submitButtonText: "Registrieren",
			cancelButtonText: "Abbrechen",
		}
	);

	if (!result) return false;

	const [vorname, name, email, passwort] = result;

	try {
		const response = await api.user.PUT.fetch({
			email,
			passwort,
			vorname,
			name,
		});

		return true;
	} catch(e) {
		addNotification({
			type: "error",
			message: "Registrieren fehlgeschlagen",
			dismissable: true,
			subtext: "Ein Fehler ist aufgetreten, vielleicht wird die angegebene Email bereits verwendet.",
			timeout: 5000,
		})
		return false;
	}
}