import { dialogs } from "../../../svelte-dialogs.config";
import { loginClient } from "#lib/login";
import { addNotification } from "#components/Notifications/shared";

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

	if (!result) return false;

	const [email, passwort] = result;

	const loginResult = await loginClient(email, passwort);

	if (loginResult === null) {
		addNotification({
			type: "error",
			message: "Einloggen fehlgeschlagen",
			dismissable: true,
			subtext: "Bitte überprüfen Sie ihre Eingaben und versuchen es erneut.",
			timeout: 5000,
		})

		return false
	}

	return true
}