"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useEffect, useState } from "react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
import { Button } from "@open-mercato/ui/primitives/button";
import { Alert, AlertDescription } from "@open-mercato/ui/primitives/alert";
import { Spinner } from "@open-mercato/ui/primitives/spinner";
function PortalVerifyPage({ params }) {
  const t = useT();
  const searchParams = useSearchParams();
  const token = searchParams.get("token") ?? "";
  const [state, setState] = useState(token ? "verifying" : "error");
  const [error, setError] = useState(token ? null : t("portal.verify.error.invalidToken", "Verification token is missing."));
  useEffect(() => {
    if (!token) return;
    let active = true;
    void apiCall("/api/customer_accounts/email/verify", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ token })
    }).then((result) => {
      if (!active) return;
      if (result.ok && result.result?.ok) {
        setState("success");
        return;
      }
      setState("error");
      setError(result.result?.error || t("portal.verify.error.generic", "Email verification failed. Please try again."));
    }).catch(() => {
      if (!active) return;
      setState("error");
      setError(t("portal.verify.error.generic", "Email verification failed. Please try again."));
    });
    return () => {
      active = false;
    };
  }, [token, t]);
  if (state === "verifying") {
    return /* @__PURE__ */ jsxs("div", { className: "mx-auto flex max-w-sm flex-col items-center gap-4 py-12 text-center", children: [
      /* @__PURE__ */ jsx(Spinner, {}),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("h1", { className: "text-2xl font-bold tracking-tight", children: t("portal.verify.title", "Verifying email") }),
        /* @__PURE__ */ jsx("p", { className: "mt-1.5 text-sm text-muted-foreground", children: t("portal.verify.description", "Please wait while we verify your email address.") })
      ] })
    ] });
  }
  if (state === "success") {
    return /* @__PURE__ */ jsxs("div", { className: "mx-auto flex max-w-sm flex-col items-center gap-4 py-12 text-center", children: [
      /* @__PURE__ */ jsx("div", { className: "mx-auto flex size-12 items-center justify-center rounded-full bg-foreground text-background", children: /* @__PURE__ */ jsx("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", className: "size-6", children: /* @__PURE__ */ jsx("polyline", { points: "20 6 9 17 4 12" }) }) }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("h1", { className: "text-2xl font-bold tracking-tight", children: t("portal.verify.success.title", "Email verified") }),
        /* @__PURE__ */ jsx("p", { className: "mt-1.5 text-sm text-muted-foreground", children: t("portal.verify.success.description", "Your email has been verified. You can now sign in to the portal.") })
      ] }),
      /* @__PURE__ */ jsx(Button, { asChild: true, className: "w-full rounded-lg", children: /* @__PURE__ */ jsx(Link, { href: `/${params.orgSlug}/portal/login`, children: t("portal.verify.success.loginLink", "Sign In") }) })
    ] });
  }
  return /* @__PURE__ */ jsxs("div", { className: "mx-auto flex max-w-sm flex-col gap-4 py-12", children: [
    /* @__PURE__ */ jsx(Alert, { variant: "destructive", children: /* @__PURE__ */ jsx(AlertDescription, { children: error || t("portal.verify.error.generic", "Email verification failed. Please try again.") }) }),
    /* @__PURE__ */ jsx(Button, { asChild: true, variant: "outline", className: "rounded-lg", children: /* @__PURE__ */ jsx(Link, { href: `/${params.orgSlug}/portal/login`, children: t("portal.verify.error.backToLogin", "Back to sign in") }) })
  ] });
}
export {
  PortalVerifyPage as default
};
//# sourceMappingURL=page.js.map
