'use strict';

/**
 * Ícones modulares de recursos.
 *
 * Todos os símbolos são SVG internos para não dependerem dos emojis/fontes do
 * Windows. O módulo é usado na barra superior, custos, recompensas, relatórios,
 * espionagem, administração e restantes interfaces de recursos.
 */
(() => {
  const ICONS = Object.freeze({
    wood: `<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4.2 7.2h11.2a4.4 4.4 0 0 1 0 8.8H4.2A2.2 2.2 0 0 1 2 13.8V9.4a2.2 2.2 0 0 1 2.2-2.2Z" fill="#9a592d" stroke="#5a321d" stroke-width="1.2"/><ellipse cx="15.4" cy="11.6" rx="4.4" ry="4.4" fill="#d79248" stroke="#5a321d" stroke-width="1.2"/><ellipse cx="15.4" cy="11.6" rx="2.15" ry="2.15" fill="none" stroke="#8c4d28" stroke-width="1"/><path d="M5.2 9.2H12M5.2 13.8h6.4" stroke="#d69a62" stroke-width="1.1" stroke-linecap="round"/></svg>`,
    stone: `<svg viewBox="0 0 24 24" aria-hidden="true"><path d="m3.2 16.8 3.6-7.3 5.8-2.8 4.3 3.1 3.9 7-3 2.4H6.1Z" fill="#8d969d" stroke="#485159" stroke-width="1.2" stroke-linejoin="round"/><path d="m6.8 9.5 3.4 3.1 2.4-5.9M10.2 12.6l-2.5 6.2M12.6 6.7l4.3 3.1-1.8 3.8" fill="none" stroke="#cbd0d4" stroke-width="1.05" stroke-linecap="round" stroke-linejoin="round"/></svg>`,
    food: `<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 21V5.3M12 9.2C8.6 9 6.8 7.4 6.5 4.5c3.4.1 5.2 1.7 5.5 4.7Zm0 4.4c-3.6-.1-5.6-1.7-6.1-4.8 3.7.1 5.7 1.7 6.1 4.8Zm0 4.2c-3.3 0-5.2-1.5-5.8-4.4 3.5 0 5.4 1.5 5.8 4.4Zm0-8.6c3.4-.2 5.2-1.8 5.5-4.7-3.4.1-5.2 1.7-5.5 4.7Zm0 4.4c3.6-.1 5.6-1.7 6.1-4.8-3.7.1-5.7 1.7-6.1 4.8Zm0 4.2c3.3 0 5.2-1.5 5.8-4.4-3.5 0-5.4 1.5-5.8 4.4Z" fill="#e3b52e" stroke="#806415" stroke-width=".85" stroke-linecap="round" stroke-linejoin="round"/></svg>`,
    coins: `<svg viewBox="0 0 24 24" aria-hidden="true"><ellipse cx="12" cy="7" rx="7.2" ry="3.4" fill="#f6c842" stroke="#8b6212" stroke-width="1.2"/><path d="M4.8 7v3.2c0 1.9 3.2 3.4 7.2 3.4s7.2-1.5 7.2-3.4V7" fill="#dba526" stroke="#8b6212" stroke-width="1.2"/><path d="M4.8 10.2v3.2c0 1.9 3.2 3.4 7.2 3.4s7.2-1.5 7.2-3.4v-3.2" fill="#efbb31" stroke="#8b6212" stroke-width="1.2"/><path d="M4.8 13.4v3.2c0 1.9 3.2 3.4 7.2 3.4s7.2-1.5 7.2-3.4v-3.2" fill="#d99f20" stroke="#8b6212" stroke-width="1.2"/><path d="M9.1 7h5.8" stroke="#fff1a1" stroke-width="1.1" stroke-linecap="round"/></svg>`,
    gems: `<svg viewBox="0 0 24 24" aria-hidden="true"><path d="m4.2 8.2 3.2-4h9.2l3.2 4L12 20.2Z" fill="#35c8ef" stroke="#106385" stroke-width="1.25" stroke-linejoin="round"/><path d="m4.2 8.2 4.9 1.1L12 20.2 14.9 9.3l4.9-1.1M7.4 4.2l1.7 5.1 2.9-5.1 2.9 5.1 1.7-5.1" fill="none" stroke="#baf3ff" stroke-width="1" stroke-linejoin="round"/><path d="M5.5 8.5h13" stroke="#0d759a" stroke-width=".9"/></svg>`
  });

  const LABELS = Object.freeze({
    wood: 'Madeira',
    stone: 'Pedra',
    food: 'Comida',
    coins: 'Moedas',
    gems: 'Coroas'
  });

  const LEGACY_SYMBOLS = Object.freeze({
    '🪵': 'wood',
    '🪨': 'stone',
    '🌾': 'food',
    '🪙': 'coins',
    '💎': 'gems'
  });

  function escapeAttribute(value) {
    return String(value ?? '')
      .replaceAll('&', '&amp;')
      .replaceAll('"', '&quot;')
      .replaceAll('<', '&lt;')
      .replaceAll('>', '&gt;');
  }

  function safeType(type) {
    return Object.hasOwn(ICONS, type) ? type : 'coins';
  }

  function icon(type) {
    const normalized = safeType(type);
    const label = LABELS[normalized];
    return `<span class="resource-svg-icon resource-svg-${normalized}" role="img" aria-label="${escapeAttribute(label)}" title="${escapeAttribute(label)}">${ICONS[normalized]}</span>`;
  }

  function amount(type, value, format = String) {
    const normalized = safeType(type);
    const safeFormat = typeof format === 'function' ? format : String;
    return `<span class="resource-svg-item resource-svg-item-${normalized}">${icon(normalized)}<span class="resource-svg-value">${safeFormat(Number(value) || 0)}</span></span>`;
  }

  function list(resources, types = ['wood', 'stone', 'food', 'coins'], format = String) {
    const data = resources || {};
    const safeTypes = Array.isArray(types) && types.length ? types.map(safeType) : ['wood', 'stone', 'food', 'coins'];
    const parts = safeTypes.map(type => amount(type, data[type], format));
    return `<span class="resource-svg-line">${parts.join('<span class="resource-svg-separator" aria-hidden="true">·</span>')}</span>`;
  }

  function line(resources, format = String) {
    return list(resources, ['wood', 'stone', 'food', 'coins'], format);
  }

  /**
   * Converte símbolos antigos guardados em relatórios para SVG sem permitir HTML
   * não confiável. O texto é escapado primeiro e apenas depois recebe os ícones.
   */
  function richText(text, escapeHtml = escapeAttribute) {
    let safe = typeof escapeHtml === 'function' ? escapeHtml(String(text ?? '')) : escapeAttribute(text);
    for (const [symbol, type] of Object.entries(LEGACY_SYMBOLS)) {
      safe = safe.replaceAll(symbol, icon(type));
    }
    return safe;
  }

  globalThis.ReinosResourceIcons = Object.freeze({
    icon,
    amount,
    list,
    line,
    richText,
    labels: LABELS
  });
})();
