import { useUiTranslation as e } from "../../../i18n/useUiTranslation.js";
import { useState as t } from "react";
//#region src/atomic/organisms/login_form/useLoginForm.ts
function n(n, r, i) {
	let { t: a } = e(), [o, s] = t({
		formData: {
			email: i ?? "",
			password: ""
		},
		errors: {},
		isSubmitting: !1
	});
	function c() {
		let e = {}, t = !0;
		return o.formData.email.trim().length === 0 && (e.email = a("auth.loginForm.errors.emailRequired"), t = !1), o.formData.password.length === 0 ? (e.password = a("auth.loginForm.errors.passwordRequired"), t = !1) : o.formData.password.length < 6 && (e.password = a("auth.loginForm.errors.passwordMinLength", { minLength: 6 }), t = !1), s((t) => ({
			...t,
			errors: e
		})), t;
	}
	function l(e) {
		let { name: t, value: n, type: r, checked: i } = e.target, a = n;
		r === "checkbox" && (a = i), s((e) => ({
			...e,
			formData: {
				...e.formData,
				[t]: a
			},
			errors: {
				...e.errors,
				[t]: void 0,
				form: void 0
			}
		}));
	}
	async function u(e) {
		if (e.preventDefault(), c()) {
			s((e) => ({
				...e,
				isSubmitting: !0
			}));
			try {
				if (n.beginAuthentication != null) {
					let e = await n.beginAuthentication(o.formData.email.trim());
					if ((Array.isArray(e.methods) ? e.methods : []).length === 0) {
						let t = e.lockedUntil == null ? a("auth.loginForm.errors.locked") : a("auth.loginForm.errors.lockedWithTime", { time: new Date(e.lockedUntil).toLocaleString() });
						s((e) => ({
							...e,
							errors: {
								...e.errors,
								form: t
							}
						}));
						return;
					}
				}
				await n.login({
					email: o.formData.email,
					password: o.formData.password
				}) === "success" && r?.();
			} catch {
				let e = a("auth.loginForm.errors.loginFailed"), t = n.error?.message, r = e;
				t != null && t !== "" && (r = t), s((e) => ({
					...e,
					errors: {
						...e.errors,
						form: r
					}
				}));
			} finally {
				s((e) => ({
					...e,
					isSubmitting: !1
				}));
			}
		}
	}
	return {
		formData: o.formData,
		errors: o.errors,
		isSubmitting: n.isLoading || o.isSubmitting,
		handleChange: l,
		handleSubmit: u
	};
}
//#endregion
export { n as useLoginForm };

//# sourceMappingURL=useLoginForm.js.map