import { jsxs as i, jsx as t } from "react/jsx-runtime";
import { useState as d, useCallback as M, useEffect as $, useMemo as O } from "react";
import { useNavigate as R } from "react-router-dom";
import { useTranslation as q } from "react-i18next";
import { Loader2 as y, Plus as F, EyeOff as N, Check as K, Copy as U, Pencil as H, Trash2 as _, Palette as z } from "lucide-react";
import { j as G, v as u, P as J, a0 as w, $ as Q } from "./index-CeEMms9z.js";
import { e as m } from "./extractErrorMessage-C0uxTGn5.js";
function ae() {
  const { t: s } = q("admin"), x = R(), { loadFromServer: C } = G(), [p, P] = d([]), [f, V] = d([]), [k, h] = d(!0), [v, o] = d(null), [D, n] = d(null), c = M(async () => {
    h(!0), o(null);
    try {
      const [e, a] = await Promise.all([
        u.uiThemes.list(!0),
        u.uiPresets.list({ includeDisabled: !0 })
      ]);
      P(e), V(a);
    } catch (e) {
      o(m(e));
    } finally {
      h(!1);
    }
  }, []);
  $(() => {
    c();
  }, [c]);
  const b = (e) => x(`/administration/uiconfiguration/presets/${encodeURIComponent(e)}/edit`), T = async (e) => {
    n(e.id);
    try {
      await u.uiPresets.setDisabled(e.id, !e.isDisabled), await c(), C();
    } catch (a) {
      o(m(a));
    } finally {
      n(null);
    }
  }, A = async (e) => {
    if (e.isBuiltin) return;
    const a = s("uiConfiguration.confirmDeletePreset", {
      defaultValue: "Supprimer le preset « {{name}} » ?",
      name: e.name
    });
    if (window.confirm(a)) {
      n(e.id);
      try {
        await u.uiPresets.remove(e.id), await c();
      } catch (r) {
        o(m(r));
      } finally {
        n(null);
      }
    }
  }, I = async (e) => {
    const a = window.prompt(
      s("uiConfiguration.newPresetName", { defaultValue: "Nom du nouveau preset" }),
      `${e.name} (copie)`
    );
    if (a?.trim()) {
      n(e.id);
      try {
        const r = await u.uiPresets.duplicate({
          sourcePresetId: e.id,
          newName: a.trim(),
          // Inherit scope from source (duplicate stays in same scope/tenant)
          scope: e.scope,
          tenantId: e.tenantId
        });
        await c(), b(r);
      } catch (r) {
        o(m(r));
      } finally {
        n(null);
      }
    }
  }, E = (e) => {
    x(`/administration/uiconfiguration/presets/new/edit?themeId=${encodeURIComponent(e)}`);
  }, S = (e) => e.stopPropagation(), j = O(() => {
    const e = /* @__PURE__ */ new Map();
    for (const a of p) e.set(a.id, []);
    for (const a of f)
      e.has(a.themeId) || e.set(a.themeId, []), e.get(a.themeId).push(a);
    return e;
  }, [p, f]);
  return /* @__PURE__ */ i(
    J,
    {
      title: s("uiConfiguration.presetsPageTitle", { defaultValue: "Presets" }),
      subtitle: s("uiConfiguration.presetsPageSubtitle", {
        defaultValue: "Variantes stylistiques (accent, palette d'items) liées à un thème parent."
      }),
      icon: /* @__PURE__ */ t(z, { className: "w-6 h-6" }),
      children: [
        v && /* @__PURE__ */ t("div", { className: "mb-4 p-3 rounded-[var(--radius-card)] bg-[var(--error-bg)] border border-[var(--error-border)] text-sm text-[var(--error-text)]", children: v }),
        k ? /* @__PURE__ */ i("div", { className: "flex items-center justify-center py-12 text-[var(--text-muted)]", children: [
          /* @__PURE__ */ t(y, { className: "w-5 h-5 animate-spin mr-2" }),
          s("common:loading", { defaultValue: "Chargement…" })
        ] }) : /* @__PURE__ */ t("div", { className: "space-y-6", children: p.map((e) => {
          const a = j.get(e.id) ?? [];
          return /* @__PURE__ */ i("section", { className: "rounded-[var(--radius-card)] border border-[var(--border-color)] bg-[var(--bg-secondary)] overflow-hidden", children: [
            /* @__PURE__ */ i("header", { className: "flex items-center justify-between gap-3 px-4 py-3 border-b border-[var(--border-color)] bg-[var(--bg-tertiary)]", children: [
              /* @__PURE__ */ i("div", { className: "flex items-center gap-3 min-w-0", children: [
                /* @__PURE__ */ t(w, { icon: e.icon, className: "w-6 h-6 text-[var(--text-primary)]" }),
                /* @__PURE__ */ i("div", { className: "min-w-0", children: [
                  /* @__PURE__ */ t("h3", { className: "text-base font-semibold text-[var(--text-primary)] truncate", children: e.name }),
                  /* @__PURE__ */ i("p", { className: "text-xs text-[var(--text-muted)]", children: [
                    a.length,
                    " preset(s)"
                  ] })
                ] })
              ] }),
              /* @__PURE__ */ i(
                "button",
                {
                  type: "button",
                  onClick: () => E(e.id),
                  className: "flex items-center gap-1.5 px-2.5 py-1.5 rounded-[var(--radius-button)] bg-[var(--color-accent-500)] text-white text-xs font-medium hover:bg-[var(--color-accent-600)]",
                  children: [
                    /* @__PURE__ */ t(F, { className: "w-3.5 h-3.5" }),
                    s("uiConfiguration.presetsCreate", { defaultValue: "Créer un preset" })
                  ]
                }
              )
            ] }),
            a.length === 0 ? /* @__PURE__ */ t("p", { className: "p-4 text-sm text-[var(--text-muted)] text-center", children: s("uiConfiguration.presetsNoForTheme", { defaultValue: "Aucun preset pour ce thème." }) }) : /* @__PURE__ */ t("table", { className: "w-full text-sm", children: /* @__PURE__ */ t("tbody", { children: a.map((r) => {
              const g = Q[r.accentColorKey]?.shades[500] ?? "#6366f1", l = D === r.id, B = r.scope === "Tenant" ? s("uiConfiguration.presetScopeTenant", { defaultValue: "Entreprise" }) : s("uiConfiguration.presetScopePlatform", { defaultValue: "Plateforme" }), L = r.scope === "Tenant" ? "bg-[var(--accent-bg)] text-[var(--accent-text)] border-[var(--accent-border)]" : "bg-[var(--bg-primary)] text-[var(--text-muted)] border-[var(--border-color)]";
              return /* @__PURE__ */ i(
                "tr",
                {
                  onClick: () => b(r.id),
                  title: s("uiConfiguration.presetsActionEdit", { defaultValue: "Modifier" }),
                  className: "border-t border-[var(--border-color)] hover:bg-[var(--bg-tertiary)] transition-colors cursor-pointer",
                  children: [
                    /* @__PURE__ */ t("td", { className: "px-4 py-2.5 w-10", children: /* @__PURE__ */ t(
                      "span",
                      {
                        className: "flex items-center justify-center w-8 h-8 rounded-[var(--radius-card)]",
                        style: { backgroundColor: g },
                        children: /* @__PURE__ */ t(w, { icon: r.icon, className: "w-4 h-4 text-white" })
                      }
                    ) }),
                    /* @__PURE__ */ i("td", { className: "px-2 py-2.5", children: [
                      /* @__PURE__ */ i("div", { className: "font-medium text-[var(--text-primary)] flex items-center gap-2 flex-wrap", children: [
                        r.name,
                        r.isBuiltin && /* @__PURE__ */ t("span", { className: "text-[9px] uppercase tracking-wider px-1.5 py-0.5 rounded bg-[var(--bg-primary)] text-[var(--text-muted)] border border-[var(--border-color)]", children: s("uiConfiguration.presetsBuiltinBadge", { defaultValue: "Built-in" }) })
                      ] }),
                      r.description && /* @__PURE__ */ t("div", { className: "text-xs text-[var(--text-muted)] mt-0.5", children: r.description })
                    ] }),
                    /* @__PURE__ */ t("td", { className: "px-2 py-2.5", children: /* @__PURE__ */ t("span", { className: ["inline-flex items-center gap-1 px-2 py-0.5 text-[10px] font-medium uppercase tracking-wider rounded border", L].join(" "), children: B }) }),
                    /* @__PURE__ */ t("td", { className: "px-2 py-2.5", children: /* @__PURE__ */ i("div", { className: "flex items-center gap-2", children: [
                      /* @__PURE__ */ t("span", { className: "w-5 h-5 rounded-md flex-shrink-0 border border-[var(--border-color)]", style: { backgroundColor: g } }),
                      /* @__PURE__ */ t("code", { className: "text-xs font-mono text-[var(--text-muted)]", children: r.accentColorKey })
                    ] }) }),
                    /* @__PURE__ */ t("td", { className: "px-2 py-2.5", children: r.isDisabled ? /* @__PURE__ */ i("span", { className: "inline-flex items-center gap-1 px-2 py-0.5 text-[10px] font-medium uppercase tracking-wider rounded bg-[var(--bg-primary)] text-[var(--text-muted)] border border-[var(--border-color)]", children: [
                      /* @__PURE__ */ t(N, { className: "w-3 h-3" }),
                      s("uiConfiguration.presetsStatusDisabled", { defaultValue: "Désactivé" })
                    ] }) : /* @__PURE__ */ i("span", { className: "inline-flex items-center gap-1 px-2 py-0.5 text-[10px] font-medium uppercase tracking-wider rounded bg-[var(--success-bg)] text-[var(--success-text)] border border-[var(--success-border)]", children: [
                      /* @__PURE__ */ t(K, { className: "w-3 h-3" }),
                      s("uiConfiguration.presetsStatusActive", { defaultValue: "Actif" })
                    ] }) }),
                    /* @__PURE__ */ t("td", { className: "px-3 py-2.5", onClick: S, children: /* @__PURE__ */ i("div", { className: "flex items-center justify-end gap-1", children: [
                      l && /* @__PURE__ */ t(y, { className: "w-4 h-4 animate-spin text-[var(--text-muted)]" }),
                      /* @__PURE__ */ t(
                        "button",
                        {
                          type: "button",
                          onClick: () => T(r),
                          disabled: l,
                          className: "p-1.5 rounded hover:bg-[var(--bg-hover)] text-[var(--text-muted)] hover:text-[var(--text-primary)] disabled:opacity-50",
                          title: r.isDisabled ? s("uiConfiguration.presetsActionEnable", { defaultValue: "Activer" }) : s("uiConfiguration.presetsActionDisable", { defaultValue: "Désactiver" }),
                          children: /* @__PURE__ */ t(N, { className: "w-4 h-4" })
                        }
                      ),
                      /* @__PURE__ */ t(
                        "button",
                        {
                          type: "button",
                          onClick: () => I(r),
                          disabled: l,
                          className: "p-1.5 rounded hover:bg-[var(--bg-hover)] text-[var(--text-muted)] hover:text-[var(--text-primary)] disabled:opacity-50",
                          title: s("uiConfiguration.presetsActionDuplicate", { defaultValue: "Dupliquer" }),
                          children: /* @__PURE__ */ t(U, { className: "w-4 h-4" })
                        }
                      ),
                      /* @__PURE__ */ t(
                        "button",
                        {
                          type: "button",
                          onClick: () => b(r.id),
                          disabled: l,
                          className: "p-1.5 rounded hover:bg-[var(--bg-hover)] text-[var(--text-muted)] hover:text-[var(--text-primary)] disabled:opacity-50",
                          title: s("uiConfiguration.presetsActionEdit", { defaultValue: "Modifier" }),
                          children: /* @__PURE__ */ t(H, { className: "w-4 h-4" })
                        }
                      ),
                      !r.isBuiltin && /* @__PURE__ */ t(
                        "button",
                        {
                          type: "button",
                          onClick: () => A(r),
                          disabled: l,
                          className: "p-1.5 rounded hover:bg-[var(--error-bg)] text-[var(--text-muted)] hover:text-[var(--error-text)] disabled:opacity-50",
                          title: s("uiConfiguration.presetsActionDelete", { defaultValue: "Supprimer" }),
                          children: /* @__PURE__ */ t(_, { className: "w-4 h-4" })
                        }
                      )
                    ] }) })
                  ]
                },
                r.id
              );
            }) }) })
          ] }, e.id);
        }) })
      ]
    }
  );
}
export {
  ae as PresetsListPage
};
//# sourceMappingURL=PresetsListPage-CZ7uMhbV.js.map
