"use client";
'use strict';

var chunkMT66VKLS_js = require('./chunk-MT66VKLS.js');
var chunkYXN2K77G_js = require('./chunk-YXN2K77G.js');
var chunkBHOT22QL_js = require('./chunk-BHOT22QL.js');
var chunkUZ3CMNUJ_js = require('./chunk-UZ3CMNUJ.js');
var react = require('react');
var outline = require('@heroicons/react/24/outline');
var jsxRuntime = require('react/jsx-runtime');

var ALL_TABS = ["members", "invitations", "domains"];
function MembersTabContent({
  organizationId,
  client
}) {
  const t = chunkYXN2K77G_js.useTranslations("windsock");
  const [members, setMembers] = react.useState([]);
  const [isLoading, setIsLoading] = react.useState(true);
  const [removingId, setRemovingId] = react.useState(null);
  const [removeTarget, setRemoveTarget] = react.useState(null);
  const fetchMembers = react.useCallback(async () => {
    setIsLoading(true);
    try {
      const result = await client.listOrganizationMembers(organizationId);
      setMembers(result);
    } catch {
    } finally {
      setIsLoading(false);
    }
  }, [client, organizationId]);
  react.useEffect(() => {
    fetchMembers();
  }, [fetchMembers]);
  const handleRemoveRequest = react.useCallback((member) => {
    chunkUZ3CMNUJ_js.triggerHaptic("warning");
    setRemoveTarget(member);
  }, []);
  const handleRemoveConfirm = react.useCallback(async () => {
    if (!removeTarget) return;
    setRemovingId(removeTarget.id);
    chunkUZ3CMNUJ_js.triggerHaptic("medium");
    try {
      await client.removeOrganizationMember(organizationId, removeTarget.id);
      setRemoveTarget(null);
      chunkUZ3CMNUJ_js.triggerHaptic("success");
      fetchMembers();
    } catch {
      chunkUZ3CMNUJ_js.triggerHaptic("error");
    } finally {
      setRemovingId(null);
    }
  }, [removeTarget, client, organizationId, fetchMembers]);
  const handleRemoveCancel = react.useCallback(() => {
    chunkUZ3CMNUJ_js.triggerHaptic("light");
    setRemoveTarget(null);
  }, []);
  const roleBadgeColor = (role) => {
    if (role === "owner") return "purple";
    if (role === "admin") return "blue";
    return "zinc";
  };
  if (isLoading) {
    return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-center gap-2 p-8 text-sm text-gray-500 dark:text-gray-400", children: [
      /* @__PURE__ */ jsxRuntime.jsx(chunkMT66VKLS_js.InlineSpinner, {}),
      /* @__PURE__ */ jsxRuntime.jsx("span", { children: t("admin.organizationDetail.members.loading") })
    ] });
  }
  if (members.length === 0) {
    return /* @__PURE__ */ jsxRuntime.jsx(
      chunkMT66VKLS_js.EmptyState,
      {
        message: t("admin.organizationDetail.members.emptyMessage"),
        description: t("admin.organizationDetail.members.emptyDescription"),
        icon: outline.UsersIcon,
        variant: "card"
      }
    );
  }
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    /* @__PURE__ */ jsxRuntime.jsx(chunkMT66VKLS_js.ListCard, { children: members.map((member) => /* @__PURE__ */ jsxRuntime.jsx(
      chunkMT66VKLS_js.ListCardItem,
      {
        leading: /* @__PURE__ */ jsxRuntime.jsx(
          chunkMT66VKLS_js.Avatar,
          {
            src: member.image,
            initials: (member.name ?? member.email).slice(0, 2).toUpperCase(),
            alt: member.name ?? member.email
          }
        ),
        trailing: member.role !== "owner" ? /* @__PURE__ */ jsxRuntime.jsx(
          chunkBHOT22QL_js.Button,
          {
            size: "sm",
            color: "ios-glass-red",
            onClick: () => handleRemoveRequest(member),
            loading: removingId === member.id,
            disabled: removingId !== null,
            children: /* @__PURE__ */ jsxRuntime.jsx(outline.TrashIcon, { className: "h-3.5 w-3.5" })
          }
        ) : void 0,
        children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
          /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "min-w-0", children: [
            /* @__PURE__ */ jsxRuntime.jsx("p", { className: "truncate text-sm font-medium text-gray-900 dark:text-white", children: member.name ?? member.email }),
            /* @__PURE__ */ jsxRuntime.jsx("p", { className: "truncate text-xs text-gray-500 dark:text-gray-400", children: member.email })
          ] }),
          /* @__PURE__ */ jsxRuntime.jsx(chunkBHOT22QL_js.Badge, { color: roleBadgeColor(member.role), children: member.role })
        ] })
      },
      member.id
    )) }),
    /* @__PURE__ */ jsxRuntime.jsx(
      chunkMT66VKLS_js.DynamicIslandConfirm,
      {
        open: removeTarget !== null,
        onClose: handleRemoveCancel,
        onConfirm: handleRemoveConfirm,
        title: t("admin.organizationDetail.members.removeConfirmTitle", { name: removeTarget?.name ?? removeTarget?.email ?? "" }),
        icon: /* @__PURE__ */ jsxRuntime.jsx(outline.TrashIcon, { className: "h-5 w-5 text-white" }),
        iconBackground: "bg-ios-red",
        confirmLabel: t("admin.organizationDetail.members.removeConfirmButton"),
        cancelLabel: t("admin.organizationDetail.members.removeCancelButton")
      }
    )
  ] });
}
function InvitationsTabContent({
  organizationId,
  client
}) {
  const t = chunkYXN2K77G_js.useTranslations("windsock");
  const format = chunkYXN2K77G_js.useFormatter();
  const [invitations, setInvitations] = react.useState([]);
  const [isLoading, setIsLoading] = react.useState(true);
  const [isInviteSheetOpen, setIsInviteSheetOpen] = react.useState(false);
  const [inviteEmail, setInviteEmail] = react.useState("");
  const [inviteRole, setInviteRole] = react.useState("member");
  const [isInviting, setIsInviting] = react.useState(false);
  const [inviteError, setInviteError] = react.useState(null);
  const fetchInvitations = react.useCallback(async () => {
    setIsLoading(true);
    try {
      const result = await client.listOrganizationInvitations(organizationId);
      setInvitations(result);
    } catch {
    } finally {
      setIsLoading(false);
    }
  }, [client, organizationId]);
  react.useEffect(() => {
    fetchInvitations();
  }, [fetchInvitations]);
  const handleOpenInviteSheet = react.useCallback(() => {
    chunkUZ3CMNUJ_js.triggerHaptic("light");
    setInviteEmail("");
    setInviteRole("member");
    setInviteError(null);
    setIsInviteSheetOpen(true);
  }, []);
  const handleCloseInviteSheet = react.useCallback(() => {
    chunkUZ3CMNUJ_js.triggerHaptic("light");
    setIsInviteSheetOpen(false);
  }, []);
  const handleSendInvitation = react.useCallback(async () => {
    if (!inviteEmail.trim()) {
      setInviteError(t("admin.organizationDetail.invitations.errorEmailRequired"));
      chunkUZ3CMNUJ_js.triggerHaptic("error");
      return;
    }
    setInviteError(null);
    setIsInviting(true);
    chunkUZ3CMNUJ_js.triggerHaptic("light");
    try {
      const data = {
        email: inviteEmail.trim().toLowerCase(),
        role: inviteRole
      };
      await client.createOrganizationInvitation(organizationId, data);
      chunkUZ3CMNUJ_js.triggerHaptic("success");
      setIsInviteSheetOpen(false);
      fetchInvitations();
    } catch {
      setInviteError(t("admin.organizationDetail.invitations.errorSendFailed"));
      chunkUZ3CMNUJ_js.triggerHaptic("error");
    } finally {
      setIsInviting(false);
    }
  }, [inviteEmail, inviteRole, client, organizationId, fetchInvitations, t]);
  const statusBadgeColor = (status) => {
    if (status === "accepted") return "green";
    if (status === "pending") return "amber";
    if (status === "revoked" || status === "expired") return "red";
    return "zinc";
  };
  const roleOptions = ["viewer", "member", "admin", "owner"];
  if (isLoading) {
    return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-center gap-2 p-8 text-sm text-gray-500 dark:text-gray-400", children: [
      /* @__PURE__ */ jsxRuntime.jsx(chunkMT66VKLS_js.InlineSpinner, {}),
      /* @__PURE__ */ jsxRuntime.jsx("span", { children: t("admin.organizationDetail.invitations.loading") })
    ] });
  }
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4", children: [
      /* @__PURE__ */ jsxRuntime.jsxs(
        chunkBHOT22QL_js.Button,
        {
          color: "ios-glass-blue",
          onClick: handleOpenInviteSheet,
          fullWidth: true,
          children: [
            /* @__PURE__ */ jsxRuntime.jsx(outline.PlusIcon, { className: "mr-1.5 h-4 w-4" }),
            t("admin.organizationDetail.invitations.inviteButton")
          ]
        }
      ),
      invitations.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(
        chunkMT66VKLS_js.EmptyState,
        {
          message: t("admin.organizationDetail.invitations.emptyMessage"),
          description: t("admin.organizationDetail.invitations.emptyDescription"),
          icon: outline.EnvelopeIcon,
          variant: "card"
        }
      ) : /* @__PURE__ */ jsxRuntime.jsx(chunkMT66VKLS_js.ListCard, { children: invitations.map((invitation) => /* @__PURE__ */ jsxRuntime.jsx(
        chunkMT66VKLS_js.ListCardItem,
        {
          leading: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex h-9 w-9 items-center justify-center rounded-xl bg-gradient-to-br from-amber-500 to-orange-600 shadow-sm", children: /* @__PURE__ */ jsxRuntime.jsx(outline.EnvelopeIcon, { className: "h-5 w-5 text-white" }) }),
          trailing: /* @__PURE__ */ jsxRuntime.jsx(chunkBHOT22QL_js.Badge, { color: statusBadgeColor(invitation.status), children: invitation.status }),
          children: /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
            /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-sm font-medium text-gray-900 dark:text-white", children: invitation.email }),
            /* @__PURE__ */ jsxRuntime.jsxs("p", { className: "mt-0.5 text-xs text-gray-500 dark:text-gray-400", children: [
              t("admin.organizationDetail.invitations.roleLabel", { role: invitation.role }),
              " \xB7 ",
              format.dateTime(new Date(invitation.createdAt), {
                month: "short",
                day: "numeric"
              })
            ] })
          ] })
        },
        invitation.id
      )) })
    ] }),
    /* @__PURE__ */ jsxRuntime.jsx(
      chunkMT66VKLS_js.Sheet,
      {
        open: isInviteSheetOpen,
        onClose: handleCloseInviteSheet,
        title: t("admin.organizationDetail.invitations.sheetTitle"),
        side: "bottom",
        size: "md",
        children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4 p-4 sm:p-6", children: [
          /* @__PURE__ */ jsxRuntime.jsx(
            chunkMT66VKLS_js.Input,
            {
              label: t("admin.organizationDetail.invitations.emailLabel"),
              type: "email",
              value: inviteEmail,
              onChange: (event) => setInviteEmail(event.target.value),
              placeholder: t("admin.organizationDetail.invitations.emailPlaceholder"),
              disabled: isInviting,
              autoFocus: true
            }
          ),
          /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
            /* @__PURE__ */ jsxRuntime.jsx("label", { className: "mb-1.5 block text-sm font-medium text-gray-900 dark:text-white", children: t("admin.organizationDetail.invitations.roleSelectLabel") }),
            /* @__PURE__ */ jsxRuntime.jsx("div", { className: "grid grid-cols-4 gap-2", children: roleOptions.map((role) => /* @__PURE__ */ jsxRuntime.jsx(
              "button",
              {
                type: "button",
                onClick: () => {
                  setInviteRole(role);
                  chunkUZ3CMNUJ_js.triggerHaptic("light");
                },
                className: `rounded-xl border px-2 py-2 text-xs font-semibold backdrop-blur-xl transition ${inviteRole === role ? "border-indigo-400/80 bg-gradient-to-br from-indigo-100/85 via-white/80 to-sky-100/75 text-slate-900 dark:border-indigo-300/70 dark:bg-[linear-gradient(140deg,rgba(99,102,241,0.32)_0%,rgba(30,41,59,0.72)_100%)] dark:text-indigo-100" : "border-white/55 bg-gradient-to-br from-white/82 via-white/66 to-slate-100/62 text-slate-700 hover:from-white/92 hover:to-sky-100/72 dark:border-white/15 dark:bg-[linear-gradient(140deg,rgba(30,41,59,0.72)_0%,rgba(15,23,42,0.62)_100%)] dark:text-slate-100 dark:hover:bg-[linear-gradient(140deg,rgba(51,65,85,0.76)_0%,rgba(30,41,59,0.68)_100%)]"}`,
                children: role
              },
              role
            )) })
          ] }),
          inviteError && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-sm text-red-600 dark:text-red-400", role: "alert", children: inviteError }),
          /* @__PURE__ */ jsxRuntime.jsx(
            chunkBHOT22QL_js.Button,
            {
              color: "ios-glass-blue",
              fullWidth: true,
              onClick: handleSendInvitation,
              loading: isInviting,
              disabled: isInviting || !inviteEmail.trim(),
              children: t("admin.organizationDetail.invitations.sendButton")
            }
          )
        ] })
      }
    )
  ] });
}
function DomainsTabContent({
  organizationId,
  client
}) {
  const t = chunkYXN2K77G_js.useTranslations("windsock");
  const [domains, setDomains] = react.useState([]);
  const [isLoading, setIsLoading] = react.useState(true);
  const [isAddSheetOpen, setIsAddSheetOpen] = react.useState(false);
  const [newDomain, setNewDomain] = react.useState("");
  const [isAdding, setIsAdding] = react.useState(false);
  const [addError, setAddError] = react.useState(null);
  const [verifyingDomain, setVerifyingDomain] = react.useState(null);
  const fetchDomains = react.useCallback(async () => {
    setIsLoading(true);
    try {
      const result = await client.listOrganizationDomains(organizationId);
      setDomains(result);
    } catch {
    } finally {
      setIsLoading(false);
    }
  }, [client, organizationId]);
  react.useEffect(() => {
    fetchDomains();
  }, [fetchDomains]);
  const handleOpenAddSheet = react.useCallback(() => {
    chunkUZ3CMNUJ_js.triggerHaptic("light");
    setNewDomain("");
    setAddError(null);
    setIsAddSheetOpen(true);
  }, []);
  const handleCloseAddSheet = react.useCallback(() => {
    chunkUZ3CMNUJ_js.triggerHaptic("light");
    setIsAddSheetOpen(false);
  }, []);
  const handleAddDomain = react.useCallback(async () => {
    if (!newDomain.trim()) {
      setAddError(t("admin.organizationDetail.domains.errorDomainRequired"));
      chunkUZ3CMNUJ_js.triggerHaptic("error");
      return;
    }
    setAddError(null);
    setIsAdding(true);
    chunkUZ3CMNUJ_js.triggerHaptic("light");
    try {
      await client.addOrganizationDomain(organizationId, newDomain.trim().toLowerCase());
      chunkUZ3CMNUJ_js.triggerHaptic("success");
      setIsAddSheetOpen(false);
      fetchDomains();
    } catch {
      setAddError(t("admin.organizationDetail.domains.errorAddFailed"));
      chunkUZ3CMNUJ_js.triggerHaptic("error");
    } finally {
      setIsAdding(false);
    }
  }, [newDomain, client, organizationId, fetchDomains, t]);
  const handleVerifyDomain = react.useCallback(async (domain) => {
    setVerifyingDomain(domain);
    chunkUZ3CMNUJ_js.triggerHaptic("light");
    try {
      await client.verifyOrganizationDomain(organizationId, domain);
      chunkUZ3CMNUJ_js.triggerHaptic("success");
      fetchDomains();
    } catch {
      chunkUZ3CMNUJ_js.triggerHaptic("error");
    } finally {
      setVerifyingDomain(null);
    }
  }, [client, organizationId, fetchDomains]);
  if (isLoading) {
    return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-center gap-2 p-8 text-sm text-gray-500 dark:text-gray-400", children: [
      /* @__PURE__ */ jsxRuntime.jsx(chunkMT66VKLS_js.InlineSpinner, {}),
      /* @__PURE__ */ jsxRuntime.jsx("span", { children: t("admin.organizationDetail.domains.loading") })
    ] });
  }
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4", children: [
      /* @__PURE__ */ jsxRuntime.jsxs(
        chunkBHOT22QL_js.Button,
        {
          color: "ios-glass-blue",
          onClick: handleOpenAddSheet,
          fullWidth: true,
          children: [
            /* @__PURE__ */ jsxRuntime.jsx(outline.PlusIcon, { className: "mr-1.5 h-4 w-4" }),
            t("admin.organizationDetail.domains.addButton")
          ]
        }
      ),
      domains.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(
        chunkMT66VKLS_js.EmptyState,
        {
          message: t("admin.organizationDetail.domains.emptyMessage"),
          description: t("admin.organizationDetail.domains.emptyDescription"),
          icon: outline.GlobeAltIcon,
          variant: "card"
        }
      ) : /* @__PURE__ */ jsxRuntime.jsx(chunkMT66VKLS_js.ListCard, { children: domains.map((domain) => /* @__PURE__ */ jsxRuntime.jsx(
        chunkMT66VKLS_js.ListCardItem,
        {
          leading: /* @__PURE__ */ jsxRuntime.jsx("div", { className: `flex h-9 w-9 items-center justify-center rounded-xl shadow-sm ${domain.verified ? "bg-gradient-to-br from-emerald-500 to-green-600" : "bg-gradient-to-br from-amber-500 to-orange-600"}`, children: domain.verified ? /* @__PURE__ */ jsxRuntime.jsx(outline.CheckCircleIcon, { className: "h-5 w-5 text-white" }) : /* @__PURE__ */ jsxRuntime.jsx(outline.GlobeAltIcon, { className: "h-5 w-5 text-white" }) }),
          trailing: !domain.verified ? /* @__PURE__ */ jsxRuntime.jsxs(
            chunkBHOT22QL_js.Button,
            {
              size: "sm",
              color: "ios-glass-blue",
              onClick: () => handleVerifyDomain(domain.domain),
              loading: verifyingDomain === domain.domain,
              disabled: verifyingDomain !== null,
              children: [
                /* @__PURE__ */ jsxRuntime.jsx(outline.ArrowPathIcon, { className: "mr-1 h-3.5 w-3.5" }),
                t("admin.organizationDetail.domains.verifyButton")
              ]
            }
          ) : /* @__PURE__ */ jsxRuntime.jsx(chunkMT66VKLS_js.StatusBadge, { status: "active", label: t("admin.organizationDetail.domains.verifiedLabel"), size: "sm" }),
          children: /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
            /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-sm font-medium text-gray-900 dark:text-white", children: domain.domain }),
            domain.autoJoinEnabled && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "mt-0.5 text-xs text-gray-500 dark:text-gray-400", children: t("admin.organizationDetail.domains.autoJoinEnabled") })
          ] })
        },
        domain.id
      )) })
    ] }),
    /* @__PURE__ */ jsxRuntime.jsx(
      chunkMT66VKLS_js.Sheet,
      {
        open: isAddSheetOpen,
        onClose: handleCloseAddSheet,
        title: t("admin.organizationDetail.domains.sheetTitle"),
        side: "bottom",
        size: "md",
        children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4 p-4 sm:p-6", children: [
          /* @__PURE__ */ jsxRuntime.jsx(
            chunkMT66VKLS_js.Input,
            {
              label: t("admin.organizationDetail.domains.domainLabel"),
              value: newDomain,
              onChange: (event) => setNewDomain(event.target.value),
              placeholder: t("admin.organizationDetail.domains.domainPlaceholder"),
              disabled: isAdding,
              autoFocus: true
            }
          ),
          addError && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-sm text-red-600 dark:text-red-400", role: "alert", children: addError }),
          /* @__PURE__ */ jsxRuntime.jsx(
            chunkBHOT22QL_js.Button,
            {
              color: "ios-glass-blue",
              fullWidth: true,
              onClick: handleAddDomain,
              loading: isAdding,
              disabled: isAdding || !newDomain.trim(),
              children: t("admin.organizationDetail.domains.addDomainButton")
            }
          )
        ] })
      }
    )
  ] });
}
function AdminOrganizationDetail({
  organization,
  client
}) {
  const t = chunkYXN2K77G_js.useTranslations("windsock");
  const [activeTab, setActiveTab] = react.useState("members");
  const segments = ALL_TABS.map((tab) => ({
    value: tab,
    label: t(`admin.organizationDetail.tabs.${tab}`)
  }));
  return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "space-y-6", children: /* @__PURE__ */ jsxRuntime.jsx(
    chunkMT66VKLS_js.SectionCard,
    {
      header: {
        icon: /* @__PURE__ */ jsxRuntime.jsx(outline.UsersIcon, { className: "h-5 w-5 text-white" }),
        title: organization.displayName ?? organization.name,
        subtitle: organization.description ?? t("admin.organizationDetail.defaultSubtitle"),
        gradient: "from-emerald-500 via-teal-500 to-cyan-500",
        rightContent: /* @__PURE__ */ jsxRuntime.jsx(
          chunkMT66VKLS_js.StatusBadge,
          {
            status: organization.enabled ? "active" : "inactive",
            label: organization.enabled ? t("admin.organizationDetail.statusEnabled") : t("admin.organizationDetail.statusDisabled"),
            size: "sm"
          }
        )
      },
      children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4", children: [
        /* @__PURE__ */ jsxRuntime.jsx(
          chunkMT66VKLS_js.SegmentedControl,
          {
            segments,
            value: activeTab,
            onChange: (value) => setActiveTab(value),
            fullWidth: true
          }
        ),
        activeTab === "members" && /* @__PURE__ */ jsxRuntime.jsx(MembersTabContent, { organizationId: organization.id, client }),
        activeTab === "invitations" && /* @__PURE__ */ jsxRuntime.jsx(InvitationsTabContent, { organizationId: organization.id, client }),
        activeTab === "domains" && /* @__PURE__ */ jsxRuntime.jsx(DomainsTabContent, { organizationId: organization.id, client })
      ] })
    }
  ) });
}

exports.AdminOrganizationDetail = AdminOrganizationDetail;
//# sourceMappingURL=chunk-T4IYOKHR.js.map
//# sourceMappingURL=chunk-T4IYOKHR.js.map