// Rôle : regroupe les helpers d'affichage et de filtrage Diablo IV.
export function normalizeText(value) {
  return String(value || "").trim().toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/\s+/g, " ");
}

export function formatCategory(value) {
  return String(value || "").replace(/_/g, " ");
}

export function getCategoryId(category) {
  return typeof category === "string" ? category : category?.id || "";
}

export function getCategoryLabel(category) {
  return typeof category === "string" ? formatCategory(category) : category?.label || formatCategory(category?.id);
}

export function getCategoryIconStyle(category) {
  const icon = typeof category === "string" ? "" : category?.icon;
  return icon ? { "--diablo4-category-icon": `url("/static/img/diablo4/${icon}.svg")` } : undefined;
}

export function getFilteredDiablo4Affixes({ affixes, filters }) {
  const selected = filters.categories || [];
  const search = normalizeText(filters.name);

  return affixes.filter((affix) => {
    const nameMatches = !search || normalizeText(affix.label).includes(search) || normalizeText(affix.id).includes(search);
    if (!nameMatches) return false;
    if (!selected.length) return true;

    const categories = affix.categories || [];
    return filters.logic === "or" && selected.length >= 2
      ? selected.some((category) => categories.includes(category))
      : selected.every((category) => categories.includes(category));
  });
}
