'use strict';

/**
 * Marcador visual dos castelos dos jogadores no mapa mundial.
 *
 * Este módulo trata apenas da apresentação do ícone e da faixa com o nome.
 * Não altera coordenadas, cliques, espionagem, ataques ou dados dos jogadores.
 */
(() => {
  const ASSET_PATH = '../assets/castelo_jogador_mapa_gge.png';

  function playerName(value) {
    const name = String(value || '').trim();
    return name || 'Jogador';
  }

  function renderNode(options = {}, context = {}) {
    const escapeHtml = typeof context.escapeHtml === 'function'
      ? context.escapeHtml
      : value => String(value ?? '');
    const worldPixelX = typeof context.worldPixelX === 'function'
      ? context.worldPixelX
      : value => Number(value) || 0;
    const worldPixelY = typeof context.worldPixelY === 'function'
      ? context.worldPixelY
      : value => Number(value) || 0;

    const username = playerName(options.username);
    const safeUsername = escapeHtml(username);
    const x = Number(options.x) || 0;
    const y = Number(options.y) || 0;
    const own = Boolean(options.own);
    const action = own ? 'map-home' : 'player-open';
    const userId = own ? '' : ` data-user-id="${escapeHtml(options.userId || '')}"`;
    const title = own
      ? `O teu castelo — X ${x} • Y ${y}`
      : `Espiar ou atacar ${username} — X ${x} • Y ${y}`;
    const safeTitle = escapeHtml(title);
    return `<div class="map-node player ${own ? 'player-own' : 'player-other'} player-castle-marker" style="left:${worldPixelX(x)}px;top:${worldPixelY(y)}px">
      <button class="player-castle-btn player-castle-map-btn" data-action="${action}"${userId} title="${safeTitle}" aria-label="Castelo ${safeUsername}, coordenadas X ${x}, Y ${y}">
        <img class="player-castle-art" src="${ASSET_PATH}" alt="" draggable="false" />
      </button>
      <div class="player-castle-nameplate" aria-hidden="true"><strong>Castelo ${safeUsername}</strong></div>
    </div>`;
  }

  globalThis.ReinosPlayerCastleMap = Object.freeze({
    renderNode,
    ASSET_PATH
  });
})();
