---
// Mini-mapa da costa com o ponto marcado + "Como chegar" (Google Maps / Waze).
// O mapa interativo do Google só carrega quando a pessoa clica (página leve e sem cookies antes do aceite).
import { getCollection } from 'astro:content';
import { noPonto, dividir, caminho, COSTA, T_FORTE, rotaGoogle, rotaWaze, mapaEmbed } from '../lib/costa';
import Icone from './Icone.astro';
interface Props { nome: string; consulta: string; km: number; endereco?: string | null; zoom?: number }
const { nome, consulta, km, endereco, zoom = 13 } = Astro.props;
const ds = (await getCollection('destinos')).sort((a, b) => a.data.ordem - b.data.ordem);
const pin = noPonto(km);
const W = 460, H = 300;
const vx = Math.max(0, Math.min(1000 - W, pin.x - W / 2)), vy = Math.max(0, Math.min(690 - H, pin.y - H / 2));
const [c1, c2] = dividir(T_FORTE);
const uid = `m${Math.random().toString(36).slice(2, 7)}`;
// rótulo do ponto: à direita do alfinete, ou à esquerda quando não couber no recorte
const cabeDireita = pin.x + 24 + nome.length * 9 < vx + W - 8;
const rotX = cabeDireita ? pin.x + 24 : pin.x - 24;
const rotAncora = cabeDireita ? 'start' : 'end';
---
<div class="mapa-local">
  <div class="mapa-area" data-mapa={mapaEmbed(consulta, zoom)} data-titulo={`Mapa: ${nome}`}>
    <svg viewBox={`${vx} ${vy} ${W} ${H}`} preserveAspectRatio="xMidYMid slice" role="img" aria-label={`Localização de ${nome} no Litoral Norte`}>
      <defs><pattern id={`o${uid}`} width="40" height="14" patternUnits="userSpaceOnUse"><path d="M0 7 q10 -6 20 0 t20 0" fill="none" stroke="#fff" stroke-opacity=".55" stroke-width="1.2" /></pattern></defs>
      <rect x="0" y="0" width="1000" height="690" fill="#a9d5e5" />
      <rect x="0" y="0" width="1000" height="690" fill={`url(#o${uid})`} />
      <path d={`${COSTA} L1000 70 L1000 0 L0 0 L0 650 Z`} fill="#efe6d8" />
      <path d={COSTA} fill="none" stroke="#e3d2b6" stroke-width="16" stroke-linecap="round" />
      <path d={caminho(c1)} fill="none" stroke="#ec671a" stroke-width="3" stroke-linecap="round" />
      <path d={caminho(c2)} fill="none" stroke="#0e5a7e" stroke-width="3" stroke-linecap="round" />
      {ds.map((d) => { const p = noPonto(d.data.km_salvador); return <g><circle cx={p.x} cy={p.y} r="4.5" fill="#fff" stroke="#8b9196" stroke-width="1.5" /><text x={p.x - p.nx * 14} y={p.y - p.ny * 14 + 4} font-size="11" fill="#8b9196" font-family="Outfit Variable, sans-serif" text-anchor={p.nx > 0 ? 'end' : 'start'}>{d.data.nome === nome || Math.hypot(p.x - pin.x, p.y - pin.y) < 60 ? '' : d.data.nome}</text></g>; })}
      <g transform={`translate(${pin.x} ${pin.y})`}>
        <circle r="22" fill="#ec671a" opacity=".18"><animate attributeName="r" values="14;26;14" dur="2.6s" repeatCount="indefinite" /></circle>
        <path d="M0 0 C -9 -12 -14 -20 -14 -28 a14 14 0 1 1 28 0 C 14 -20 9 -12 0 0Z" fill="#ec671a" stroke="#fff" stroke-width="2.5" />
        <circle cy="-28" r="5" fill="#fff" />
      </g>
      <text x={rotX} y={pin.y + 5} font-size="16" font-weight="500" fill="#1f2326" font-family="Outfit Variable, sans-serif" text-anchor={rotAncora} paint-order="stroke" stroke="#efe6d8" stroke-width="5">{nome}</text>
    </svg>
    <button type="button" class="btn btn-branco btn-p mapa-carregar" data-carregar-mapa><Icone n="pin" /> Ver mapa interativo</button>
  </div>
  <div class="mapa-rodape">
    <p><b>{nome}</b>{endereco && <span>{endereco}</span>}</p>
    <div class="mapa-acoes">
      <a class="btn btn-laranja" href={rotaGoogle(consulta)} target="_blank" rel="noopener" data-clique="como-chegar" data-nome={nome} data-alvo={nome}><Icone n="seta" /> Como chegar</a>
      <a class="btn btn-linha" href={rotaWaze(consulta)} target="_blank" rel="noopener" data-clique="como-chegar-waze" data-nome={nome} data-alvo={nome}>Waze</a>
    </div>
  </div>
</div>
