import { jsx as a, jsxs as s, Fragment as he } from "react/jsx-runtime";
import { Loader2 as te, AlertCircle as ge, Save as ve, Check as be, Palette as xe } from "lucide-react";
import { useTranslation as j } from "react-i18next";
import { v as d, a7 as F, P as ye } from "./index-CeEMms9z.js";
import { useState as l, useCallback as C, useEffect as Ce } from "react";
import { e as k } from "./extractErrorMessage-C0uxTGn5.js";
import { L as ee } from "./LogoUploader-MJaO1AEq.js";
const S = {
  logo: { width: 256, height: 64 },
  favicon: { width: 64, height: 64 }
}, ae = {
  canUsersChangeTheme: !0,
  canUsersChangePreset: !0,
  canUsersCreatePresets: !0,
  allowedThemeIds: null,
  allowedPresetIds: null,
  allowUserCustomization: !0
};
function Ne({ scope: t, tenantId: r }) {
  const { t: i } = j(["admin", "common"]), [c, re] = l("identity"), [M, p] = l(null), [O, h] = l(null), [A, ie] = l([]), [se, ne] = l([]), [le, q] = l(!0), [E, g] = l(!1), [L, v] = l(!1), [P, m] = l(null), [b, B] = l(""), [I, K] = l(""), [N, R] = l(""), [f, H] = l(""), [U, T] = l(null), [u, x] = l(ae), [V, _] = l(!0), $ = C(async () => {
    q(!0), m(null);
    try {
      const [e, o] = await Promise.all([
        d.uiThemes.list(),
        d.uiPresets.list()
      ]);
      if (ie(e), ne(o), t === "platform") {
        const n = await d.platformUiConfig.get();
        p(n), B(n.displayName ?? ""), K(n.tagline ?? ""), R(n.subdomain ?? ""), H(n.defaultThemeId), T(n.defaultPresetId), x(n.userPermissions);
      } else if (r) {
        const n = await d.tenantBranding.get(r);
        h(n), B(n.displayName ?? ""), H(n.defaultThemeId ?? ""), T(n.defaultPresetId), _(n.userPermissions !== null), x(n.userPermissions ?? ae);
      }
    } catch (e) {
      m(k(e));
    } finally {
      q(!1);
    }
  }, [t, r]);
  Ce(() => {
    $();
  }, [$]);
  const oe = C(async () => {
    g(!0), m(null);
    try {
      if (t === "platform") {
        const e = await d.platformUiConfig.updateIdentity({
          displayName: b.trim() || null,
          tagline: I.trim() || null,
          subdomain: N.trim() || null
        });
        p(e);
      } else if (r) {
        const e = await d.tenantBranding.updateIdentity(r, {
          displayName: b.trim() || null
        });
        h(e);
      }
      v(!0), F(), setTimeout(() => v(!1), 2e3);
    } catch (e) {
      m(k(e));
    } finally {
      g(!1);
    }
  }, [t, r, b, I, N]), de = C(async () => {
    g(!0), m(null);
    try {
      if (t === "platform") {
        const e = await d.platformUiConfig.updateDefaults({
          defaultThemeId: f,
          defaultPresetId: U
        });
        p(e);
      } else if (r) {
        const e = await d.tenantBranding.updateDefaults(r, {
          defaultThemeId: f || null,
          defaultPresetId: U
        });
        h(e);
      }
      v(!0), setTimeout(() => v(!1), 2e3);
    } catch (e) {
      m(k(e));
    } finally {
      g(!1);
    }
  }, [t, r, f, U]), ue = C(async () => {
    g(!0), m(null);
    try {
      if (t === "platform") {
        const e = await d.platformUiConfig.updateUserPermissions(u);
        p(e);
      } else if (r) {
        const e = await d.tenantBranding.updateUserPermissions(r, {
          permissions: V ? u : null
        });
        h(e);
      }
      v(!0), setTimeout(() => v(!1), 2e3);
    } catch (e) {
      m(k(e));
    } finally {
      g(!1);
    }
  }, [t, r, u, V]), G = C(
    async (e, o, n, Z) => {
      if (t === "platform") {
        const z = await d.platformUiConfig.uploadAsset(e, o, n, Z);
        p(z);
      } else if (r) {
        const z = await d.tenantBranding.uploadAsset(r, e, o, n, Z);
        h(z);
      }
      F();
    },
    [t, r]
  ), J = C(
    async (e) => {
      if (t === "platform") {
        const o = await d.platformUiConfig.deleteAsset(e);
        p(o);
      } else if (r) {
        const o = await d.tenantBranding.deleteAsset(r, e);
        h(o);
      }
      F();
    },
    [t, r]
  );
  if (le)
    return /* @__PURE__ */ a("div", { className: "flex items-center justify-center py-12", children: /* @__PURE__ */ a(te, { className: "w-6 h-6 animate-spin text-[var(--text-muted)]" }) });
  if (P && !M && !O)
    return /* @__PURE__ */ a("p", { className: "text-[var(--error-text)]", children: P });
  const y = t === "platform" ? M : O, ce = y?.logoUrl ?? null, me = y?.faviconUrl ?? null, Q = { width: y?.logoWidth ?? null, height: y?.logoHeight ?? null }, X = { width: y?.faviconWidth ?? null, height: y?.faviconHeight ?? null }, fe = [
    { id: "identity", label: i("admin:uiConfiguration.tabIdentity", { defaultValue: "Identité" }) },
    { id: "logos", label: i("admin:uiConfiguration.tabLogos", { defaultValue: "Logos" }) },
    { id: "defaults", label: i("admin:uiConfiguration.tabDefaults", { defaultValue: "Valeurs par défaut" }) },
    { id: "permissions", label: i("admin:uiConfiguration.tabPermissions", { defaultValue: "Permissions utilisateurs" }) }
  ], pe = se.filter((e) => e.themeId === f), Y = A.find((e) => e.id === f) ?? A[0];
  return /* @__PURE__ */ s("div", { className: "space-y-6", children: [
    /* @__PURE__ */ a("div", { className: "flex gap-6 border-b border-[var(--border-color)] overflow-x-auto [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden", children: fe.map((e) => {
      const o = e.id === c;
      return /* @__PURE__ */ a(
        "button",
        {
          type: "button",
          onClick: () => re(e.id),
          className: [
            "py-3 text-sm font-medium whitespace-nowrap transition-colors border-b-2 -mb-px",
            o ? "text-[var(--text-primary)] border-[var(--color-accent-500)]" : "text-[var(--text-muted)] border-transparent hover:text-[var(--text-secondary)]"
          ].join(" "),
          children: e.label
        },
        e.id
      );
    }) }),
    P && /* @__PURE__ */ s("div", { className: "flex items-start gap-2 p-3 rounded-[var(--radius-card)] bg-[var(--error-bg)] border border-[var(--error-border)]", children: [
      /* @__PURE__ */ a(ge, { className: "w-5 h-5 text-[var(--error-text)] flex-shrink-0 mt-0.5" }),
      /* @__PURE__ */ a("div", { className: "flex-1 text-sm text-[var(--error-text)]", children: P }),
      /* @__PURE__ */ a("button", { type: "button", onClick: () => m(null), className: "text-xs text-[var(--error-text)] opacity-70 hover:opacity-100", children: "×" })
    ] }),
    c === "identity" && /* @__PURE__ */ s("section", { className: "space-y-4", children: [
      /* @__PURE__ */ s("div", { children: [
        /* @__PURE__ */ a("h3", { className: "text-lg font-semibold text-[var(--text-primary)]", children: i("admin:uiConfiguration.tabIdentity", { defaultValue: "Identité" }) }),
        /* @__PURE__ */ a("p", { className: "text-sm text-[var(--text-secondary)] mt-1", children: i("admin:uiConfiguration.identityDesc", { defaultValue: "Informations affichées dans le header, les e-mails et l'écran de connexion." }) })
      ] }),
      /* @__PURE__ */ s("div", { className: "rounded-[var(--radius-card)] bg-[var(--bg-secondary)] border border-[var(--border-color)] p-5 space-y-4", children: [
        /* @__PURE__ */ a(w, { label: i("admin:uiConfiguration.displayName", { defaultValue: "Nom affiché" }), children: /* @__PURE__ */ a(
          "input",
          {
            type: "text",
            value: b,
            onChange: (e) => B(e.target.value),
            className: "w-full px-3 py-2 rounded-[var(--radius-input)] bg-[var(--bg-primary)] border border-[var(--border-color)] text-[var(--text-primary)]"
          }
        ) }),
        t === "platform" && /* @__PURE__ */ s(he, { children: [
          /* @__PURE__ */ a(w, { label: i("admin:uiConfiguration.tagline", { defaultValue: "Accroche" }), children: /* @__PURE__ */ a(
            "input",
            {
              type: "text",
              value: I,
              onChange: (e) => K(e.target.value),
              className: "w-full px-3 py-2 rounded-[var(--radius-input)] bg-[var(--bg-primary)] border border-[var(--border-color)] text-[var(--text-primary)]"
            }
          ) }),
          /* @__PURE__ */ a(
            w,
            {
              label: i("admin:uiConfiguration.subdomain", { defaultValue: "Sous-domaine" }),
              hint: N ? `${N}.app.smartstack.io` : ".app.smartstack.io",
              children: /* @__PURE__ */ a(
                "input",
                {
                  type: "text",
                  value: N,
                  onChange: (e) => R(e.target.value.toLowerCase().replace(/[^a-z0-9-]/g, "")),
                  className: "w-full px-3 py-2 rounded-[var(--radius-input)] bg-[var(--bg-primary)] border border-[var(--border-color)] text-[var(--text-primary)]"
                }
              )
            }
          )
        ] })
      ] }),
      /* @__PURE__ */ a(W, { onClick: oe, saving: E, saved: L })
    ] }),
    c === "logos" && /* @__PURE__ */ s("section", { className: "space-y-4", children: [
      /* @__PURE__ */ a(
        ee,
        {
          label: i("admin:uiConfiguration.logo", { defaultValue: "Logo" }),
          currentUrl: ce,
          onUpload: (e, o, n) => G("logo", e, o, n),
          onDelete: () => J("logo"),
          recommendedWidth: S.logo.width,
          recommendedHeight: S.logo.height,
          currentWidth: Q.width,
          currentHeight: Q.height,
          sampleKind: "logo",
          sampleLabel: b || "SmartStack"
        }
      ),
      /* @__PURE__ */ a(
        ee,
        {
          label: i("admin:uiConfiguration.favicon", { defaultValue: "Favicon" }),
          currentUrl: me,
          onUpload: (e, o, n) => G("favicon", e, o, n),
          onDelete: () => J("favicon"),
          recommendedWidth: S.favicon.width,
          recommendedHeight: S.favicon.height,
          currentWidth: X.width,
          currentHeight: X.height,
          sampleKind: "favicon",
          sampleLabel: b || "SmartStack"
        }
      )
    ] }),
    c === "defaults" && /* @__PURE__ */ s("section", { className: "space-y-4", children: [
      /* @__PURE__ */ s("div", { children: [
        /* @__PURE__ */ a("h3", { className: "text-lg font-semibold text-[var(--text-primary)]", children: i("admin:uiConfiguration.defaults", { defaultValue: "Thème et preset par défaut" }) }),
        /* @__PURE__ */ a("p", { className: "text-sm text-[var(--text-secondary)] mt-1", children: i("admin:uiConfiguration.defaultsDesc", {
          defaultValue: "Thème et preset appliqués par défaut aux nouveaux utilisateurs."
        }) })
      ] }),
      /* @__PURE__ */ s("div", { className: "rounded-[var(--radius-card)] bg-[var(--bg-secondary)] border border-[var(--border-color)] p-5 space-y-4", children: [
        /* @__PURE__ */ s(w, { label: i("admin:uiConfiguration.defaultTheme", { defaultValue: "Thème par défaut" }), children: [
          /* @__PURE__ */ s(
            "select",
            {
              value: f,
              onChange: (e) => {
                H(e.target.value), T(null);
              },
              className: "w-full px-3 py-2 rounded-[var(--radius-input)] bg-[var(--bg-primary)] border border-[var(--border-color)] text-[var(--text-primary)]",
              children: [
                t === "tenant" && /* @__PURE__ */ a("option", { value: "", children: "— Hériter de la plateforme —" }),
                A.map((e) => /* @__PURE__ */ s("option", { value: e.id, children: [
                  e.name,
                  e.isBuiltin ? " (built-in)" : ""
                ] }, e.id))
              ]
            }
          ),
          Y && /* @__PURE__ */ a("p", { className: "text-xs text-[var(--text-muted)] mt-1", children: Y.description ?? "" })
        ] }),
        /* @__PURE__ */ a(w, { label: i("admin:uiConfiguration.defaultPreset", { defaultValue: "Preset par défaut" }), children: /* @__PURE__ */ s(
          "select",
          {
            value: U ?? "",
            onChange: (e) => T(e.target.value || null),
            disabled: !f,
            className: "w-full px-3 py-2 rounded-[var(--radius-input)] bg-[var(--bg-primary)] border border-[var(--border-color)] text-[var(--text-primary)] disabled:opacity-50",
            children: [
              /* @__PURE__ */ a("option", { value: "", children: "— Aucun preset (accent du thème) —" }),
              pe.map((e) => /* @__PURE__ */ a("option", { value: e.id, children: e.name }, e.id))
            ]
          }
        ) })
      ] }),
      /* @__PURE__ */ a(W, { onClick: de, saving: E, saved: L })
    ] }),
    c === "permissions" && /* @__PURE__ */ s("section", { className: "space-y-4", children: [
      /* @__PURE__ */ s("div", { children: [
        /* @__PURE__ */ a("h3", { className: "text-lg font-semibold text-[var(--text-primary)]", children: i("admin:uiConfiguration.userPermissions", { defaultValue: "Permissions utilisateurs" }) }),
        /* @__PURE__ */ a("p", { className: "text-sm text-[var(--text-secondary)] mt-1", children: i("admin:uiConfiguration.userPermissionsDesc", {
          defaultValue: "Contrôle ce que les utilisateurs finaux peuvent modifier dans leur espace Préférences."
        }) })
      ] }),
      t === "tenant" && /* @__PURE__ */ s("label", { className: "flex items-center gap-2 cursor-pointer p-3 rounded-[var(--radius-card)] bg-[var(--bg-secondary)] border border-[var(--border-color)]", children: [
        /* @__PURE__ */ a(
          "input",
          {
            type: "checkbox",
            checked: V,
            onChange: (e) => _(e.target.checked),
            className: "w-4 h-4"
          }
        ),
        /* @__PURE__ */ a("span", { className: "text-sm text-[var(--text-primary)]", children: i("admin:uiConfiguration.tenantOverridePermissions", {
          defaultValue: "Surcharger les permissions pour ce tenant (sinon hérite de la plateforme)"
        }) })
      ] }),
      /* @__PURE__ */ s("div", { className: [
        "rounded-[var(--radius-card)] bg-[var(--bg-secondary)] border border-[var(--border-color)] p-5 space-y-3",
        t === "tenant" && !V ? "opacity-50 pointer-events-none" : ""
      ].join(" "), children: [
        /* @__PURE__ */ a(
          D,
          {
            label: "Autoriser à changer de thème",
            checked: u.canUsersChangeTheme,
            onChange: (e) => x({ ...u, canUsersChangeTheme: e })
          }
        ),
        /* @__PURE__ */ a(
          D,
          {
            label: "Autoriser à changer de preset",
            checked: u.canUsersChangePreset,
            onChange: (e) => x({ ...u, canUsersChangePreset: e })
          }
        ),
        /* @__PURE__ */ a(
          D,
          {
            label: "Autoriser à créer leurs propres presets",
            checked: u.canUsersCreatePresets,
            onChange: (e) => x({ ...u, canUsersCreatePresets: e })
          }
        ),
        /* @__PURE__ */ a(
          D,
          {
            label: "Personnalisation utilisateur globale autorisée",
            checked: u.allowUserCustomization,
            onChange: (e) => x({ ...u, allowUserCustomization: e })
          }
        )
      ] }),
      /* @__PURE__ */ a(W, { onClick: ue, saving: E, saved: L })
    ] })
  ] });
}
function w({
  label: t,
  hint: r,
  children: i
}) {
  return /* @__PURE__ */ s("div", { className: "space-y-1.5", children: [
    /* @__PURE__ */ a("label", { className: "block text-sm font-medium text-[var(--text-primary)]", children: t }),
    i,
    r && /* @__PURE__ */ a("p", { className: "text-xs text-[var(--text-muted)]", children: r })
  ] });
}
function D({
  label: t,
  checked: r,
  onChange: i
}) {
  return /* @__PURE__ */ s("label", { className: "flex items-center gap-2 cursor-pointer", children: [
    /* @__PURE__ */ a("input", { type: "checkbox", checked: r, onChange: (c) => i(c.target.checked), className: "w-4 h-4" }),
    /* @__PURE__ */ a("span", { className: "text-sm text-[var(--text-primary)]", children: t })
  ] });
}
function W({
  onClick: t,
  saving: r,
  saved: i
}) {
  const { t: c } = j("common");
  return /* @__PURE__ */ s("div", { className: "flex items-center gap-3 pt-4 border-t border-[var(--border-color)]", children: [
    /* @__PURE__ */ s(
      "button",
      {
        type: "button",
        onClick: t,
        disabled: r,
        className: "px-4 py-2 rounded-[var(--radius-button)] bg-[var(--color-accent-500)] text-white hover:bg-[var(--color-accent-600)] disabled:opacity-50 flex items-center gap-2",
        children: [
          r ? /* @__PURE__ */ a(te, { className: "w-4 h-4 animate-spin" }) : /* @__PURE__ */ a(ve, { className: "w-4 h-4" }),
          c("save", { defaultValue: "Enregistrer" })
        ]
      }
    ),
    i && /* @__PURE__ */ s("span", { className: "flex items-center gap-1 text-sm text-[var(--success-text)]", children: [
      /* @__PURE__ */ a(be, { className: "w-4 h-4" }),
      c("saved", { defaultValue: "Enregistré" })
    ] })
  ] });
}
function De() {
  const { t } = j(["admin", "navigation"]);
  return /* @__PURE__ */ a(
    ye,
    {
      title: t("admin:uiConfiguration.pageTitle", { defaultValue: "UI Configuration" }),
      subtitle: t("admin:uiConfiguration.pageSubtitle", {
        defaultValue: "Define the default branding and restrict the theme choices available to users."
      }),
      icon: /* @__PURE__ */ a(xe, { className: "w-6 h-6" }),
      children: /* @__PURE__ */ a(Ne, { scope: "platform" })
    }
  );
}
export {
  De as UiConfigurationPage
};
//# sourceMappingURL=UiConfigurationPage-BCtXrLyb.js.map
