import { jsxs as t, jsx as e } from "react/jsx-runtime";
import { useState as x, useCallback as y, memo as P } from "react";
import { useTranslation as w } from "react-i18next";
import { Clock as k, RefreshCw as C, LogIn as j, X as L, Loader2 as F, Star as A, Box as N, Lock as S, ArrowRight as T, FileText as D, Briefcase as I, LifeBuoy as B } from "lucide-react";
import { Link as M } from "react-router-dom";
import { e as U, h as _, f as $ } from "./index-Dh9kUeDn.js";
import { a as z } from "./accessRequestsApi-oahQ9OgP.js";
import { u as J } from "./useUserAccessRequests-CQcWx4x2.js";
function O({ applicationId: i, tenantId: a, existingStatus: o, onSuccess: m }) {
  const { t: n } = w("admin"), [c, g] = x(!1), [l, r] = x(!1), [d, v] = x(""), [b, h] = x(!1), f = async () => {
    g(!0);
    try {
      await z.createRequest(a, i, {
        applicationId: i,
        reason: d || void 0
      }), r(!1), v(""), h(!0), m?.();
    } catch (s) {
      console.error("Failed to create access request:", s);
    } finally {
      g(!1);
    }
  };
  return b || o === "Pending" ? /* @__PURE__ */ t("div", { className: "inline-flex items-center gap-2 px-4 py-2 rounded-[var(--radius-button)] bg-amber-500/10 text-amber-600 border border-amber-500/30 cursor-default", children: [
    /* @__PURE__ */ e(k, { className: "w-4 h-4" }),
    n("accessRequests.pendingLabel", "Request pending")
  ] }) : /* @__PURE__ */ t("div", { children: [
    /* @__PURE__ */ t(
      "button",
      {
        onClick: () => r(!0),
        className: "inline-flex items-center gap-2 px-4 py-2 rounded-[var(--radius-button)] bg-[var(--color-accent-600)] text-white hover:bg-[var(--color-accent-700)]",
        children: [
          o === "Denied" ? /* @__PURE__ */ e(C, { className: "w-4 h-4" }) : /* @__PURE__ */ e(j, { className: "w-4 h-4" }),
          o === "Denied" ? n("accessRequests.retryAccess", "Retry request") : n("accessRequests.requestAccess")
        ]
      }
    ),
    l && /* @__PURE__ */ e("div", { className: "fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm", children: /* @__PURE__ */ t("div", { className: "bg-[var(--bg-primary)] rounded-[var(--radius-card)] p-6 max-w-md w-full", children: [
      /* @__PURE__ */ t("div", { className: "flex items-center justify-between mb-4", children: [
        /* @__PURE__ */ e("h3", { className: "text-lg font-semibold", children: n("accessRequests.modal.requestTitle") }),
        /* @__PURE__ */ e(
          "button",
          {
            onClick: () => r(!1),
            className: "p-1 hover:bg-[var(--bg-hover)] rounded",
            disabled: c,
            children: /* @__PURE__ */ e(L, { className: "w-5 h-5" })
          }
        )
      ] }),
      /* @__PURE__ */ t("div", { className: "space-y-4", children: [
        /* @__PURE__ */ t("div", { children: [
          /* @__PURE__ */ e("label", { className: "block font-medium mb-2", children: n("accessRequests.modal.reasonLabel") }),
          /* @__PURE__ */ e(
            "textarea",
            {
              value: d,
              onChange: (s) => v(s.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: n("accessRequests.modal.reasonPlaceholder")
            }
          )
        ] }),
        /* @__PURE__ */ t("div", { className: "flex justify-end gap-2", children: [
          /* @__PURE__ */ e(
            "button",
            {
              onClick: () => r(!1),
              className: "px-4 py-2 rounded-[var(--radius-button)] border border-[var(--border-color)] hover:bg-[var(--bg-hover)]",
              disabled: c,
              children: n("common:cancel")
            }
          ),
          /* @__PURE__ */ t(
            "button",
            {
              onClick: f,
              disabled: c,
              className: "px-4 py-2 rounded-[var(--radius-button)] bg-[var(--color-accent-600)] text-white hover:bg-[var(--color-accent-700)] disabled:opacity-50 flex items-center gap-2",
              children: [
                c && /* @__PURE__ */ e(F, { className: "w-4 h-4 animate-spin" }),
                n("common:submit")
              ]
            }
          )
        ] })
      ] })
    ] }) })
  ] });
}
const X = {
  lifebuoy: B,
  briefcase: I,
  file: D,
  box: N
};
function Y(i) {
  return i ? X[i.toLowerCase()] || N : N;
}
const R = P(function({ app: a, isFavorite: o, onToggleFavorite: m, buildUrl: n, tenantId: c, existingRequestStatus: g, onAccessRequested: l }) {
  const { t: r } = w("navigation"), d = Y(a.icon), v = a.accessLevel === "RequestAccess" && a.modules.length === 0, b = y((h) => {
    h.preventDefault(), h.stopPropagation(), m();
  }, [m]);
  return v ? /* @__PURE__ */ e("div", { className: "group relative bg-[var(--bg-card)] rounded-[var(--radius-card)] border border-[var(--border-color)] overflow-hidden hover:border-[var(--warning-border)] transition-all duration-300 hover:shadow-lg", children: /* @__PURE__ */ t("div", { className: "block p-6", children: [
    /* @__PURE__ */ t("div", { className: "flex items-start justify-between mb-4", children: [
      /* @__PURE__ */ e("div", { className: "w-14 h-14 rounded-xl bg-[var(--warning-bg)] flex items-center justify-center", children: /* @__PURE__ */ e(d, { className: "w-7 h-7 text-[var(--warning-text)]" }) }),
      /* @__PURE__ */ e("div", { className: "p-2 rounded-lg bg-[var(--warning-bg)] text-[var(--warning-text)]", children: /* @__PURE__ */ e(S, { className: "w-5 h-5" }) })
    ] }),
    /* @__PURE__ */ e("h3", { className: "text-lg font-semibold text-[var(--text-primary)] mb-2", children: a.label }),
    /* @__PURE__ */ e("p", { className: "text-sm text-[var(--warning-text)] opacity-80 mb-4", children: r("applicationsPage.requestAccessDesc", "Restricted access - request required") }),
    c && /* @__PURE__ */ e(
      O,
      {
        applicationId: a.id,
        tenantId: c,
        existingStatus: g,
        onSuccess: l
      }
    )
  ] }) }) : /* @__PURE__ */ e("div", { className: "group relative bg-[var(--bg-card)] rounded-[var(--radius-card)] border border-[var(--border-color)] overflow-hidden hover:border-[var(--color-accent-400)] transition-all duration-300 hover:shadow-lg", children: /* @__PURE__ */ t(
    M,
    {
      to: a.route ? n(a.route) : "#",
      className: "block p-6",
      children: [
        /* @__PURE__ */ t("div", { className: "flex items-start justify-between mb-4", children: [
          /* @__PURE__ */ e("div", { className: "w-14 h-14 rounded-xl bg-[var(--accent-bg)] flex items-center justify-center group-hover:opacity-90 transition-all", children: /* @__PURE__ */ e(d, { className: "w-7 h-7 text-[var(--color-accent-500)]" }) }),
          /* @__PURE__ */ e(
            "button",
            {
              onClick: b,
              className: `p-2 rounded-lg transition-all duration-200 ${o ? "text-[var(--warning-text)] bg-[var(--warning-bg)]" : "text-[var(--text-tertiary)] opacity-0 group-hover:opacity-100 hover:bg-[var(--bg-tertiary)]"}`,
              title: o ? r("applicationsPage.removeFavorite", "Remove from favorites") : r("applicationsPage.addFavorite", "Add to favorites"),
              children: /* @__PURE__ */ e(A, { className: `w-5 h-5 ${o ? "fill-current" : ""}` })
            }
          )
        ] }),
        /* @__PURE__ */ e("h3", { className: "text-lg font-semibold text-[var(--text-primary)] mb-2", children: a.label }),
        /* @__PURE__ */ t("p", { className: "text-sm text-[var(--text-secondary)] mb-4", children: [
          a.modules.length,
          " ",
          a.modules.length === 1 ? r("applicationsPage.moduleCount_one", "module") : r("applicationsPage.moduleCount_other", "modules")
        ] }),
        /* @__PURE__ */ t("div", { className: "flex items-center gap-2 text-[var(--color-accent-500)] text-sm font-medium opacity-0 group-hover:opacity-100 transition-opacity", children: [
          r("applicationsPage.open", "Open"),
          /* @__PURE__ */ e(T, { className: "w-4 h-4" })
        ] })
      ]
    }
  ) });
});
function te() {
  const { t: i } = w("navigation"), { favorites: a, menu: o, toggleFavorite: m, isFavorite: n, isLoading: c, refreshMenu: g } = U(), { currentTenant: l } = _(), r = $(), { requests: d, reload: v } = J(), b = o?.applications || [], h = y((s) => {
    const u = d.filter((p) => p.applicationId === s && p.tenantId === l?.id);
    return u.find((p) => p.status === "Pending") ? "Pending" : u.find((p) => p.status === "Denied") ? "Denied" : null;
  }, [d, l?.id]), f = y(() => {
    g(), v();
  }, [g, v]);
  return c ? /* @__PURE__ */ e("div", { className: "flex items-center justify-center min-h-[400px]", children: /* @__PURE__ */ e("div", { className: "w-8 h-8 border-2 border-[var(--color-accent-500)] border-t-transparent rounded-full animate-spin" }) }) : /* @__PURE__ */ t("div", { className: "space-y-8", children: [
    /* @__PURE__ */ t("div", { children: [
      /* @__PURE__ */ e("h1", { className: "text-2xl font-bold text-[var(--text-primary)]", children: i("applicationsPage.title", "Applications") }),
      /* @__PURE__ */ e("p", { className: "text-[var(--text-secondary)] mt-1", children: i("applicationsPage.subtitle", "Your available applications") })
    ] }),
    a.length > 0 && /* @__PURE__ */ t("section", { children: [
      /* @__PURE__ */ t("h2", { className: "text-lg font-semibold text-[var(--text-primary)] mb-4 flex items-center gap-2", children: [
        /* @__PURE__ */ e(A, { className: "w-5 h-5 text-[var(--warning-text)] fill-[var(--warning-text)]" }),
        i("applicationsPage.favorites", "Favorites")
      ] }),
      /* @__PURE__ */ e("div", { className: "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4", children: a.map((s) => {
        const u = b.find((q) => q.id === s.id);
        return u ? /* @__PURE__ */ e(
          R,
          {
            app: u,
            isFavorite: !0,
            onToggleFavorite: () => m(u.id),
            buildUrl: r,
            tenantId: l?.id,
            existingRequestStatus: h(u.id),
            onAccessRequested: f
          },
          u.id
        ) : null;
      }) })
    ] }),
    /* @__PURE__ */ t("section", { children: [
      /* @__PURE__ */ e("h2", { className: "text-lg font-semibold text-[var(--text-primary)] mb-4", children: i("applicationsPage.all", "All applications") }),
      b.length > 0 ? /* @__PURE__ */ e("div", { className: "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4", children: b.map((s) => /* @__PURE__ */ e(
        R,
        {
          app: s,
          isFavorite: n(s.id),
          onToggleFavorite: () => m(s.id),
          buildUrl: r,
          tenantId: l?.id,
          existingRequestStatus: h(s.id),
          onAccessRequested: f
        },
        s.id
      )) }) : /* @__PURE__ */ t("div", { className: "text-center py-12 bg-[var(--bg-card)] rounded-[var(--radius-card)] border border-[var(--border-color)]", children: [
        /* @__PURE__ */ e(N, { className: "w-12 h-12 text-[var(--text-tertiary)] mx-auto mb-4" }),
        /* @__PURE__ */ e("p", { className: "text-[var(--text-secondary)]", children: i("applicationsPage.noApps", "No applications available") })
      ] })
    ] })
  ] });
}
export {
  te as ApplicationsPage
};
//# sourceMappingURL=ApplicationsPage-B1IZvwmo.js.map
