// Rôle : regroupe les helpers d'assets, traduction et filtres Monster Hunter Wilds.
const MHWILDS_IMG_PATH = "/static/img/mhwilds";

export function assetPath(name) {
  return encodeURI(`${MHWILDS_IMG_PATH}/${String(name || "").replace(/_/g, " ")}.png`);
}

export function normalizeText(value) {
  return String(value || "").trim().toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/\s+/g, " ");
}

export function getConditionValues(conditions = []) {
  return [...new Set(conditions.flatMap((condition) => condition.values || []))];
}

export function getUniqueConditionValues(items, property, translate) {
  return [...new Set(items.flatMap((item) => getConditionValues(item[property])))]
    .filter((value) => value !== "none")
    .sort((a, b) => translate(a).localeCompare(translate(b), "fr"));
}

export function getMhwildsFilterKey(category, mhwilds) {
  const optionKey = mhwilds.filterOptionKeys?.[category];
  if (optionKey === "elements") return "weaknesses";
  return optionKey || (category === "monsters" ? "weaknesses" : "locations");
}

export function getFilteredMhwildsItems({ category, filterKey, mhwilds, filters, t }) {
  const items = category === "monsters" ? mhwilds.monsters : mhwilds.endemic;
  const activeFilters = filters[category];
  const selected = activeFilters[filterKey] || [];
  const search = normalizeText(activeFilters.name);

  return items.filter((item) => {
    const nameMatches = !search || normalizeText(t(item.name)).includes(search) || normalizeText(item.name).includes(search);
    if (!nameMatches) return false;
    if (!selected.length) return true;

    const values = getConditionValues(item[filterKey]);
    return activeFilters.logic === "or" && selected.length >= 2
      ? selected.some((value) => values.includes(value))
      : selected.every((value) => values.includes(value));
  });
}
