"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@open-mercato/ui/primitives/card";
import { Button } from "@open-mercato/ui/primitives/button";
import { PasswordInput } from "@open-mercato/ui/primitives/password-input";
import { Label } from "@open-mercato/ui/primitives/label";
import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { formatPasswordRequirements, getPasswordPolicy, validatePassword } from "@open-mercato/shared/lib/auth/passwordPolicy";
function ResetWithTokenPage({ params }) {
  const router = useRouter();
  const t = useT();
  const [error, setError] = useState(null);
  const [submitting, setSubmitting] = useState(false);
  const [clientReady, setClientReady] = useState(false);
  const passwordPolicy = getPasswordPolicy();
  const passwordRequirements = formatPasswordRequirements(passwordPolicy, t);
  const passwordDescription = passwordRequirements ? t("auth.password.requirements.help", "Password requirements: {requirements}", { requirements: passwordRequirements }) : "";
  useEffect(() => {
    setClientReady(true);
  }, []);
  async function onSubmit(e) {
    e.preventDefault();
    setError(null);
    const form = new FormData(e.currentTarget);
    const password = String(form.get("password") ?? "");
    const confirmPassword = String(form.get("confirmPassword") ?? "");
    if (!password) {
      setError(t("auth.profile.form.errors.newPasswordRequired", "New password is required."));
      return;
    }
    if (!confirmPassword) {
      setError(t("auth.profile.form.errors.confirmPasswordRequired", "Please confirm the new password."));
      return;
    }
    if (password !== confirmPassword) {
      setError(t("auth.profile.form.errors.passwordMismatch", "Passwords do not match."));
      return;
    }
    if (!validatePassword(password, passwordPolicy).ok) {
      setError(t("auth.profile.form.errors.passwordRequirements", "Password must meet the requirements."));
      return;
    }
    setSubmitting(true);
    try {
      form.set("token", params.token);
      const { ok, result } = await apiCall(
        "/api/auth/reset/confirm",
        { method: "POST", body: form }
      );
      if (!ok || result?.ok === false) {
        setError(result?.error || t("auth.reset.errors.failed", "Unable to reset password"));
        return;
      }
      router.replace(result?.redirect || "/login");
    } finally {
      setSubmitting(false);
    }
  }
  return /* @__PURE__ */ jsx("div", { className: "min-h-svh flex items-center justify-center p-4", children: /* @__PURE__ */ jsxs(Card, { className: "w-full max-w-sm", children: [
    /* @__PURE__ */ jsxs(CardHeader, { children: [
      /* @__PURE__ */ jsx(CardTitle, { children: t("auth.reset.title", "Set a new password") }),
      /* @__PURE__ */ jsx(CardDescription, { children: t("auth.reset.subtitle", "Choose a strong password for your account.") })
    ] }),
    /* @__PURE__ */ jsx(CardContent, { children: /* @__PURE__ */ jsxs("form", { className: "grid gap-3", onSubmit, "data-auth-ready": clientReady ? "1" : "0", children: [
      error && /* @__PURE__ */ jsx("div", { className: "text-sm text-status-error-text", children: error }),
      /* @__PURE__ */ jsxs("div", { className: "grid gap-1", children: [
        /* @__PURE__ */ jsx(Label, { htmlFor: "password", children: t("auth.reset.form.password", "New password") }),
        /* @__PURE__ */ jsx(PasswordInput, { id: "password", name: "password", required: true, minLength: passwordPolicy.minLength, autoComplete: "new-password" }),
        passwordDescription ? /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: passwordDescription }) : null
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "grid gap-1", children: [
        /* @__PURE__ */ jsx(Label, { htmlFor: "confirmPassword", children: t("auth.profile.form.confirmPassword", "Confirm new password") }),
        /* @__PURE__ */ jsx(
          PasswordInput,
          {
            id: "confirmPassword",
            name: "confirmPassword",
            required: true,
            minLength: passwordPolicy.minLength,
            autoComplete: "new-password"
          }
        )
      ] }),
      /* @__PURE__ */ jsx(Button, { type: "submit", className: "mt-2 w-full", disabled: submitting, children: submitting ? t("auth.reset.form.loading", "...") : t("auth.reset.form.submit", "Update password") })
    ] }) })
  ] }) });
}
export {
  ResetWithTokenPage as default
};
//# sourceMappingURL=page.js.map
