import { Button as e } from "../primitives/button.js";
import { useAuth as t } from "../hooks/useAuth.js";
import { usePasskey as n } from "../hooks/usePasskey.js";
import { Input as r } from "../primitives/input.js";
import { Field as i } from "../primitives/field.js";
import { Alert as a, AlertDescription as o, AlertTitle as s } from "../primitives/alert.js";
import { useId as c, useState as l } from "react";
import { jsx as u, jsxs as d } from "react/jsx-runtime";
//#region src/react/auth/login-form.tsx
function f({ authConfig: f, emailLabel: p = "Email", passwordLabel: m = "Password", submitLabel: h = "Sign in", passkeyLabel: g = "Sign in with passkey", forgotPasswordLabel: _ = "Forgot password?", forgotPasswordHref: v, onSuccess: y }) {
	let { manager: b, refresh: x, user: S } = t(f), { isSupported: C } = n(), [w, T] = l(""), [E, D] = l(""), [O, k] = l(!1), [A, j] = l(null), M = c(), N = c(), P = c();
	async function F(e) {
		e.preventDefault(), j(null), k(!0);
		try {
			let e = b.config.apiBasePath, t = await fetch(`${e}/login`, {
				method: "POST",
				headers: { "Content-Type": "application/json" },
				body: JSON.stringify({
					email: w,
					password: E
				})
			});
			if (!t.ok) {
				let e = "Invalid email or password.";
				try {
					let n = await t.json();
					e = n.detail ?? n.message ?? e;
				} catch {}
				throw Error(e);
			}
			let n = await t.json();
			if (!n.access_token) throw Error("No access token returned from server.");
			b.setTokens(n.access_token, n.refresh_token), x();
			let r = b.getAccessToken(), i = r ? b.parseToken(r) : null, a = i ? {
				id: i.sub,
				username: i.sub,
				email: w,
				roles: i.roles ?? [],
				groups: i.groups ?? [],
				is_active: !0,
				is_verified: !0
			} : S ?? {
				id: "",
				username: "",
				email: w,
				roles: [],
				groups: [],
				is_active: !0,
				is_verified: !0
			};
			y?.(a);
		} catch (e) {
			j(e instanceof Error ? e.message : "An unexpected error occurred.");
		} finally {
			k(!1);
		}
	}
	let I = A !== null;
	return /* @__PURE__ */ d("form", {
		onSubmit: (e) => void F(e),
		noValidate: !0,
		"aria-label": "Sign in",
		className: "flex w-full flex-col gap-5",
		children: [
			I && /* @__PURE__ */ d(a, {
				variant: "error",
				id: P,
				role: "alert",
				"aria-live": "polite",
				children: [/* @__PURE__ */ u(s, { children: "Sign in failed" }), /* @__PURE__ */ u(o, { children: A })]
			}),
			/* @__PURE__ */ u(i, {
				label: p,
				htmlFor: M,
				required: !0,
				...I ? { error: " " } : {},
				children: /* @__PURE__ */ u(r, {
					id: M,
					type: "email",
					autoComplete: "email",
					value: w,
					onChange: (e) => {
						T(e.currentTarget.value);
					},
					disabled: O,
					error: I,
					...I ? { errorMessageId: P } : {},
					required: !0,
					placeholder: "you@example.com"
				})
			}),
			/* @__PURE__ */ u(i, {
				label: m,
				htmlFor: N,
				required: !0,
				...I ? { error: " " } : {},
				children: /* @__PURE__ */ u(r, {
					id: N,
					type: "password",
					autoComplete: "current-password",
					value: E,
					onChange: (e) => {
						D(e.currentTarget.value);
					},
					disabled: O,
					error: I,
					...I ? { errorMessageId: P } : {},
					required: !0
				})
			}),
			/* @__PURE__ */ d("div", {
				className: "flex flex-col gap-2",
				children: [
					/* @__PURE__ */ u(e, {
						variant: "default",
						type: "submit",
						loading: O,
						className: "w-full",
						children: h
					}),
					C && /* @__PURE__ */ u(e, {
						variant: "secondary",
						type: "button",
						disabled: O,
						className: "w-full",
						children: g
					}),
					v && /* @__PURE__ */ u(e, {
						variant: "link",
						size: "sm",
						type: "button",
						asChild: !0,
						className: "self-end",
						children: /* @__PURE__ */ u("a", {
							href: v,
							children: _
						})
					})
				]
			})
		]
	});
}
//#endregion
export { f as LoginForm };

//# sourceMappingURL=login-form.js.map