'use strict';

/**
 * Informação dos Barões Falken apresentada apenas ao passar o rato no mapa.
 * Mantém o mapa limpo e reproduz o comportamento visual típico do GGE.
 */
(() => {
  const VERSION = 1;

  const fallbackEscapeHtml = value => String(value ?? '')
    .replaceAll('&', '&amp;')
    .replaceAll('<', '&lt;')
    .replaceAll('>', '&gt;')
    .replaceAll('"', '&quot;')
    .replaceAll("'", '&#039;');

  function tooltipHtml(target, options = {}) {
    const escapeHtml = typeof options.escapeHtml === 'function'
      ? options.escapeHtml
      : fallbackEscapeHtml;
    const formatTime = typeof options.formatTime === 'function'
      ? options.formatTime
      : value => `${Math.max(0, Math.ceil(Number(value) || 0))} s`;
    const currentTime = Number(options.now) || Date.now();
    const progression = globalThis.ReinosFalkenProgression?.progress?.(target) || {
      level: Math.max(1, Math.floor(Number(target?.level) || 1)),
      remaining: 1,
      maxed: false
    };

    const nextLevel = Math.min(
      Number(globalThis.ReinosFalkenProgression?.MAX_LEVEL) || 80,
      progression.level + 1
    );
    const remaining = Math.max(0, Math.floor(Number(progression.remaining) || 0));
    const victoryText = remaining === 1 ? 'vitória' : 'vitórias';
    const progressText = progression.maxed
      ? 'Nível máximo alcançado'
      : `${remaining} ${victoryText} até ao nível ${nextLevel}`;

    const cooldownUntil = Number(target?.cooldownUntil) || 0;
    const cooldownText = cooldownUntil > currentTime
      ? `<div class="falken-map-tooltip-cooldown">Disponível em ${escapeHtml(formatTime(cooldownUntil - currentTime))}</div>`
      : '';

    return `<div class="falken-map-tooltip" role="tooltip" aria-hidden="true">
      <strong>${escapeHtml(target?.name || 'Barão Falken')}</strong>
      <div class="falken-map-tooltip-level">Nível: ${progression.level}</div>
      <div class="falken-map-tooltip-progress">${escapeHtml(progressText)}</div>
      ${cooldownText}
    </div>`;
  }

  globalThis.ReinosFalkenMapHover = Object.freeze({
    VERSION,
    tooltipHtml
  });
})();
