// Endereço de empresa clicável: abre uma modal com o Google Maps embutido (sem sair do site).
// Uso: <AddressLink query="Rua X, 111, Bairro, Cidade/UF, CEP 00000-000">conteúdo exibido</AddressLink>
// Sem chave de API: usa o embed público (maps?q=…&output=embed), liberado por frame-src em vercel.json.
const vxMapQuery = s => String(s || '').replace(/\s*[·—]\s*/g, ', ').replace(/\bCEP\s+|\bnº\s*/gi, '').replace(/\s+/g, ' ').replace(/(,\s*){2,}/g, ', ').trim();
function AddressLink({ query, label, children, style, className }) {
  const { Modal, Button } = window.VRticeDesignSystem_d763eb;
  const [open, setOpen] = React.useState(false);
  const q = vxMapQuery(query);
  if (!q) return children || null;
  const enc = encodeURIComponent(q);
  const close = () => setOpen(false);
  return <>
    <button type="button" className={className} title="Ver no mapa" aria-label={'Ver no mapa: ' + q}
      onClick={e => { e.stopPropagation(); setOpen(true); }}
      style={{ all: 'unset', boxSizing: 'border-box', cursor: 'pointer', color: 'inherit', textDecoration: 'underline', textDecorationColor: 'var(--text-accent)', textUnderlineOffset: 3, textDecorationStyle: 'dotted', maxWidth: '100%', minWidth: 0, ...style }}>
      {children || label || q}
    </button>
    {open && <span onClick={e => e.stopPropagation()}>
      <Modal kicker="ENDEREÇO NO MAPA" title={label || q} width={760} onClose={close}
        footer={<><Button variant="secondary" icon="ph-arrow-square-out" onClick={() => window.open('https://www.google.com/maps/search/?api=1&query=' + enc, '_blank', 'noopener,noreferrer')}>Abrir no Google Maps</Button><Button variant="ghost" onClick={close}>Fechar</Button></>}>
        <div style={{ fontSize: 14, color: 'var(--text-muted)', overflowWrap: 'anywhere' }}>{q}</div>
        <iframe title={'Mapa: ' + q} src={'https://www.google.com/maps?q=' + enc + '&output=embed&hl=pt-BR'} loading="lazy" referrerPolicy="no-referrer-when-downgrade" allowFullScreen
          style={{ width: '100%', height: 'min(52vh, 420px)', minHeight: 260, border: '1px solid var(--border-subtle)', borderRadius: 12, background: 'var(--surface-sunken, transparent)' }} />
      </Modal>
    </span>}
  </>;
}
window.AddressLink = AddressLink;
