import { jsx as e, jsxs as s, Fragment as P } from "react/jsx-runtime";
import { useState as u, useEffect as G, useCallback as T } from "react";
import { useTranslation as F } from "react-i18next";
import { v as S, E as B } from "./index-CeEMms9z.js";
import { A as D } from "./AccessRequestStatusBadge-CjkJPKuE.js";
import { X as H, Users as X, Shield as z, Loader2 as q, CheckCircle as K, XCircle as U } from "lucide-react";
import { a as w } from "./accessRequestsApi-DH7p6AR9.js";
function J({
  open: i,
  onClose: v,
  onConfirm: p,
  request: o,
  action: r,
  processing: d
}) {
  const { t: a } = F("admin"), [n, m] = u(""), [l, h] = u("group"), [g, f] = u(""), [x, N] = u(""), [t, y] = u([]), [A, k] = u([]), [C, E] = u(!1), [I, L] = u(!1), R = r === "deny", b = r === "approve";
  G(() => {
    !i || !b || (L(!0), S.tenants.getGroups(o.tenantId).then((c) => {
      k(c.filter((M) => M.isActive));
    }).catch(() => {
      k([]);
    }).finally(() => {
      L(!1);
    }));
  }, [i, b, o.tenantId]), G(() => {
    !i || !b || l !== "role" || t.length > 0 || (E(!0), S.roles.getByApplication(o.applicationId).then((c) => {
      y(c);
    }).catch(() => {
      y([]);
    }).finally(() => {
      E(!1);
    }));
  }, [i, b, l, o.applicationId, t.length]), G(() => {
    i || (m(""), h("group"), f(""), N(""), y([]), k([]));
  }, [i]);
  const $ = async () => {
    if (R) {
      await p({ notes: n || void 0 });
      return;
    }
    await p({
      roleId: l === "role" ? g : void 0,
      groupId: l === "group" ? x : void 0,
      notes: n || void 0
    });
  };
  if (!i) return null;
  const j = b ? (l === "role" && g !== "" || l === "group" && x !== "") && !d : n !== "" && !d;
  return /* @__PURE__ */ e("div", { className: "fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm", children: /* @__PURE__ */ s("div", { className: "bg-[var(--bg-primary)] rounded-[var(--radius-card)] p-6 max-w-md w-full max-h-[90vh] overflow-y-auto", children: [
    /* @__PURE__ */ s("div", { className: "flex items-center justify-between mb-4", children: [
      /* @__PURE__ */ e("h3", { className: "text-lg font-semibold", children: a(`accessRequests.modal.${r}Title`) }),
      /* @__PURE__ */ e(
        "button",
        {
          onClick: v,
          className: "p-1 hover:bg-[var(--bg-hover)] rounded",
          disabled: d,
          children: /* @__PURE__ */ e(H, { className: "w-5 h-5" })
        }
      )
    ] }),
    /* @__PURE__ */ s("div", { className: "space-y-4", children: [
      /* @__PURE__ */ s("div", { children: [
        /* @__PURE__ */ e("div", { className: "font-medium mb-1", children: a("accessRequests.modal.user") }),
        /* @__PURE__ */ s("div", { children: [
          o.userFullName,
          " (",
          o.userEmail,
          ")"
        ] })
      ] }),
      /* @__PURE__ */ s("div", { children: [
        /* @__PURE__ */ e("div", { className: "font-medium mb-1", children: a("accessRequests.modal.application") }),
        /* @__PURE__ */ e("div", { children: o.applicationLabel })
      ] }),
      o.reason && /* @__PURE__ */ s("div", { children: [
        /* @__PURE__ */ e("div", { className: "font-medium mb-1", children: a("accessRequests.modal.userReason") }),
        /* @__PURE__ */ e("div", { className: "text-sm text-[var(--text-secondary)]", children: o.reason })
      ] }),
      b && /* @__PURE__ */ s(P, { children: [
        /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ s("label", { className: "block font-medium mb-2", children: [
            a("accessRequests.modal.assignmentMethod"),
            /* @__PURE__ */ e("span", { className: "text-[var(--error-text)] ml-1", children: "*" })
          ] }),
          /* @__PURE__ */ s("div", { className: "grid grid-cols-2 gap-2", children: [
            /* @__PURE__ */ s(
              "button",
              {
                type: "button",
                onClick: () => h("group"),
                className: `flex items-center gap-2 px-3 py-2.5 rounded-[var(--radius-input)] border text-sm font-medium transition-colors ${l === "group" ? "border-[var(--color-accent-500)] bg-[var(--color-accent-50)] text-[var(--color-accent-700)]" : "border-[var(--border-color)] hover:bg-[var(--bg-hover)]"}`,
                children: [
                  /* @__PURE__ */ e(X, { className: "w-4 h-4" }),
                  a("accessRequests.modal.addToGroup")
                ]
              }
            ),
            /* @__PURE__ */ s(
              "button",
              {
                type: "button",
                onClick: () => h("role"),
                className: `flex items-center gap-2 px-3 py-2.5 rounded-[var(--radius-input)] border text-sm font-medium transition-colors ${l === "role" ? "border-[var(--color-accent-500)] bg-[var(--color-accent-50)] text-[var(--color-accent-700)]" : "border-[var(--border-color)] hover:bg-[var(--bg-hover)]"}`,
                children: [
                  /* @__PURE__ */ e(z, { className: "w-4 h-4" }),
                  a("accessRequests.modal.assignRole")
                ]
              }
            )
          ] }),
          /* @__PURE__ */ e("p", { className: "text-xs text-[var(--text-secondary)] mt-1.5", children: a(l === "group" ? "accessRequests.modal.groupHint" : "accessRequests.modal.roleHint") })
        ] }),
        l === "group" && /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ s("label", { className: "block font-medium mb-2", children: [
            a("accessRequests.modal.selectGroup"),
            /* @__PURE__ */ e("span", { className: "text-[var(--error-text)] ml-1", children: "*" })
          ] }),
          I && /* @__PURE__ */ s("div", { className: "flex items-center gap-2 text-sm text-[var(--text-secondary)]", children: [
            /* @__PURE__ */ e(q, { className: "w-4 h-4 animate-spin" }),
            a("accessRequests.modal.loadingGroups")
          ] }),
          !I && A.length === 0 && /* @__PURE__ */ e("div", { className: "text-sm text-[var(--warning-text)]", children: a("accessRequests.modal.noGroupsAvailable") }),
          !I && A.length > 0 && /* @__PURE__ */ s(
            "select",
            {
              value: x,
              onChange: (c) => N(c.target.value),
              className: "w-full px-3 py-2 border rounded-[var(--radius-input)] bg-[var(--bg-primary)] border-[var(--border-color)]",
              children: [
                /* @__PURE__ */ e("option", { value: "", children: a("accessRequests.modal.selectGroupPlaceholder") }),
                A.map((c) => /* @__PURE__ */ s("option", { value: c.groupId, children: [
                  c.name,
                  " (",
                  c.memberCount,
                  " ",
                  a("accessRequests.modal.members"),
                  ")"
                ] }, c.groupId))
              ]
            }
          )
        ] }),
        l === "role" && /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ s("label", { className: "block font-medium mb-2", children: [
            a("accessRequests.modal.selectRole"),
            /* @__PURE__ */ e("span", { className: "text-[var(--error-text)] ml-1", children: "*" })
          ] }),
          C && /* @__PURE__ */ s("div", { className: "flex items-center gap-2 text-sm text-[var(--text-secondary)]", children: [
            /* @__PURE__ */ e(q, { className: "w-4 h-4 animate-spin" }),
            a("accessRequests.modal.loadingRoles")
          ] }),
          !C && t.length === 0 && /* @__PURE__ */ e("div", { className: "text-sm text-[var(--warning-text)]", children: a("accessRequests.modal.noRolesAvailable") }),
          !C && t.length > 0 && /* @__PURE__ */ s(
            "select",
            {
              value: g,
              onChange: (c) => f(c.target.value),
              className: "w-full px-3 py-2 border rounded-[var(--radius-input)] bg-[var(--bg-primary)] border-[var(--border-color)]",
              children: [
                /* @__PURE__ */ e("option", { value: "", children: a("accessRequests.modal.selectRolePlaceholder") }),
                t.map((c) => /* @__PURE__ */ s("option", { value: c.id, children: [
                  c.name,
                  " (",
                  c.shortName,
                  ")"
                ] }, c.id))
              ]
            }
          )
        ] })
      ] }),
      /* @__PURE__ */ s("div", { children: [
        /* @__PURE__ */ s("label", { className: "block font-medium mb-2", children: [
          a(`accessRequests.modal.${r}Notes`),
          R && /* @__PURE__ */ e("span", { className: "text-[var(--error-text)] ml-1", children: "*" })
        ] }),
        /* @__PURE__ */ e(
          "textarea",
          {
            value: n,
            onChange: (c) => m(c.target.value),
            className: "w-full px-3 py-2 border rounded-[var(--radius-input)] bg-[var(--bg-primary)] border-[var(--border-color)]",
            rows: 3,
            placeholder: a(`accessRequests.modal.${r}NotesPlaceholder`),
            required: R
          }
        )
      ] }),
      /* @__PURE__ */ s("div", { className: "flex justify-end gap-2 pt-2", children: [
        /* @__PURE__ */ e(
          "button",
          {
            onClick: v,
            className: "px-4 py-2 rounded-[var(--radius-button)] border border-[var(--border-color)] hover:bg-[var(--bg-hover)]",
            disabled: d,
            children: a("common:cancel")
          }
        ),
        /* @__PURE__ */ s(
          "button",
          {
            onClick: $,
            disabled: !j,
            className: `px-4 py-2 rounded-[var(--radius-button)] text-white disabled:opacity-50 flex items-center gap-2 ${R ? "bg-[var(--error-text)]" : "bg-[var(--success-text)]"}`,
            children: [
              d && /* @__PURE__ */ e(q, { className: "w-4 h-4 animate-spin" }),
              a(`accessRequests.actions.${r}`)
            ]
          }
        )
      ] })
    ] })
  ] }) });
}
function O(i) {
  const [v, p] = u(!1), [o, r] = u(null);
  return { approve: async (n, m) => {
    p(!0), r(null);
    try {
      await w.approveRequest(n, m), i?.();
    } catch (l) {
      throw r(l instanceof Error ? l.message : "Failed to approve request"), l;
    } finally {
      p(!1);
    }
  }, deny: async (n, m) => {
    p(!0), r(null);
    try {
      await w.denyRequest(n, m), i?.();
    } catch (l) {
      throw r(l instanceof Error ? l.message : "Failed to deny request"), l;
    } finally {
      p(!1);
    }
  }, processing: v, error: o };
}
function se({ requests: i, loading: v, onReload: p, showTenantColumn: o = !1 }) {
  const { t: r } = F("admin"), [d, a] = u(null), [n, m] = u(null), { approve: l, deny: h, processing: g } = O(() => {
    a(null), m(null), p();
  }), f = (t, y) => {
    a(t), m(y);
  }, x = async (t) => {
    d && (n === "approve" ? await l(d.id, t) : n === "deny" && t.notes && await h(d.id, t.notes));
  }, N = [
    {
      key: "userFullName",
      label: r("accessRequests.table.user"),
      render: (t) => /* @__PURE__ */ s("div", { children: [
        /* @__PURE__ */ e("div", { className: "font-medium", children: t.userFullName }),
        /* @__PURE__ */ e("div", { className: "text-sm text-[var(--text-secondary)]", children: t.userEmail })
      ] })
    },
    ...o ? [{
      key: "tenantName",
      label: r("accessRequests.table.tenant"),
      render: (t) => t.tenantName
    }] : [],
    {
      key: "applicationLabel",
      label: r("accessRequests.table.application")
    },
    {
      key: "reason",
      label: r("accessRequests.table.reason"),
      render: (t) => t.reason || "-"
    },
    {
      key: "requestedAt",
      label: r("accessRequests.table.requestedAt"),
      render: (t) => new Date(t.requestedAt).toLocaleString()
    },
    {
      key: "status",
      label: r("accessRequests.table.status"),
      render: (t) => /* @__PURE__ */ e(D, { status: t.status })
    },
    {
      key: "actions",
      label: r("common:actionsColumn"),
      align: "right",
      render: (t) => t.status === "Pending" ? /* @__PURE__ */ s("div", { className: "flex gap-2", children: [
        /* @__PURE__ */ e(
          "button",
          {
            onClick: () => f(t, "approve"),
            className: "p-1 rounded hover:bg-[var(--success-bg)] text-[var(--success-text)]",
            title: r("accessRequests.actions.approve"),
            children: /* @__PURE__ */ e(K, { className: "w-4 h-4" })
          }
        ),
        /* @__PURE__ */ e(
          "button",
          {
            onClick: () => f(t, "deny"),
            className: "p-1 rounded hover:bg-[var(--error-bg)] text-[var(--error-text)]",
            title: r("accessRequests.actions.deny"),
            children: /* @__PURE__ */ e(U, { className: "w-4 h-4" })
          }
        )
      ] }) : /* @__PURE__ */ e("div", { className: "text-sm text-[var(--text-secondary)]", children: t.processedBy })
    }
  ];
  return v ? /* @__PURE__ */ e("div", { className: "flex items-center justify-center py-8", children: /* @__PURE__ */ e(q, { className: "w-6 h-6 animate-spin text-[var(--color-accent-500)]" }) }) : /* @__PURE__ */ s("div", { className: "space-y-4", children: [
    /* @__PURE__ */ e(
      B,
      {
        data: i,
        columns: N,
        searchable: !0,
        searchPlaceholder: r("accessRequests.search"),
        getRowKey: (t) => t.id,
        emptyMessage: r("accessRequests.empty")
      }
    ),
    d && n && /* @__PURE__ */ e(
      J,
      {
        open: !0,
        onClose: () => {
          a(null), m(null);
        },
        onConfirm: x,
        request: d,
        action: n,
        processing: g
      }
    )
  ] });
}
function ae(i, v) {
  const [p, o] = u([]), [r, d] = u(!1), [a, n] = u(null), m = T(async (l) => {
    d(!0), n(null);
    try {
      const h = i === "application" ? await w.getRequestsByApplication(v, l) : await w.getRequestsByTenant(v, l);
      o(Array.isArray(h) ? h : []);
    } catch (h) {
      n(h.message || "Failed to load requests"), o([]);
    } finally {
      d(!1);
    }
  }, [i, v]);
  return { requests: p, loading: r, error: a, loadRequests: m };
}
export {
  se as A,
  ae as u
};
//# sourceMappingURL=useAccessRequests-BE1aKgyt.js.map
