"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useEffect, useMemo } from "react";
import { useRouter } from "next/navigation";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { Badge } from "@open-mercato/ui/primitives/badge";
import { Spinner } from "@open-mercato/ui/primitives/spinner";
import { usePortalContext } from "@open-mercato/ui/portal/PortalContext";
import { PortalPageHeader } from "@open-mercato/ui/portal/components/PortalPageHeader";
import { PortalCard, PortalCardHeader, PortalStatRow, PortalCardDivider } from "@open-mercato/ui/portal/components/PortalCard";
import { InjectionSpot } from "@open-mercato/ui/backend/injection/InjectionSpot";
import { PortalInjectionSpots } from "@open-mercato/ui/backend/injection/spotIds";
function PortalProfilePage({ params }) {
  const t = useT();
  const router = useRouter();
  const { auth } = usePortalContext();
  const { user, roles, resolvedFeatures, isPortalAdmin, loading } = auth;
  useEffect(() => {
    if (!loading && !user) {
      router.replace(`/${params.orgSlug}/portal/login`);
    }
  }, [loading, user, router, params.orgSlug]);
  const injectionContext = useMemo(
    () => ({ orgSlug: params.orgSlug, user, roles, resolvedFeatures, isPortalAdmin }),
    [params.orgSlug, user, roles, resolvedFeatures, isPortalAdmin]
  );
  if (loading) {
    return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center py-20", children: /* @__PURE__ */ jsx(Spinner, {}) });
  }
  if (!user) return null;
  const formatDate = (dateString) => {
    if (!dateString) return t("portal.dashboard.never", "Never");
    return new Date(dateString).toLocaleDateString(void 0, {
      year: "numeric",
      month: "short",
      day: "numeric",
      hour: "2-digit",
      minute: "2-digit"
    });
  };
  return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-8", children: [
    /* @__PURE__ */ jsx(
      PortalPageHeader,
      {
        label: t("portal.profile.label", "Account"),
        title: t("portal.profile.title", "Profile")
      }
    ),
    /* @__PURE__ */ jsx(InjectionSpot, { spotId: PortalInjectionSpots.pageBefore("profile"), context: injectionContext }),
    /* @__PURE__ */ jsxs("div", { className: "grid gap-5 md:grid-cols-2", children: [
      /* @__PURE__ */ jsxs(PortalCard, { children: [
        /* @__PURE__ */ jsx(PortalCardHeader, { label: t("portal.dashboard.profile", "Profile"), title: user.displayName }),
        /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
          /* @__PURE__ */ jsx(
            PortalStatRow,
            {
              label: t("portal.dashboard.email", "Email"),
              value: /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-2", children: [
                /* @__PURE__ */ jsx("span", { className: "truncate", children: user.email }),
                /* @__PURE__ */ jsx(Badge, { variant: user.emailVerified ? "default" : "outline", className: "shrink-0 text-overline", children: user.emailVerified ? t("portal.dashboard.emailVerified", "Verified") : t("portal.dashboard.emailNotVerified", "Unverified") })
              ] })
            }
          ),
          /* @__PURE__ */ jsx(PortalCardDivider, {}),
          /* @__PURE__ */ jsx(PortalStatRow, { label: t("portal.dashboard.lastLogin", "Last login"), value: formatDate(user.lastLoginAt) }),
          /* @__PURE__ */ jsx(PortalCardDivider, {}),
          /* @__PURE__ */ jsx(PortalStatRow, { label: t("portal.dashboard.memberSince", "Member since"), value: formatDate(user.createdAt) }),
          isPortalAdmin ? /* @__PURE__ */ jsxs(Fragment, { children: [
            /* @__PURE__ */ jsx(PortalCardDivider, {}),
            /* @__PURE__ */ jsx(PortalStatRow, { label: t("portal.dashboard.roles", "Role"), value: /* @__PURE__ */ jsx(Badge, { className: "text-overline", children: t("portal.dashboard.portalAdmin", "Portal Admin") }) })
          ] }) : null
        ] })
      ] }),
      /* @__PURE__ */ jsxs(PortalCard, { children: [
        /* @__PURE__ */ jsx(PortalCardHeader, { label: t("portal.dashboard.roles", "Roles"), title: `${roles.length} assigned` }),
        roles.length > 0 ? /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-2", children: roles.map((role) => /* @__PURE__ */ jsx("span", { className: "inline-flex items-center rounded-lg border px-3 py-1.5 text-xs font-medium", children: role.name }, role.id)) }) : /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: t("portal.dashboard.noRoles", "No roles assigned") })
      ] }),
      /* @__PURE__ */ jsxs(PortalCard, { className: "md:col-span-2", children: [
        /* @__PURE__ */ jsx(PortalCardHeader, { label: t("portal.dashboard.permissions", "Permissions"), title: `${resolvedFeatures.length} features` }),
        resolvedFeatures.length > 0 ? /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-2", children: resolvedFeatures.map((feature) => /* @__PURE__ */ jsx("span", { className: "inline-flex items-center rounded-md border bg-muted/50 px-2 py-1 font-mono text-overline text-muted-foreground", children: feature }, feature)) }) : /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: t("portal.dashboard.noPermissions", "No permissions") })
      ] })
    ] }),
    /* @__PURE__ */ jsx(InjectionSpot, { spotId: PortalInjectionSpots.pageAfter("profile"), context: injectionContext })
  ] });
}
export {
  PortalProfilePage as default
};
//# sourceMappingURL=page.js.map
