import { jsx as e, jsxs as s, Fragment as P } from "react/jsx-runtime";
import { useState as d, useEffect as G } from "react";
import { useTranslation as F } from "react-i18next";
import { k as E, D as T } from "./index-CwSaRXXg.js";
import { A as B } from "./AccessRequestStatusBadge-CjkJPKuE.js";
import { X as D, Users as H, Shield as X, Loader2 as q, CheckCircle as z, XCircle as K } from "lucide-react";
import { a as w } from "./accessRequestsApi-ZXFPCid2.js";
function U({
  open: u,
  onClose: h,
  onConfirm: m,
  request: o,
  action: r,
  processing: i
}) {
  const { t: a } = F("admin"), [n, p] = d(""), [l, v] = d("group"), [g, f] = d(""), [x, N] = d(""), [t, y] = d([]), [A, k] = d([]), [C, L] = d(!1), [I, S] = d(!1), R = r === "deny", b = r === "approve";
  G(() => {
    !u || !b || (S(!0), E.tenants.getGroups(o.tenantId).then((c) => {
      k(c.filter((M) => M.isActive));
    }).catch(() => {
      k([]);
    }).finally(() => {
      S(!1);
    }));
  }, [u, b, o.tenantId]), G(() => {
    !u || !b || l !== "role" || t.length > 0 || (L(!0), E.roles.getByApplication(o.applicationId).then((c) => {
      y(c);
    }).catch(() => {
      y([]);
    }).finally(() => {
      L(!1);
    }));
  }, [u, b, l, o.applicationId, t.length]), G(() => {
    u || (p(""), v("group"), f(""), N(""), y([]), k([]));
  }, [u]);
  const $ = async () => {
    if (R) {
      await m({ notes: n || void 0 });
      return;
    }
    await m({
      roleId: l === "role" ? g : void 0,
      groupId: l === "group" ? x : void 0,
      notes: n || void 0
    });
  };
  if (!u) return null;
  const j = b ? (l === "role" && g !== "" || l === "group" && x !== "") && !i : n !== "" && !i;
  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: h,
          className: "p-1 hover:bg-[var(--bg-hover)] rounded",
          disabled: i,
          children: /* @__PURE__ */ e(D, { 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: () => v("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(H, { className: "w-4 h-4" }),
                  a("accessRequests.modal.addToGroup")
                ]
              }
            ),
            /* @__PURE__ */ s(
              "button",
              {
                type: "button",
                onClick: () => v("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(X, { 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) => p(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: h,
            className: "px-4 py-2 rounded-[var(--radius-button)] border border-[var(--border-color)] hover:bg-[var(--bg-hover)]",
            disabled: i,
            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: [
              i && /* @__PURE__ */ e(q, { className: "w-4 h-4 animate-spin" }),
              a(`accessRequests.actions.${r}`)
            ]
          }
        )
      ] })
    ] })
  ] }) });
}
function J(u) {
  const [h, m] = d(!1), [o, r] = d(null);
  return { approve: async (n, p) => {
    m(!0), r(null);
    try {
      await w.approveRequest(n, p), u?.();
    } catch (l) {
      throw r(l instanceof Error ? l.message : "Failed to approve request"), l;
    } finally {
      m(!1);
    }
  }, deny: async (n, p) => {
    m(!0), r(null);
    try {
      await w.denyRequest(n, p), u?.();
    } catch (l) {
      throw r(l instanceof Error ? l.message : "Failed to deny request"), l;
    } finally {
      m(!1);
    }
  }, processing: h, error: o };
}
function ee({ requests: u, loading: h, onReload: m, showTenantColumn: o = !1 }) {
  const { t: r } = F("admin"), [i, a] = d(null), [n, p] = d(null), { approve: l, deny: v, processing: g } = J(() => {
    a(null), p(null), m();
  }), f = (t, y) => {
    a(t), p(y);
  }, x = async (t) => {
    i && (n === "approve" ? await l(i.id, t) : n === "deny" && t.notes && await v(i.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(B, { 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(z, { 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(K, { className: "w-4 h-4" })
          }
        )
      ] }) : /* @__PURE__ */ e("div", { className: "text-sm text-[var(--text-secondary)]", children: t.processedBy })
    }
  ];
  return h ? /* @__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(
      T,
      {
        data: u,
        columns: N,
        searchable: !0,
        searchPlaceholder: r("accessRequests.search"),
        getRowKey: (t) => t.id,
        emptyMessage: r("accessRequests.empty")
      }
    ),
    i && n && /* @__PURE__ */ e(
      U,
      {
        open: !0,
        onClose: () => {
          a(null), p(null);
        },
        onConfirm: x,
        request: i,
        action: n,
        processing: g
      }
    )
  ] });
}
function se(u, h) {
  const [m, o] = d([]), [r, i] = d(!1), [a, n] = d(null);
  return { requests: m, loading: r, error: a, loadRequests: async (l) => {
    i(!0), n(null);
    try {
      const v = u === "application" ? await w.getRequestsByApplication(h, l) : await w.getRequestsByTenant(h, l);
      o(Array.isArray(v) ? v : []);
    } catch (v) {
      n(v.message || "Failed to load requests"), o([]);
    } finally {
      i(!1);
    }
  } };
}
export {
  ee as A,
  se as u
};
//# sourceMappingURL=useAccessRequests-B9bF4swg.js.map
