import { i as __toESM } from "../_runtime.mjs";
import { g as useNavigate } from "../_libs/@tanstack/react-router+[...].mjs";
import { b as require_react, y as require_jsx_runtime } from "../_libs/@radix-ui/react-accordion+[...].mjs";
import { t as supabase } from "./client-DL1N1MJt.mjs";
import { t as Button } from "./button-BJ7sUxAL.mjs";
import { n as Label, t as Input } from "./label-BJlqMHkc.mjs";
import { n as CardContent, t as Card } from "./card-BuMrzT75.mjs";
import { n as toast } from "../_libs/sonner.mjs";
import { i as SiteLayout, r as PageHeader } from "./SiteLayout-BB0n_2xh.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/auth-Cqn98X6X.js
var import_react = /* @__PURE__ */ __toESM(require_react());
var import_jsx_runtime = require_jsx_runtime();
/** Usernames sign in via an internal email alias; a real email still works. */
function usernameToEmail(username) {
	const u = username.trim().toLowerCase();
	return u.includes("@") ? u : `${u}@admin.svonja-beauty.rs`;
}
function AuthPage() {
	const nav = useNavigate();
	const [mode, setMode] = (0, import_react.useState)("signin");
	const [username, setUsername] = (0, import_react.useState)("");
	const [password, setPassword] = (0, import_react.useState)("");
	const [loading, setLoading] = (0, import_react.useState)(false);
	async function submit(e) {
		e.preventDefault();
		setLoading(true);
		try {
			const email = usernameToEmail(username);
			if (mode === "signin") {
				const { error } = await supabase.auth.signInWithPassword({
					email,
					password
				});
				if (error) throw error;
				toast.success("Prijavljeni ste");
				nav({ to: "/admin" });
			} else {
				const { error } = await supabase.auth.signUp({
					email,
					password,
					options: { emailRedirectTo: window.location.origin + "/admin" }
				});
				if (error) throw error;
				toast.success("Nalog kreiran. Prijavite se.");
				setMode("signin");
			}
		} catch (err) {
			toast.error(err.message || "Greška");
		} finally {
			setLoading(false);
		}
	}
	return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(SiteLayout, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(PageHeader, {
		title: "Admin prijava",
		intro: "Deo za osoblje salona."
	}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
		className: "mx-auto max-w-md px-4 py-10",
		children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Card, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CardContent, {
			className: "p-6",
			children: [
				/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
					className: "mb-4 flex gap-2",
					children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, {
						type: "button",
						variant: mode === "signin" ? "default" : "outline",
						size: "sm",
						onClick: () => setMode("signin"),
						children: "Prijava"
					}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, {
						type: "button",
						variant: mode === "signup" ? "default" : "outline",
						size: "sm",
						onClick: () => setMode("signup"),
						children: "Registracija"
					})]
				}),
				/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("form", {
					onSubmit: submit,
					className: "space-y-4",
					children: [
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Label, {
							htmlFor: "username",
							children: "Korisničko ime"
						}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Input, {
							id: "username",
							autoComplete: "username",
							required: true,
							value: username,
							onChange: (e) => setUsername(e.target.value)
						})] }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Label, {
							htmlFor: "password",
							children: "Lozinka"
						}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Input, {
							id: "password",
							type: "password",
							required: true,
							minLength: 6,
							value: password,
							onChange: (e) => setPassword(e.target.value)
						})] }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, {
							type: "submit",
							className: "w-full",
							disabled: loading,
							children: loading ? "…" : mode === "signin" ? "Prijavi se" : "Registruj se"
						})
					]
				}),
				/* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", {
					className: "mt-4 text-xs text-muted-foreground",
					children: "Prva registrovana osoba automatski dobija admin ulogu."
				})
			]
		}) })
	})] });
}
//#endregion
export { AuthPage as component };
