'use client'; import Link from 'next/link'; import { useEffect, useMemo, useState } from 'react'; import { t } from '@/i18n'; import { clientApi } from '@/lib/client-api'; import { routes } from '@/lib/site'; import type { DNAResponse, DnaDimension } from '@/lib/types'; import { DNA_DIMS, DnaRadial } from '@/components/charts/dna-radial'; import { Segmented } from '@/components/controls/indicator-select'; import { EntityPicker, type PickedEntity } from '@/components/explore/entity-picker'; type RefKind = 'world' | 'region' | 'income' | 'country'; /** * Country DNA 2.0: the fingerprint with a reference profile — World (50 on every axis), the region's median, * income peers' median or another country — fetched from `/countries/{id}/dna?reference=`. The table below lists * the nine percentile dimensions with the reference values. Descriptive, never a score. */ export function DnaPanel({ countryId, name, initial }: { countryId: string; name: string; initial: DNAResponse | null }) { const [kind, setKind] = useState('world'); const [peer, setPeer] = useState(null); const [cache, setCache] = useState>({}); const [loading, setLoading] = useState(false); const refKey = kind === 'country' ? (peer ? `country:${peer.id}` : null) : kind === 'world' ? null : kind; useEffect(() => { if (!refKey || cache[refKey] !== undefined) return; const ctrl = new AbortController(); setLoading(true); const reference = refKey.startsWith('country:') ? refKey.slice(8) : refKey; clientApi .countryDna(countryId, reference, ctrl.signal) .then((r) => setCache((c) => ({ ...c, [refKey]: r }))) .catch(() => setCache((c) => ({ ...c, [refKey]: null }))) .finally(() => { if (!ctrl.signal.aborted) setLoading(false); }); return () => ctrl.abort(); }, [refKey, countryId, cache]); const base = initial; const withRef = refKey ? cache[refKey] : null; const reference: Record | null = useMemo(() => { if (kind === 'world') return Object.fromEntries(DNA_DIMS.map((d) => [d, 50])); return withRef?.reference?.dims ?? null; }, [kind, withRef]); const refLabel = kind === 'world' ? t('country.dna.ref.worldLabel') : withRef?.reference?.label ?? (kind === 'country' ? peer?.name ?? null : null); if (!base) return

{t('country.dna.none')}

; return (
value={kind} onChange={setKind} label={t('country.dna.ref')} size="sm" options={[{ value: 'world', label: t('country.dna.ref.world') }, { value: 'region', label: t('country.dna.ref.region') }, { value: 'income', label: t('country.dna.ref.income') }, { value: 'country', label: t('country.dna.ref.country') }]} /> {kind === 'country' ? : null}

{t('country.dna.notScore')} {base.year_ref ? · {base.year_ref} : null}

{reference && (kind !== 'country' || peer) ? ( ) : null} {DNA_DIMS.map((d: DnaDimension) => { const v = base.dims[d]; const r = reference?.[d]; const row = base.dimensions.find((x) => x.id === d); return ( {reference && (kind !== 'country' || peer) ? : null} ); })}
{t('country.dna.title')}
{t('country.dna.dimension')} {name} {refLabel}
{row?.indicator ? ( {t(`country.dna.${d}` as const)} ) : ( t(`country.dna.${d}` as const) )} {v != null ? Math.round(v) : '—'}{r != null ? Math.round(r) : '—'}
); }