import { jsxs as t, jsx as e } from "react/jsx-runtime";
import { useState as x, useCallback as w, memo as k } from "react";
import { useTranslation as y } from "react-i18next";
import { Clock as C, RefreshCw as L, LogIn as j, X as F, Loader2 as S, Star as P, Box as N, LayoutGrid as T, Lock as D, ArrowRight as I, FileText as B, Briefcase as M, LifeBuoy as U } from "lucide-react";
import { Link as _ } from "react-router-dom";
import { u as $, a as z, b as G, P as J } from "./index-CeEMms9z.js";
import { a as O } from "./accessRequestsApi-DH7p6AR9.js";
import { u as X } from "./useUserAccessRequests-BMfMQ9H6.js";
function Y({ applicationId: i, tenantId: a, existingStatus: o, onSuccess: m }) {
  const { t: n } = y("admin"), [c, g] = x(!1), [l, r] = x(!1), [b, v] = x(""), [d, p] = x(!1), f = async () => {
    g(!0);
    try {
      await O.createRequest(a, i, {
        applicationId: i,
        reason: b || void 0
      }), r(!1), v(""), p(!0), m?.();
    } catch (s) {
      console.error("Failed to create access request:", s);
    } finally {
      g(!1);
    }
  };
  return d || 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(C, { 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(L, { 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(F, { 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: b,
              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(S, { className: "w-4 h-4 animate-spin" }),
                n("common:submit")
              ]
            }
          )
        ] })
      ] })
    ] }) })
  ] });
}
const E = {
  lifebuoy: U,
  briefcase: M,
  file: B,
  box: N
};
function R(i, a) {
  const o = i ? E[i.toLowerCase()] || N : N;
  return /* @__PURE__ */ e(o, { className: a });
}
const A = k(function({ app: a, isFavorite: o, onToggleFavorite: m, buildUrl: n, tenantId: c, existingRequestStatus: g, onAccessRequested: l }) {
  const { t: r } = y("navigation"), b = a.accessLevel === "RequestAccess" && a.modules.length === 0, v = w((d) => {
    d.preventDefault(), d.stopPropagation(), m();
  }, [m]);
  return b ? /* @__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: R(a.icon, "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(D, { 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(
      Y,
      {
        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(
    _,
    {
      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: R(a.icon, "w-7 h-7 text-[var(--color-accent-500)]") }),
          /* @__PURE__ */ e(
            "button",
            {
              onClick: v,
              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(P, { 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(I, { className: "w-4 h-4" })
        ] })
      ]
    }
  ) });
});
function re() {
  const { t: i } = y("navigation"), { favorites: a, menu: o, toggleFavorite: m, isFavorite: n, isLoading: c, refreshMenu: g } = $(), { currentTenant: l } = z(), r = G(), { requests: b, reload: v } = X(), d = o?.applications || [], p = w((s) => {
    const u = b.filter((h) => h.applicationId === s && h.tenantId === l?.id);
    return u.find((h) => h.status === "Pending") ? "Pending" : u.find((h) => h.status === "Denied") ? "Denied" : null;
  }, [b, l?.id]), f = w(() => {
    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(
    J,
    {
      title: i("applicationsPage.title", "Applications"),
      subtitle: i("applicationsPage.subtitle", "Your available applications"),
      icon: /* @__PURE__ */ e(T, { className: "w-6 h-6" }),
      children: [
        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(P, { 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 = d.find((q) => q.id === s.id);
            return u ? /* @__PURE__ */ e(
              A,
              {
                app: u,
                isFavorite: !0,
                onToggleFavorite: () => m(u.id),
                buildUrl: r,
                tenantId: l?.id,
                existingRequestStatus: p(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") }),
          d.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: d.map((s) => /* @__PURE__ */ e(
            A,
            {
              app: s,
              isFavorite: n(s.id),
              onToggleFavorite: () => m(s.id),
              buildUrl: r,
              tenantId: l?.id,
              existingRequestStatus: p(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 {
  re as ApplicationsPage
};
//# sourceMappingURL=ApplicationsPage-BYugqtCB.js.map
