const levels = ['province_id', 'district_id', 'admin_post_id', 'locality_id']; const searchRoutes = ['/carteira', '/oportunidades', '/zonas', '/educacao']; /** Keep search geography explicit; a documentary origin is never an invented offer. */ export function portfolioPreparationHref(id: string, pageUrl: string): string { const source = new URL(pageUrl, 'https://portal.invalid'); const inherited = source.searchParams.has('catalogue_query') ? new URLSearchParams(source.searchParams.get('catalogue_query')!) : source.searchParams; const query = new URLSearchParams({ source: `portfolio:${id}` }); for (const field of levels) if (inherited.get(field)) query.set(field, inherited.get(field)!); return '/investimentos/iniciar?' + query.toString(); } /** Preserve an explicit complementary selection while consulting its canonical public fiche. */ export function complementPreparationHref( pageUrl: string, target?: string, ): string | null { const params = new URL(pageUrl, 'https://portal.invalid').searchParams; if ( !params.get('complement_origin') || !params.get('complement_code') || (target && params.get('complement_target') !== target) ) return null; const query = new URLSearchParams(); for (const field of [ 'complement_origin', 'complement_code', 'complement_target', 'complement_territory', 'selection_key', ]) if (params.get(field)) query.set(field, params.get(field)!); const dossier = params.get('investment_dossier'); return ( (dossier && /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i.test(dossier) ? `/investimentos/${dossier}/contexto/editar` : '/investimentos/iniciar') + '?' + query ); } /** Carry the explicitly consulted portfolio origin and search geography into the existing preparation entry. */ export function investmentPreparationHref( opportunity: number, pageUrl: string, origin?: string | null, ): string { const complementary = complementPreparationHref( pageUrl, `opportunity:${opportunity}`, ); if (complementary) return complementary; const source = new URL(pageUrl, 'https://portal.invalid'); const inherited = source.searchParams.has('catalogue_query') ? new URLSearchParams(source.searchParams.get('catalogue_query')!) : source.searchParams; const query = new URLSearchParams({ source: `opportunity:${opportunity}` }); if (origin) query.set('from_dossier', origin); for (const field of levels) if (inherited.get(field)) query.set(field, inherited.get(field)!); return '/investimentos/iniciar?' + query.toString(); } /** URL-scoped search context, never a saved investment or another browser tab. */ export function searchTerritoryHref(href: string, pageUrl: string): string { const base = 'https://portal.invalid'; const source = new URL(pageUrl, base); const target = new URL(href, base); const supported = (path: string) => searchRoutes.some( (route) => path === route || path.startsWith(`${route}/`), ); if ( target.origin !== base || !supported(source.pathname) || !supported(target.pathname) ) return href; const inherited = source.searchParams.has('catalogue_query') ? new URLSearchParams(source.searchParams.get('catalogue_query')!) : source.searchParams; // An explicit ancestor, including an explicit empty value, overrides its descendants. let changed = false; for (const field of levels) { if (target.searchParams.has(field)) { changed ||= target.searchParams.get(field) !== inherited.get(field); } else if (!changed && inherited.get(field)) { target.searchParams.set(field, inherited.get(field)!); } } return target.pathname + target.search + target.hash; }