import { jsx as e, jsxs as o } from "react/jsx-runtime";
import { useState as m, useEffect as p } from "react";
import { useSearchParams as x, useNavigate as b } from "react-router-dom";
import { Loader2 as v, CheckCircle as y, XCircle as N, AlertCircle as w } from "lucide-react";
import { g as k, d as S, u as C } from "./index-Dh9kUeDn.js";
const I = {
  auth_failed: "L'authentification a échoué. Veuillez réessayer.",
  no_email: "Impossible de récupérer votre adresse email depuis le fournisseur.",
  account_disabled: "Votre compte est désactivé. Contactez l'administrateur.",
  account_locked: "Votre compte est verrouillé. Contactez l'administrateur.",
  internal_error: "Une erreur interne est survenue. Veuillez réessayer."
};
function V() {
  const [i] = x(), u = b(), [l, s] = m("processing"), [h, d] = m("");
  p(() => {
    const r = i.get("token"), c = i.get("refreshToken"), a = i.get("error");
    if (a) {
      s("error"), d(I[a] || "Une erreur est survenue lors de l'authentification.");
      return;
    }
    r && c ? f(r, c) : (s("error"), d("Paramètres d'authentification manquants."));
  }, [i]);
  const f = async (r, c) => {
    try {
      localStorage.setItem("token", r), localStorage.setItem("refreshToken", c);
      try {
        const t = await k(), n = S();
        (t || n) && await fetch("/api/auth/update-session-client-info", {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${r}`
          },
          body: JSON.stringify({ clientIp: t, deviceType: n })
        });
      } catch {
      }
      const a = await fetch("/api/auth/me", {
        headers: {
          Authorization: `Bearer ${r}`
        }
      });
      if (!a.ok)
        throw new Error("Failed to fetch user info");
      const g = await a.json();
      localStorage.setItem("user", JSON.stringify(g));
      try {
        const t = await fetch("/api/onboarding/status", {
          headers: {
            Authorization: `Bearer ${r}`
          }
        });
        if (t.ok && !(await t.json()).hasCompletedOnboarding) {
          s("success"), setTimeout(() => {
            window.location.href = "/auth/onboarding";
          }, 1500);
          return;
        }
      } catch {
      }
      try {
        const t = await C.preferences.get(), n = {
          mode: t.theme || "dark",
          accentColorKey: t.accentColorKey || "indigo",
          borderRadius: t.borderRadius || {},
          itemPaletteKey: t.itemPaletteKey || "neutral"
        };
        localStorage.setItem("smartstack-theme-config", JSON.stringify(n)), t.language && localStorage.setItem("i18nextLng", t.language);
      } catch {
      }
      s("success"), localStorage.removeItem("currentTenantId"), localStorage.removeItem("currentTenantSlug"), localStorage.removeItem("isGlobalView"), setTimeout(() => {
        window.location.href = "/myspace";
      }, 1500);
    } catch (a) {
      console.error("Auth callback error:", a), s("error"), d("Erreur lors de la finalisation de l'authentification.");
    }
  };
  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__ */ o("div", { className: "bg-[var(--bg-secondary)] rounded-2xl p-8 shadow-lg border border-[var(--border-color)]", children: [
    l === "processing" && /* @__PURE__ */ o("div", { className: "text-center", children: [
      /* @__PURE__ */ e(v, { 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: "Authentification en cours..." }),
      /* @__PURE__ */ e("p", { className: "text-[var(--text-secondary)]", children: "Veuillez patienter pendant que nous finalisons votre connexion." })
    ] }),
    l === "success" && /* @__PURE__ */ o("div", { className: "text-center", children: [
      /* @__PURE__ */ e("div", { className: "w-16 h-16 mx-auto bg-green-100 dark:bg-green-900/30 rounded-full flex items-center justify-center mb-4", children: /* @__PURE__ */ e(y, { className: "w-10 h-10 text-green-600 dark:text-green-400" }) }),
      /* @__PURE__ */ e("h2", { className: "text-xl font-semibold mb-2", children: "Connexion réussie !" }),
      /* @__PURE__ */ e("p", { className: "text-[var(--text-secondary)]", children: "Vous allez être redirigé vers votre espace personnel..." })
    ] }),
    l === "error" && /* @__PURE__ */ o("div", { className: "text-center", children: [
      /* @__PURE__ */ e("div", { className: "w-16 h-16 mx-auto bg-red-100 dark:bg-red-900/30 rounded-full flex items-center justify-center mb-4", children: /* @__PURE__ */ e(N, { className: "w-10 h-10 text-red-600 dark:text-red-400" }) }),
      /* @__PURE__ */ e("h2", { className: "text-xl font-semibold mb-2", children: "Échec de l'authentification" }),
      /* @__PURE__ */ e("div", { className: "bg-red-50 dark:bg-red-900/20 rounded-lg p-4 mb-6", children: /* @__PURE__ */ o("div", { className: "flex items-start gap-3", children: [
        /* @__PURE__ */ e(w, { className: "w-5 h-5 text-red-600 dark:text-red-400 flex-shrink-0 mt-0.5" }),
        /* @__PURE__ */ e("p", { className: "text-sm text-red-700 dark:text-red-300", children: h })
      ] }) }),
      /* @__PURE__ */ e(
        "button",
        {
          onClick: () => {
            u("/login");
          },
          className: "btn btn-primary w-full",
          children: "Retourner à la connexion"
        }
      )
    ] })
  ] }) }) });
}
export {
  V as AuthCallbackPage
};
//# sourceMappingURL=AuthCallbackPage-BeqZmVSO.js.map
