import { jsx as e, jsxs as s } from "react/jsx-runtime";
import { useState as m, useEffect as x } from "react";
import { useTranslation as p } from "react-i18next";
import { useSearchParams as v, useNavigate as y } from "react-router-dom";
import { Loader2 as N, CheckCircle as w, XCircle as C, AlertCircle as k } from "lucide-react";
import { f as P, h as S, i as E } from "./index-CeEMms9z.js";
function D() {
  const { t: a } = p("auth"), [n] = v(), u = y(), [l, c] = m("processing"), [h, i] = m("");
  x(() => {
    const b = {
      auth_failed: a("authCallbackPage.authFailedError"),
      no_email: a("authCallbackPage.noEmailError"),
      account_disabled: a("authCallbackPage.accountDisabledError"),
      account_locked: a("authCallbackPage.accountLockedError"),
      internal_error: a("authCallbackPage.internalError")
    }, d = n.get("error");
    if (d) {
      c("error"), i(b[d] || a("authCallbackPage.generalError"));
      return;
    }
    (async () => {
      try {
        try {
          const t = await P(), r = S();
          (t || r) && await fetch("/api/auth/update-session-client-info", {
            method: "POST",
            credentials: "include",
            headers: {
              "Content-Type": "application/json"
            },
            body: JSON.stringify({ clientIp: t, deviceType: r })
          });
        } catch {
        }
        const o = await fetch("/api/auth/me", {
          credentials: "include"
        });
        if (!o.ok)
          throw new Error("Failed to fetch user info");
        const f = await o.json();
        localStorage.setItem("user", JSON.stringify(f));
        try {
          const t = await fetch("/api/onboarding/status", {
            credentials: "include"
          });
          if (t.ok && !(await t.json()).hasCompletedOnboarding) {
            c("success"), setTimeout(() => {
              window.location.href = "/auth/onboarding";
            }, 1500);
            return;
          }
        } catch {
        }
        try {
          const t = await E.preferences.get(), r = {
            mode: t.theme || "dark",
            accentColorKey: t.accentColorKey || "indigo",
            borderRadius: t.borderRadius || {},
            itemPaletteKey: t.itemPaletteKey || "neutral"
          };
          localStorage.setItem("smartstack-theme-config", JSON.stringify(r)), t.language && localStorage.setItem("i18nextLng", t.language);
        } catch {
        }
        c("success"), localStorage.removeItem("currentTenantId"), localStorage.removeItem("currentTenantSlug"), localStorage.removeItem("isGlobalView"), setTimeout(() => {
          window.location.href = "/myspace";
        }, 1500);
      } catch (o) {
        console.error("Auth callback error:", o), c("error"), i(a("authCallbackPage.generalError"));
      }
    })();
  }, [n, a]);
  const g = () => {
    u("/login");
  };
  return /* @__PURE__ */ e("div", { className: "min-h-screen flex items-center justify-center bg-[var(--bg-primary)]", children: /* @__PURE__ */ e("div", { className: "max-w-md w-full mx-4", children: /* @__PURE__ */ s("div", { className: "bg-[var(--bg-secondary)] rounded-2xl p-8 shadow-lg border border-[var(--border-color)]", children: [
    l === "processing" && /* @__PURE__ */ s("div", { className: "text-center", children: [
      /* @__PURE__ */ e(N, { className: "w-16 h-16 mx-auto text-[var(--color-accent-500)] animate-spin mb-4" }),
      /* @__PURE__ */ e("h2", { className: "text-xl font-semibold mb-2", children: a("authCallbackPage.processing") }),
      /* @__PURE__ */ e("p", { className: "text-[var(--text-secondary)]", children: a("authCallbackPage.success") })
    ] }),
    l === "success" && /* @__PURE__ */ s("div", { className: "text-center", children: [
      /* @__PURE__ */ e("div", { className: "w-16 h-16 mx-auto bg-[var(--success-bg)] rounded-full flex items-center justify-center mb-4", children: /* @__PURE__ */ e(w, { className: "w-10 h-10 text-[var(--success-text)]" }) }),
      /* @__PURE__ */ e("h2", { className: "text-xl font-semibold mb-2", children: a("authCallbackPage.success") }),
      /* @__PURE__ */ e("p", { className: "text-[var(--text-secondary)]", children: a("authCallbackPage.success") })
    ] }),
    l === "error" && /* @__PURE__ */ s("div", { className: "text-center", children: [
      /* @__PURE__ */ e("div", { className: "w-16 h-16 mx-auto bg-[var(--error-bg)] rounded-full flex items-center justify-center mb-4", children: /* @__PURE__ */ e(C, { className: "w-10 h-10 text-[var(--error-text)]" }) }),
      /* @__PURE__ */ e("h2", { className: "text-xl font-semibold mb-2", children: a("authCallbackPage.generalError") }),
      /* @__PURE__ */ e("div", { className: "bg-[var(--error-bg)] rounded-lg p-4 mb-6", children: /* @__PURE__ */ s("div", { className: "flex items-start gap-3", children: [
        /* @__PURE__ */ e(k, { className: "w-5 h-5 text-[var(--error-text)] flex-shrink-0 mt-0.5" }),
        /* @__PURE__ */ e("p", { className: "text-sm text-[var(--error-text)]", children: h })
      ] }) }),
      /* @__PURE__ */ e(
        "button",
        {
          onClick: g,
          className: "btn btn-primary w-full",
          children: a("login.submit")
        }
      )
    ] })
  ] }) }) });
}
export {
  D as AuthCallbackPage
};
//# sourceMappingURL=AuthCallbackPage-CyB6pmme.js.map
