// VORA — cookie consent banner (RGPD). Injects a banner on first visit and
// stores the choice in localStorage so it is not shown again.
export function initCookies() {
  const KEY = 'vora-cookie-consent';

  let stored = null;
  try { stored = localStorage.getItem(KEY); } catch (e) { stored = null; }
  if (stored) return;

  const banner = document.createElement('div');
  banner.className = 'cookie-banner';
  banner.setAttribute('role', 'dialog');
  banner.setAttribute('aria-label', 'Aviso de cookies');
  banner.innerHTML = `
    <div class="cookie-banner__inner">
      <p class="cookie-banner__text">
        Usamos cookies para melhorar a sua experiência.
        <a href="privacidade">Política de Privacidade</a>.
      </p>
      <div class="cookie-banner__actions">
        <button type="button" class="cookie-banner__btn cookie-banner__btn--reject" data-consent="reject">Rejeitar</button>
        <button type="button" class="cookie-banner__btn cookie-banner__btn--accept" data-consent="accept">Aceitar</button>
      </div>
    </div>`;

  document.body.appendChild(banner);

  banner.addEventListener('click', (event) => {
    const btn = event.target.closest('[data-consent]');
    if (!btn) return;
    try { localStorage.setItem(KEY, btn.dataset.consent); } catch (e) { /* ignore */ }
    banner.remove();
  });
}
