import { Head, Link } from '@inertiajs/react'; import { useEffect, useMemo, useState } from 'react'; import { ArrowUpRight, ChevronLeft, ChevronRight, Compass, Factory, FileText, Handshake, Landmark, Layers, MapPin, Newspaper, Search as SearchIcon, Store, TrendingUp, } from 'lucide-react'; import type { LucideIcon } from 'lucide-react'; type Result = { title: string; description: string | null; url: string; category: string; }; type Results = { data: Result[]; total: number; current_page: number; last_page: number; per_page: number; unavailable: string[]; }; type CategoryMeta = { icon: LucideIcon; iconClass: string; badgeClass: string }; const CATEGORY_META: Record = { 'Páginas e serviços': { icon: Compass, iconClass: 'bg-slate-100 text-slate-700', badgeClass: 'bg-slate-100 text-slate-700', }, Oportunidades: { icon: TrendingUp, iconClass: 'bg-red-50 text-[#b5121b]', badgeClass: 'bg-red-50 text-[#b5121b]', }, Negócios: { icon: Store, iconClass: 'bg-amber-50 text-amber-700', badgeClass: 'bg-amber-50 text-amber-700', }, 'Carteira: projectos, clusters e corredores': { icon: Layers, iconClass: 'bg-indigo-50 text-indigo-700', badgeClass: 'bg-indigo-50 text-indigo-700', }, 'Documentos e legislação': { icon: FileText, iconClass: 'bg-blue-50 text-blue-700', badgeClass: 'bg-blue-50 text-blue-700', }, Artigos: { icon: Newspaper, iconClass: 'bg-violet-50 text-violet-700', badgeClass: 'bg-violet-50 text-violet-700', }, Sectores: { icon: Factory, iconClass: 'bg-sky-50 text-sky-700', badgeClass: 'bg-sky-50 text-sky-700', }, Localizações: { icon: MapPin, iconClass: 'bg-teal-50 text-teal-700', badgeClass: 'bg-teal-50 text-teal-700', }, 'Zonas de investimento': { icon: Landmark, iconClass: 'bg-cyan-50 text-cyan-700', badgeClass: 'bg-cyan-50 text-cyan-700', }, Parcerias: { icon: Handshake, iconClass: 'bg-orange-50 text-orange-700', badgeClass: 'bg-orange-50 text-orange-700', }, }; const DEFAULT_META: CategoryMeta = { icon: Compass, iconClass: 'bg-slate-100 text-slate-700', badgeClass: 'bg-slate-100 text-slate-700', }; const VARIANT_GROUPS = [ ['sector', 'sectores', 'setor', 'setores'], ['projecto', 'projectos', 'projeto', 'projetos'], ['investir', 'investimento', 'investimentos'], ['investidor', 'investidores'], ['oportunidade', 'oportunidades'], ['documento', 'documentos'], ['empresa', 'empresas'], ['parceria', 'parcerias'], ['zona', 'zonas'], ['formulario', 'formularios'], ['provincia', 'provincias'], ['corredor', 'corredores'], ['cluster', 'clusters'], ['imposto', 'impostos'], ['licenca', 'licencas'], ]; const ACCENT_CLASSES: Record = { a: '[aáàâãä]', e: '[eéèêë]', i: '[iíìîï]', o: '[oóòôõö]', u: '[uúùûü]', c: '[cç]', n: '[nñ]', }; const QUICK_LINKS = [ { label: 'Sectores de investimento', description: 'Áreas prioritárias', href: '/sectores', icon: Factory, }, { label: 'Oportunidades', description: 'Projectos por sector e localização', href: '/oportunidades', icon: TrendingUp, }, { label: 'Documentos', description: 'Acervo e legislação', href: '/documentos', icon: FileText, }, { label: 'Zonas de investimento', description: 'ZEE, ZFI e parques', href: '/zonas', icon: Landmark, }, { label: 'Parcerias', description: 'Parceiros e propostas', href: '/parcerias', icon: Handshake, }, ]; function escapeRegExp(value: string): string { return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } function baseLetters(value: string): string { return value .normalize('NFD') .replace(/[\u0300-\u036f]/g, '') .toLowerCase(); } function accentInsensitivePattern(token: string): string { return baseLetters(token) .split('') .map((letter) => ACCENT_CLASSES[letter] ?? escapeRegExp(letter)) .join(''); } function tokenVariants(token: string): string[] { const group = VARIANT_GROUPS.find((variants) => variants.includes(baseLetters(token)), ); return group ?? [token]; } function buildHighlightRegex(term: string): RegExp | null { const tokens = [ ...new Set( term .toLowerCase() .split(/\s+/u) .map((token) => token.replace(/[.,;:!?"'()[\]{}]/g, '')) .filter((token) => token.length > 1), ), ]; if (!tokens.length) return null; const source = tokens .map((token) => { const variants = tokenVariants(token).map(accentInsensitivePattern); return variants.length > 1 ? `(?:${variants.join('|')})` : variants[0]; }) .join('|'); try { return new RegExp(`(${source})`, 'gi'); } catch { return null; } } function Highlight({ text, regex, }: { text: string | null; regex: RegExp | null; }) { if (!text) return null; if (!regex) return <>{text}; return ( <> {text.split(regex).map((part, index) => index % 2 === 1 ? ( {part} ) : ( {part} ), )} ); } function pageNumbers(current: number, last: number): (number | string)[] { const pages = [ ...new Set([ 1, last, ...Array.from({ length: 5 }, (_, i) => current - 2 + i), ]), ] .filter((page) => page >= 1 && page <= last) .sort((a, b) => a - b); return pages.flatMap((page, i) => i > 0 && page - pages[i - 1] > 1 ? [`gap-${page}`, page] : [page], ); } function QuickLinks() { return (
{QUICK_LINKS.map((link) => { const Icon = link.icon; return ( {link.label} {link.description}
); } export default function Search({ query, results, }: { query: string; results: Results; }) { const [input, setInput] = useState(query); useEffect(() => setInput(query), [query]); const { data, total, current_page: current, last_page: last, per_page: perPage, unavailable, } = results; const pageUrl = (page: number) => `/pesquisa?${new URLSearchParams({ q: query, page: String(page) })}`; const start = total ? (current - 1) * perPage + 1 : 0; const end = Math.min(start + data.length - 1, total); const highlight = useMemo(() => buildHighlightRegex(query), [query]); return (
{unavailable.length > 0 && (

Não foi possível consultar: {unavailable.join(', ')}. Os restantes resultados continuam disponíveis; tente novamente mais tarde.

)} {query ? ( <>

{total > 0 ? `Resultados ${start}–${end} de ${total} para “${query}”.` : `Nenhum resultado nas fontes consultadas para “${query}”.`}

{last > 1 && ( Página {current} de {last} )}
{total === 0 ? (

Sem correspondências

Experimente outras palavras ou uma expressão mais curta.

) : (
    {data.map((item) => { const meta = CATEGORY_META[item.category] ?? DEFAULT_META; const Icon = meta.icon; return (
  1. {item.category}

    {item.description && (

    )}
  2. ); })}
)} {last > 1 && ( )} ) : (

Comece por uma pesquisa

Encontre oportunidades, sectores, documentos e serviços do portal. Os resultados reúnem as fontes públicas disponíveis.

)}
); }