import { jsx as e, jsxs as o, Fragment as z } from "react/jsx-runtime";
import { useState as w, useCallback as Z, useEffect as _ } from "react";
import { useTranslation as B } from "react-i18next";
import { useNavigate as ee, useLocation as ae, Outlet as re, useOutletContext as G } from "react-router-dom";
import { v as F, P as te } from "./index-CeEMms9z.js";
import { U as se } from "./UnderlinedTabs-BEbz4Kre.js";
import { Loader2 as V, Settings as J, Upload as Y, FileText as q, Clock as U, Save as M, RotateCcw as O, History as I, AlertTriangle as oe, Shield as ce } from "lucide-react";
const R = [
  { id: "general", labelKey: "settings.tabs.general", icon: J, route: "/administration/configuration/settings/general" },
  { id: "file-upload", labelKey: "settings.tabs.fileUpload", icon: Y, route: "/administration/configuration/settings/file-upload" },
  { id: "legal-file", labelKey: "settings.tabs.legalFile", icon: q, route: "/administration/configuration/settings/legal-file" }
];
function me() {
  const { t: s } = B("admin"), h = ee(), i = ae(), [v, p] = w([]), [u, y] = w(!0), g = Z(async () => {
    try {
      y(!0);
      const m = await F.settings.getAll();
      p(m);
    } catch (m) {
      console.error("Failed to load settings:", m);
    } finally {
      y(!1);
    }
  }, []);
  _(() => {
    g();
  }, [g]);
  const k = () => {
    const m = i.pathname;
    return R.find(($) => m.includes($.id))?.id || "general";
  }, S = (m) => {
    const f = R.find(($) => $.id === m);
    f && h(f.route);
  };
  return u ? /* @__PURE__ */ e("div", { className: "flex items-center justify-center min-h-[400px]", children: /* @__PURE__ */ e(V, { className: "w-8 h-8 animate-spin text-[var(--color-accent-600)]" }) }) : /* @__PURE__ */ e(
    te,
    {
      title: s("settings.title"),
      subtitle: s("settings.subtitle"),
      breadcrumbs: [
        { label: s("header.title"), href: "/administration" },
        { label: s("settings.title") }
      ],
      children: /* @__PURE__ */ o("div", { className: "space-y-6", children: [
        /* @__PURE__ */ e(
          se,
          {
            tabs: R.map((m) => ({
              id: m.id,
              label: s(m.labelKey),
              icon: /* @__PURE__ */ e(m.icon, { className: "w-4 h-4" })
            })),
            activeTab: k(),
            onTabChange: S,
            ariaLabel: s("settings.tabs.label", "Onglets des paramètres")
          }
        ),
        /* @__PURE__ */ e(re, { context: { settings: v, refreshSettings: g } })
      ] })
    }
  );
}
function Q() {
  const { settings: s, refreshSettings: h } = G(), [i, v] = w({}), [p, u] = w(null), [y, g] = w(!1), [k, S] = w(null), [m, f] = w(null), [$, A] = w([]), [E, H] = w(!1), j = (r, n, l) => {
    v((d) => ({
      ...d,
      [`${r}.${n}`]: l
    }));
  }, D = (r) => {
    const n = `${r.category}.${r.key}`;
    return i[n] !== void 0 && i[n] !== r.value;
  }, t = (r) => {
    const n = `${r.category}.${r.key}`;
    return i[n] ?? r.value;
  }, x = (r) => `${r.category}.${r.key}`, N = async (r) => {
    const n = x(r), l = i[n];
    if (!(l === void 0 || l === r.value))
      try {
        u(n), await F.settings.update(r.category, r.key, l), v((d) => {
          const b = { ...d };
          return delete b[n], b;
        }), await h();
      } catch (d) {
        console.error("Failed to save setting:", d);
      } finally {
        u(null);
      }
  }, C = async (r) => {
    const l = (r ?? s).filter(
      (b) => i[`${b.category}.${b.key}`] !== void 0 && i[`${b.category}.${b.key}`] !== b.value
    );
    if (l.length === 0) return;
    g(!0), S(null);
    const d = [];
    for (const b of l) {
      const L = `${b.category}.${b.key}`;
      try {
        await F.settings.update(b.category, b.key, i[L]), v((P) => {
          const W = { ...P };
          return delete W[L], W;
        });
      } catch (P) {
        console.error(`Failed to save ${L}:`, P), d.push(L);
      }
    }
    d.length > 0 && S(d.join(", ")), await h(), g(!1);
  }, T = async (r) => {
    const n = x(r);
    try {
      u(n), await F.settings.reset(r.category, r.key), v((l) => {
        const d = { ...l };
        return delete d[n], d;
      }), await h();
    } catch (l) {
      console.error("Failed to reset setting:", l);
    } finally {
      u(null);
    }
  }, K = async (r, n) => {
    f({ category: r, key: n }), H(!0);
    try {
      const l = await F.settings.getHistory(r, n);
      A(l);
    } catch (l) {
      console.error("Failed to load history:", l);
    } finally {
      H(!1);
    }
  }, c = () => f(null), a = s.filter(
    (r) => i[`${r.category}.${r.key}`] !== void 0 && i[`${r.category}.${r.key}`] !== r.value
  ).length;
  return {
    settings: s,
    editedValues: i,
    saving: p,
    savingAll: y,
    saveAllError: k,
    pendingChangeCount: a,
    historyModal: m,
    history: $,
    loadingHistory: E,
    handleValueChange: j,
    hasChanges: D,
    getDisplayValue: t,
    getEditKey: x,
    handleSave: N,
    handleSaveAll: C,
    handleReset: T,
    openHistory: K,
    closeHistory: c
  };
}
function X({ category: s, settingKey: h, history: i, loading: v, onClose: p }) {
  const { t: u } = B("admin");
  return /* @__PURE__ */ e("div", { className: "fixed inset-0 z-50 overflow-y-auto", children: /* @__PURE__ */ o("div", { className: "flex items-center justify-center min-h-screen px-4", children: [
    /* @__PURE__ */ e("button", { type: "button", className: "fixed inset-0 bg-black/50", onClick: p, "aria-label": "Close modal" }),
    /* @__PURE__ */ o("div", { className: "relative rounded-[var(--radius-card)] bg-[var(--bg-card)] shadow-xl max-w-2xl w-full p-6", children: [
      /* @__PURE__ */ e("h3", { className: "text-lg font-semibold text-[var(--text-primary)] mb-4", children: u("settings.historyTitle", { key: `${s}.${h}` }) }),
      v && /* @__PURE__ */ e("div", { className: "flex justify-center py-8", children: /* @__PURE__ */ e(V, { className: "w-8 h-8 animate-spin text-[var(--color-accent-600)]" }) }),
      !v && i.length === 0 && /* @__PURE__ */ e("p", { className: "text-center py-8 text-[var(--text-secondary)]", children: u("settings.noHistory") }),
      !v && i.length > 0 && /* @__PURE__ */ e("div", { className: "space-y-3 max-h-96 overflow-y-auto", children: i.map((y) => /* @__PURE__ */ o("div", { className: "p-3 bg-[var(--bg-secondary)] rounded-lg", children: [
        /* @__PURE__ */ o("div", { className: "flex justify-between text-sm", children: [
          /* @__PURE__ */ e("span", { className: "text-[var(--text-secondary)]", children: y.changedByUserName || u("settings.system") }),
          /* @__PURE__ */ e("span", { className: "text-[var(--text-tertiary)]", children: new Date(y.changedAt).toLocaleString() })
        ] }),
        /* @__PURE__ */ o("div", { className: "mt-2 text-sm", children: [
          /* @__PURE__ */ e("span", { className: "text-[var(--error-text)] line-through", children: y.oldValue }),
          /* @__PURE__ */ e("span", { className: "mx-2 text-[var(--text-tertiary)]", children: "→" }),
          /* @__PURE__ */ e("span", { className: "text-[var(--success-text)]", children: y.newValue })
        ] })
      ] }, y.id)) }),
      /* @__PURE__ */ e("div", { className: "mt-6 flex justify-end", children: /* @__PURE__ */ e(
        "button",
        {
          onClick: p,
          className: "px-4 py-2 text-[var(--text-secondary)] hover:bg-[var(--bg-secondary)] rounded-[var(--radius-button)] transition-colors",
          children: u("common.cancel")
        }
      ) })
    ] })
  ] }) });
}
function be() {
  const { t: s } = B("admin"), {
    settings: h,
    saving: i,
    savingAll: v,
    saveAllError: p,
    historyModal: u,
    history: y,
    loadingHistory: g,
    handleValueChange: k,
    hasChanges: S,
    getDisplayValue: m,
    getEditKey: f,
    handleSave: $,
    handleSaveAll: A,
    handleReset: E,
    openHistory: H,
    closeHistory: j
  } = Q(), D = h.filter((c) => c.category === "Session"), t = h.filter((c) => c.category === "General"), x = [...D, ...t], N = x.filter(S).length, C = (c) => c === "Database" ? "bg-[var(--color-accent-500)]/10 text-[var(--color-accent-600)]" : c === "AppSettings" ? "bg-[var(--info-bg)]/50 text-[var(--info-text)]" : "bg-[var(--bg-secondary)] text-[var(--text-tertiary)]", T = (c, a, r) => c.valueType === "Bool" ? /* @__PURE__ */ o("label", { className: "relative inline-flex items-center cursor-pointer", children: [
    /* @__PURE__ */ e(
      "input",
      {
        type: "checkbox",
        checked: a === "true",
        onChange: (n) => k(c.category, c.key, n.target.checked ? "true" : "false"),
        disabled: !c.isEditable || r,
        className: "sr-only peer"
      }
    ),
    /* @__PURE__ */ e("div", { className: "w-11 h-6 bg-[var(--bg-tertiary)] peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-[var(--color-accent-500)]/30 rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-[var(--border-color)] after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-[var(--color-accent-600)] peer-disabled:opacity-50 peer-disabled:cursor-not-allowed" })
  ] }) : c.valueType === "Int" || c.valueType === "Decimal" ? /* @__PURE__ */ e(
    "input",
    {
      type: "number",
      value: a,
      onChange: (n) => k(c.category, c.key, n.target.value),
      disabled: !c.isEditable || r,
      className: "w-24 px-3 py-2 border border-[var(--border-color)] rounded-[var(--radius-button)] bg-[var(--bg-card)] text-[var(--text-primary)] disabled:opacity-50 disabled:cursor-not-allowed focus:ring-2 focus:ring-[var(--color-accent-500)] focus:border-[var(--color-accent-500)]"
    }
  ) : /* @__PURE__ */ e(
    "input",
    {
      type: c.isSensitive ? "password" : "text",
      value: c.isSensitive ? "********" : a,
      onChange: (n) => k(c.category, c.key, n.target.value),
      disabled: !c.isEditable || r || c.isSensitive,
      className: "w-48 px-3 py-2 border border-[var(--border-color)] rounded-[var(--radius-button)] bg-[var(--bg-card)] text-[var(--text-primary)] disabled:opacity-50 disabled:cursor-not-allowed focus:ring-2 focus:ring-[var(--color-accent-500)] focus:border-[var(--color-accent-500)]"
    }
  ), K = (c) => {
    const a = f(c), r = i === a, n = S(c), l = m(c);
    return /* @__PURE__ */ o(
      "div",
      {
        className: "flex items-center justify-between py-4 border-b border-[var(--border-color)] last:border-0",
        children: [
          /* @__PURE__ */ o("div", { className: "flex-1 min-w-0 pr-4", children: [
            /* @__PURE__ */ o("div", { className: "flex items-center gap-2", children: [
              /* @__PURE__ */ e("span", { className: "font-medium text-[var(--text-primary)]", children: s(`settings.keys.${c.category}.${c.key}`, c.key) }),
              !c.isEditable && /* @__PURE__ */ e("span", { className: "px-2 py-0.5 text-xs bg-[var(--bg-secondary)] text-[var(--text-tertiary)] rounded", children: s("settings.readonly") }),
              /* @__PURE__ */ e("span", { className: `px-2 py-0.5 text-xs rounded ${C(c.source)}`, children: c.source })
            ] }),
            c.description && /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)] mt-1", children: s(`settings.descriptions.${c.category}.${c.key}`, c.description) }),
            c.modifiedAt && /* @__PURE__ */ o("p", { className: "text-xs text-[var(--text-tertiary)] mt-1 flex items-center gap-1", children: [
              /* @__PURE__ */ e(U, { className: "w-3 h-3" }),
              s("settings.modifiedBy", { user: c.modifiedByUserName, date: new Date(c.modifiedAt).toLocaleString() })
            ] })
          ] }),
          /* @__PURE__ */ o("div", { className: "flex items-center gap-2", children: [
            T(c, l, r),
            c.isEditable && c.source === "Database" && /* @__PURE__ */ o(z, { children: [
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: () => $(c),
                  disabled: !n || r,
                  className: "p-2 text-[var(--color-accent-600)] hover:bg-[var(--color-accent-500)]/10 rounded-[var(--radius-button)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors",
                  title: s("common.save"),
                  children: r ? /* @__PURE__ */ e(V, { className: "w-4 h-4 animate-spin" }) : /* @__PURE__ */ e(M, { className: "w-4 h-4" })
                }
              ),
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: () => E(c),
                  disabled: c.value === c.defaultValue || r,
                  className: "p-2 text-[var(--text-secondary)] hover:bg-[var(--bg-secondary)] rounded-[var(--radius-button)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors",
                  title: s("settings.resetToDefault"),
                  children: /* @__PURE__ */ e(O, { className: "w-4 h-4" })
                }
              ),
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: () => H(c.category, c.key),
                  className: "p-2 text-[var(--text-secondary)] hover:bg-[var(--bg-secondary)] rounded-[var(--radius-button)] transition-colors",
                  title: s("settings.viewHistory"),
                  children: /* @__PURE__ */ e(I, { className: "w-4 h-4" })
                }
              )
            ] })
          ] })
        ]
      },
      a
    );
  };
  return /* @__PURE__ */ o("div", { className: "space-y-6", children: [
    D.length > 0 && /* @__PURE__ */ o("div", { className: "rounded-[var(--radius-card)] border border-[var(--border-color)] bg-[var(--bg-card)] overflow-hidden shadow-sm", children: [
      /* @__PURE__ */ e("div", { className: "px-6 py-4 bg-gradient-to-r from-[var(--color-accent-500)]/10 to-[var(--color-accent-600)]/5 border-b border-[var(--border-color)]", children: /* @__PURE__ */ o("div", { className: "flex items-center gap-3", children: [
        /* @__PURE__ */ e("div", { className: "p-2 rounded-lg bg-[var(--color-accent-500)]/20", children: /* @__PURE__ */ e(U, { className: "w-5 h-5 text-[var(--color-accent-600)]" }) }),
        /* @__PURE__ */ o("div", { children: [
          /* @__PURE__ */ e("h3", { className: "text-lg font-semibold text-[var(--text-primary)]", children: s("settings.sections.session") }),
          /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)]", children: s("settings.sections.sessionDescription") })
        ] })
      ] }) }),
      /* @__PURE__ */ e("div", { className: "px-6 py-2", children: D.map(K) })
    ] }),
    t.length > 0 && /* @__PURE__ */ o("div", { className: "rounded-[var(--radius-card)] border border-[var(--border-color)] bg-[var(--bg-card)] overflow-hidden shadow-sm", children: [
      /* @__PURE__ */ e("div", { className: "px-6 py-4 bg-gradient-to-r from-[var(--color-accent-500)]/10 to-[var(--color-accent-600)]/5 border-b border-[var(--border-color)]", children: /* @__PURE__ */ o("div", { className: "flex items-center gap-3", children: [
        /* @__PURE__ */ e("div", { className: "p-2 rounded-lg bg-[var(--color-accent-500)]/20", children: /* @__PURE__ */ e(J, { className: "w-5 h-5 text-[var(--color-accent-600)]" }) }),
        /* @__PURE__ */ o("div", { children: [
          /* @__PURE__ */ e("h3", { className: "text-lg font-semibold text-[var(--text-primary)]", children: s("settings.sections.general") }),
          /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)]", children: s("settings.sections.generalDescription") })
        ] })
      ] }) }),
      /* @__PURE__ */ e("div", { className: "px-6 py-2", children: t.map(K) })
    ] }),
    D.length === 0 && t.length === 0 && /* @__PURE__ */ e("div", { className: "text-center py-12 text-[var(--text-secondary)]", children: s("settings.noSettings") }),
    N > 0 && /* @__PURE__ */ o("div", { className: "sticky bottom-0 z-10 -mx-4 -mb-4 px-4 py-3 bg-[var(--bg-card)] border-t border-[var(--border-color)] shadow-lg flex items-center justify-between gap-4", children: [
      /* @__PURE__ */ o("div", { className: "flex items-center gap-2", children: [
        /* @__PURE__ */ e("span", { className: "text-sm text-[var(--text-primary)] font-medium", children: s("settings.pendingChanges", { count: N, defaultValue: "{{count}} modification(s) en attente" }) }),
        p && /* @__PURE__ */ e("span", { className: "text-xs text-[var(--error-text)]", children: s("settings.saveAllPartialError", "Échec partiel : {{keys}}", { keys: p }) })
      ] }),
      /* @__PURE__ */ o(
        "button",
        {
          type: "button",
          onClick: () => A(x),
          disabled: v,
          className: "inline-flex items-center gap-2 px-4 py-2 rounded-[var(--radius-button)] bg-[var(--color-accent-600)] text-white font-medium hover:bg-[var(--color-accent-700)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors",
          children: [
            v ? /* @__PURE__ */ e(V, { className: "w-4 h-4 animate-spin" }) : /* @__PURE__ */ e(M, { className: "w-4 h-4" }),
            s("settings.saveAll", "Tout enregistrer")
          ]
        }
      )
    ] }),
    u && /* @__PURE__ */ e(
      X,
      {
        category: u.category,
        settingKey: u.key,
        history: y,
        loading: g,
        onClose: j
      }
    )
  ] });
}
function pe() {
  const { t: s } = B("admin"), {
    settings: h,
    saving: i,
    historyModal: v,
    history: p,
    loadingHistory: u,
    handleValueChange: y,
    hasChanges: g,
    getDisplayValue: k,
    getEditKey: S,
    handleSave: m,
    handleReset: f,
    openHistory: $,
    closeHistory: A
  } = Q(), E = h.filter((t) => t.category === "FileUpload"), H = (t) => {
    try {
      return JSON.parse(t);
    } catch {
      return [];
    }
  }, j = (t, x, N) => t.valueType === "Int" ? /* @__PURE__ */ o("div", { className: "flex items-center gap-2", children: [
    /* @__PURE__ */ e(
      "input",
      {
        type: "number",
        value: x,
        onChange: (C) => y(t.category, t.key, C.target.value),
        disabled: !t.isEditable || N,
        className: "w-24 px-3 py-2 border border-[var(--border-color)] rounded-[var(--radius-button)] bg-[var(--bg-card)] text-[var(--text-primary)] disabled:opacity-50 disabled:cursor-not-allowed focus:ring-2 focus:ring-[var(--color-accent-500)] focus:border-[var(--color-accent-500)]"
      }
    ),
    t.key === "MaxFileSizeMB" && /* @__PURE__ */ e("span", { className: "text-sm text-[var(--text-secondary)]", children: "MB" })
  ] }) : /* @__PURE__ */ e(
    "input",
    {
      type: t.isSensitive ? "password" : "text",
      value: t.isSensitive ? "********" : x,
      onChange: (C) => y(t.category, t.key, C.target.value),
      disabled: !t.isEditable || N || t.isSensitive,
      className: "w-48 px-3 py-2 border border-[var(--border-color)] rounded-[var(--radius-button)] bg-[var(--bg-card)] text-[var(--text-primary)] disabled:opacity-50 disabled:cursor-not-allowed focus:ring-2 focus:ring-[var(--color-accent-500)] focus:border-[var(--color-accent-500)]"
    }
  ), D = (t) => {
    const x = S(t), N = i === x, C = g(t), T = k(t);
    if (t.valueType === "Json" && t.key === "AllowedExtensions") {
      const K = H(T);
      return /* @__PURE__ */ o("div", { className: "py-4 border-b border-[var(--border-color)] last:border-0", children: [
        /* @__PURE__ */ o("div", { className: "flex items-center justify-between mb-3", children: [
          /* @__PURE__ */ o("div", { children: [
            /* @__PURE__ */ o("div", { className: "flex items-center gap-2", children: [
              /* @__PURE__ */ e("span", { className: "font-medium text-[var(--text-primary)]", children: s(`settings.keys.${t.category}.${t.key}`, t.key) }),
              !t.isEditable && /* @__PURE__ */ e("span", { className: "px-2 py-0.5 text-xs bg-[var(--bg-secondary)] text-[var(--text-tertiary)] rounded", children: s("settings.readonly") }),
              /* @__PURE__ */ e("span", { className: "px-2 py-0.5 text-xs bg-[var(--color-accent-500)]/10 text-[var(--color-accent-600)] rounded", children: t.source })
            ] }),
            t.description && /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)] mt-1", children: s(`settings.descriptions.${t.category}.${t.key}`, t.description) })
          ] }),
          t.isEditable && t.source === "Database" && /* @__PURE__ */ e(
            "button",
            {
              onClick: () => $(t.category, t.key),
              className: "p-2 text-[var(--text-secondary)] hover:bg-[var(--bg-secondary)] rounded-[var(--radius-button)] transition-colors",
              title: s("settings.viewHistory"),
              children: /* @__PURE__ */ e(I, { className: "w-4 h-4" })
            }
          )
        ] }),
        /* @__PURE__ */ e("div", { className: "flex flex-wrap gap-2", children: K.map((c) => /* @__PURE__ */ e(
          "span",
          {
            className: "px-3 py-1 bg-[var(--bg-secondary)] text-[var(--text-secondary)] rounded-full text-sm",
            children: c
          },
          c
        )) })
      ] }, x);
    }
    return /* @__PURE__ */ o(
      "div",
      {
        className: "flex items-center justify-between py-4 border-b border-[var(--border-color)] last:border-0",
        children: [
          /* @__PURE__ */ o("div", { className: "flex-1 min-w-0 pr-4", children: [
            /* @__PURE__ */ o("div", { className: "flex items-center gap-2", children: [
              /* @__PURE__ */ e("span", { className: "font-medium text-[var(--text-primary)]", children: s(`settings.keys.${t.category}.${t.key}`, t.key) }),
              !t.isEditable && /* @__PURE__ */ e("span", { className: "px-2 py-0.5 text-xs bg-[var(--bg-secondary)] text-[var(--text-tertiary)] rounded", children: s("settings.readonly") }),
              /* @__PURE__ */ e("span", { className: `px-2 py-0.5 text-xs rounded ${t.source === "Database" ? "bg-[var(--color-accent-500)]/10 text-[var(--color-accent-600)]" : t.source === "AppSettings" ? "bg-[var(--info-bg)]/50 text-[var(--info-text)]" : "bg-[var(--bg-secondary)] text-[var(--text-tertiary)]"}`, children: t.source })
            ] }),
            t.description && /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)] mt-1", children: s(`settings.descriptions.${t.category}.${t.key}`, t.description) }),
            t.modifiedAt && /* @__PURE__ */ o("p", { className: "text-xs text-[var(--text-tertiary)] mt-1 flex items-center gap-1", children: [
              /* @__PURE__ */ e(U, { className: "w-3 h-3" }),
              s("settings.modifiedBy", { user: t.modifiedByUserName, date: new Date(t.modifiedAt).toLocaleString() })
            ] })
          ] }),
          /* @__PURE__ */ o("div", { className: "flex items-center gap-2", children: [
            j(t, T, N),
            t.isEditable && t.source === "Database" && /* @__PURE__ */ o(z, { children: [
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: () => m(t),
                  disabled: !C || N,
                  className: "p-2 text-[var(--color-accent-600)] hover:bg-[var(--color-accent-500)]/10 rounded-[var(--radius-button)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors",
                  title: s("common.save"),
                  children: N ? /* @__PURE__ */ e(V, { className: "w-4 h-4 animate-spin" }) : /* @__PURE__ */ e(M, { className: "w-4 h-4" })
                }
              ),
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: () => f(t),
                  disabled: t.value === t.defaultValue || N,
                  className: "p-2 text-[var(--text-secondary)] hover:bg-[var(--bg-secondary)] rounded-[var(--radius-button)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors",
                  title: s("settings.resetToDefault"),
                  children: /* @__PURE__ */ e(O, { className: "w-4 h-4" })
                }
              ),
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: () => $(t.category, t.key),
                  className: "p-2 text-[var(--text-secondary)] hover:bg-[var(--bg-secondary)] rounded-[var(--radius-button)] transition-colors",
                  title: s("settings.viewHistory"),
                  children: /* @__PURE__ */ e(I, { className: "w-4 h-4" })
                }
              )
            ] })
          ] })
        ]
      },
      x
    );
  };
  return /* @__PURE__ */ o("div", { className: "space-y-6", children: [
    /* @__PURE__ */ o("div", { className: "rounded-[var(--radius-card)] border border-[var(--border-color)] bg-[var(--bg-card)] overflow-hidden shadow-sm", children: [
      /* @__PURE__ */ e("div", { className: "px-6 py-4 bg-gradient-to-r from-[var(--color-accent-500)]/10 to-[var(--color-accent-600)]/5 border-b border-[var(--border-color)]", children: /* @__PURE__ */ o("div", { className: "flex items-center gap-3", children: [
        /* @__PURE__ */ e("div", { className: "p-2 rounded-lg bg-[var(--color-accent-500)]/20", children: /* @__PURE__ */ e(Y, { className: "w-5 h-5 text-[var(--color-accent-600)]" }) }),
        /* @__PURE__ */ o("div", { children: [
          /* @__PURE__ */ e("h3", { className: "text-lg font-semibold text-[var(--text-primary)]", children: s("settings.sections.fileUpload") }),
          /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)]", children: s("settings.sections.fileUploadDescription") })
        ] })
      ] }) }),
      /* @__PURE__ */ e("div", { className: "px-6 py-2", children: E.length > 0 ? E.map(D) : /* @__PURE__ */ e("div", { className: "text-center py-8 text-[var(--text-secondary)]", children: s("settings.noSettings") }) })
    ] }),
    v && /* @__PURE__ */ e(
      X,
      {
        category: v.category,
        settingKey: v.key,
        history: p,
        loading: u,
        onClose: A
      }
    )
  ] });
}
function xe() {
  const { t: s } = B("admin"), { settings: h, refreshSettings: i } = G(), [v, p] = w({}), [u, y] = w(null), [g, k] = w(null), [S, m] = w([]), [f, $] = w(!1), A = h.filter((a) => a.category === "LegalFile"), E = (a, r, n) => {
    p((l) => ({
      ...l,
      [`${a}.${r}`]: n
    }));
  }, H = (a) => {
    const r = `${a.category}.${a.key}`;
    return v[r] !== void 0 && v[r] !== a.value;
  }, j = (a) => {
    const r = `${a.category}.${a.key}`;
    return v[r] ?? a.value;
  }, D = async (a) => {
    const r = `${a.category}.${a.key}`, n = v[r];
    if (!(n === void 0 || n === a.value))
      try {
        y(r), await F.settings.update(a.category, a.key, n), p((l) => {
          const d = { ...l };
          return delete d[r], d;
        }), await i();
      } catch (l) {
        console.error("Failed to save setting:", l);
      } finally {
        y(null);
      }
  }, t = async (a) => {
    const r = `${a.category}.${a.key}`;
    try {
      y(r), await F.settings.reset(a.category, a.key), p((n) => {
        const l = { ...n };
        return delete l[r], l;
      }), await i();
    } catch (n) {
      console.error("Failed to reset setting:", n);
    } finally {
      y(null);
    }
  }, x = async (a, r) => {
    k({ category: a, key: r }), $(!0);
    try {
      const n = await F.settings.getHistory(a, r);
      m(n);
    } catch (n) {
      console.error("Failed to load history:", n);
    } finally {
      $(!1);
    }
  }, N = (a) => {
    const r = Math.floor(a / 365), n = a % 365;
    return n === 0 ? `${r} ${s("settings.years")}` : `${r} ${s("settings.years")} ${n} ${s("settings.days")}`;
  }, C = (a) => !a.isEditable && (a.key === "RetentionDays" || a.key === "EnableWorm"), T = (a, r, n, l) => a.valueType === "Bool" ? /* @__PURE__ */ o("label", { className: "relative inline-flex cursor-pointer", children: [
    /* @__PURE__ */ e("span", { className: "sr-only", children: a.key }),
    /* @__PURE__ */ e(
      "input",
      {
        type: "checkbox",
        checked: r === "true",
        onChange: (d) => l(a.category, a.key, d.target.checked ? "true" : "false"),
        disabled: !a.isEditable || n,
        className: "sr-only peer",
        "aria-label": a.key
      }
    ),
    /* @__PURE__ */ e("div", { className: "w-11 h-6 bg-[var(--bg-tertiary)] peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-[var(--color-accent-500)]/30 rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-[var(--border-color)] after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-[var(--color-accent-600)] peer-disabled:opacity-50 peer-disabled:cursor-not-allowed" })
  ] }) : a.valueType === "Int" ? /* @__PURE__ */ o("div", { className: "flex items-center gap-2", children: [
    /* @__PURE__ */ e(
      "input",
      {
        type: "number",
        value: r,
        onChange: (d) => l(a.category, a.key, d.target.value),
        disabled: !a.isEditable || n,
        className: "w-24 px-3 py-2 border border-[var(--border-color)] rounded-[var(--radius-button)] bg-[var(--bg-card)] text-[var(--text-primary)] disabled:opacity-50 disabled:cursor-not-allowed focus:ring-2 focus:ring-[var(--color-accent-500)] focus:border-[var(--color-accent-500)]"
      }
    ),
    (a.key === "RetentionDays" || a.key === "ArchiveAfterDays") && /* @__PURE__ */ o("span", { className: "text-sm text-[var(--text-secondary)]", children: [
      "(",
      N(parseInt(r) || 0),
      ")"
    ] })
  ] }) : /* @__PURE__ */ e(
    "input",
    {
      type: a.isSensitive ? "password" : "text",
      value: a.isSensitive ? "********" : r,
      onChange: (d) => l(a.category, a.key, d.target.value),
      disabled: !a.isEditable || n || a.isSensitive,
      className: "w-48 px-3 py-2 border border-[var(--border-color)] rounded-[var(--radius-button)] bg-[var(--bg-card)] text-[var(--text-primary)] disabled:opacity-50 disabled:cursor-not-allowed focus:ring-2 focus:ring-[var(--color-accent-500)] focus:border-[var(--color-accent-500)]"
    }
  ), K = (a) => a === "Database" ? "bg-[var(--color-accent-500)]/10 text-[var(--color-accent-600)]" : a === "AppSettings" ? "bg-[var(--info-bg)]/50 text-[var(--info-text)]" : "bg-[var(--bg-secondary)] text-[var(--text-tertiary)]", c = (a) => {
    const r = `${a.category}.${a.key}`, n = u === r, l = H(a), d = j(a), b = C(a);
    return /* @__PURE__ */ o(
      "div",
      {
        className: `flex items-center justify-between py-4 border-b border-[var(--border-color)] last:border-0 ${b ? "bg-[var(--warning-bg)] -mx-6 px-6" : ""}`,
        children: [
          /* @__PURE__ */ o("div", { className: "flex-1 min-w-0 pr-4", children: [
            /* @__PURE__ */ o("div", { className: "flex items-center gap-2", children: [
              /* @__PURE__ */ e("span", { className: "font-medium text-[var(--text-primary)]", children: s(`settings.keys.${a.category}.${a.key}`, a.key) }),
              b && /* @__PURE__ */ o("span", { className: "px-2 py-0.5 text-xs bg-[var(--warning-bg)] text-[var(--warning-text)] border border-[var(--warning-border)] rounded flex items-center gap-1", children: [
                /* @__PURE__ */ e(ce, { className: "w-3 h-3" }),
                s("settings.legallyProtected")
              ] }),
              !a.isEditable && !b && /* @__PURE__ */ e("span", { className: "px-2 py-0.5 text-xs bg-[var(--bg-secondary)] text-[var(--text-tertiary)] rounded", children: s("settings.readonly") }),
              /* @__PURE__ */ e("span", { className: `px-2 py-0.5 text-xs rounded ${K(a.source)}`, children: a.source })
            ] }),
            a.description && /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)] mt-1", children: s(`settings.descriptions.${a.category}.${a.key}`, a.description) }),
            a.modifiedAt && /* @__PURE__ */ o("p", { className: "text-xs text-[var(--text-tertiary)] mt-1 flex items-center gap-1", children: [
              /* @__PURE__ */ e(U, { className: "w-3 h-3" }),
              s("settings.modifiedBy", { user: a.modifiedByUserName, date: new Date(a.modifiedAt).toLocaleString() })
            ] })
          ] }),
          /* @__PURE__ */ o("div", { className: "flex items-center gap-2", children: [
            T(a, d, n, E),
            a.isEditable && a.source === "Database" && /* @__PURE__ */ o(z, { children: [
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: () => D(a),
                  disabled: !l || n,
                  className: "p-2 text-[var(--color-accent-600)] hover:bg-[var(--color-accent-500)]/10 rounded-[var(--radius-button)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors",
                  title: s("common.save"),
                  children: n ? /* @__PURE__ */ e(V, { className: "w-4 h-4 animate-spin" }) : /* @__PURE__ */ e(M, { className: "w-4 h-4" })
                }
              ),
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: () => t(a),
                  disabled: a.value === a.defaultValue || n,
                  className: "p-2 text-[var(--text-secondary)] hover:bg-[var(--bg-secondary)] rounded-[var(--radius-button)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors",
                  title: s("settings.resetToDefault"),
                  children: /* @__PURE__ */ e(O, { className: "w-4 h-4" })
                }
              ),
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: () => x(a.category, a.key),
                  className: "p-2 text-[var(--text-secondary)] hover:bg-[var(--bg-secondary)] rounded-[var(--radius-button)] transition-colors",
                  title: s("settings.viewHistory"),
                  children: /* @__PURE__ */ e(I, { className: "w-4 h-4" })
                }
              )
            ] })
          ] })
        ]
      },
      r
    );
  };
  return /* @__PURE__ */ o("div", { className: "space-y-6", children: [
    /* @__PURE__ */ e("div", { className: "bg-[var(--warning-bg)] border border-[var(--warning-border)] rounded-[var(--radius-card)] p-4", children: /* @__PURE__ */ o("div", { className: "flex items-start gap-3", children: [
      /* @__PURE__ */ e(oe, { className: "w-5 h-5 text-[var(--warning-text)] flex-shrink-0 mt-0.5" }),
      /* @__PURE__ */ o("div", { children: [
        /* @__PURE__ */ e("h4", { className: "font-medium text-[var(--warning-text)]", children: s("settings.legalWarningTitle") }),
        /* @__PURE__ */ e("p", { className: "text-sm text-[var(--warning-text)] opacity-90 mt-1", children: s("settings.legalWarningDescription") })
      ] })
    ] }) }),
    /* @__PURE__ */ o("div", { className: "rounded-[var(--radius-card)] border border-[var(--border-color)] bg-[var(--bg-card)] overflow-hidden shadow-sm", children: [
      /* @__PURE__ */ e("div", { className: "px-6 py-4 bg-gradient-to-r from-[var(--warning-bg)] to-[var(--warning-bg)]/60 border-b border-[var(--border-color)]", children: /* @__PURE__ */ o("div", { className: "flex items-center gap-3", children: [
        /* @__PURE__ */ e("div", { className: "p-2 rounded-lg bg-[var(--warning-bg)]", children: /* @__PURE__ */ e(q, { className: "w-5 h-5 text-[var(--warning-text)]" }) }),
        /* @__PURE__ */ o("div", { children: [
          /* @__PURE__ */ e("h3", { className: "text-lg font-semibold text-[var(--text-primary)]", children: s("settings.sections.legalFile") }),
          /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)]", children: s("settings.sections.legalFileDescription") })
        ] })
      ] }) }),
      /* @__PURE__ */ e("div", { className: "px-6 py-2", children: A.length > 0 ? A.map(c) : /* @__PURE__ */ e("div", { className: "text-center py-8 text-[var(--text-secondary)]", children: s("settings.noSettings") }) })
    ] }),
    g && /* @__PURE__ */ e("div", { className: "fixed inset-0 z-50 overflow-y-auto", children: /* @__PURE__ */ o("div", { className: "flex items-center justify-center min-h-screen px-4", children: [
      /* @__PURE__ */ e("button", { type: "button", className: "fixed inset-0 bg-black/50", onClick: () => k(null), "aria-label": "Close modal" }),
      /* @__PURE__ */ o("div", { className: "relative rounded-[var(--radius-card)] bg-[var(--bg-card)] shadow-xl max-w-2xl w-full p-6", children: [
        /* @__PURE__ */ e("h3", { className: "text-lg font-semibold text-[var(--text-primary)] mb-4", children: s("settings.historyTitle", { key: `${g.category}.${g.key}` }) }),
        f && /* @__PURE__ */ e("div", { className: "flex justify-center py-8", children: /* @__PURE__ */ e(V, { className: "w-8 h-8 animate-spin text-[var(--color-accent-600)]" }) }),
        !f && S.length === 0 && /* @__PURE__ */ e("p", { className: "text-center py-8 text-[var(--text-secondary)]", children: s("settings.noHistory") }),
        !f && S.length > 0 && /* @__PURE__ */ e("div", { className: "space-y-3 max-h-96 overflow-y-auto", children: S.map((a) => /* @__PURE__ */ o("div", { className: "p-3 bg-[var(--bg-secondary)] rounded-lg", children: [
          /* @__PURE__ */ o("div", { className: "flex justify-between text-sm", children: [
            /* @__PURE__ */ e("span", { className: "text-[var(--text-secondary)]", children: a.changedByUserName || s("settings.system") }),
            /* @__PURE__ */ e("span", { className: "text-[var(--text-tertiary)]", children: new Date(a.changedAt).toLocaleString() })
          ] }),
          /* @__PURE__ */ o("div", { className: "mt-2 text-sm", children: [
            /* @__PURE__ */ e("span", { className: "text-[var(--error-text)] line-through", children: a.oldValue }),
            /* @__PURE__ */ e("span", { className: "mx-2 text-[var(--text-tertiary)]", children: "→" }),
            /* @__PURE__ */ e("span", { className: "text-[var(--success-text)]", children: a.newValue })
          ] })
        ] }, a.id)) }),
        /* @__PURE__ */ e("div", { className: "mt-6 flex justify-end", children: /* @__PURE__ */ e(
          "button",
          {
            onClick: () => k(null),
            className: "px-4 py-2 text-[var(--text-secondary)] hover:bg-[var(--bg-secondary)] rounded-[var(--radius-button)] transition-colors",
            children: s("common.cancel")
          }
        ) })
      ] })
    ] }) })
  ] });
}
export {
  pe as FileUploadSettingsSection,
  be as GeneralSettingsSection,
  xe as LegalFileSettingsSection,
  me as SettingsPage
};
//# sourceMappingURL=index-CA7FxcSq.js.map
