'use client'; import Link from 'next/link'; import { useEffect, useState } from 'react'; import { t } from '@/i18n'; import { clientExplore } from '@/lib/client-api-explore'; import { cn } from '@/lib/cn'; import { fixed } from '@/lib/format'; import { routes } from '@/lib/site'; import type { SimilarResponse, SimilarityMode } from '@/lib/types'; import { EntityPicker, type PickedEntity } from '@/components/explore/entity-picker'; const MODES: SimilarityMode[] = ['overall', 'economic', 'demographic', 'energy', 'social']; /** Pick a country → peers per mode from `/countries/{id}/similar?mode=`. Mode tabs, score bars (0–100). */ export function SimilarityPlayground({ initial }: { initial?: PickedEntity | null }) { const [country, setCountry] = useState(initial ?? null); const [mode, setMode] = useState('overall'); const [data, setData] = useState>({}); const [loading, setLoading] = useState(false); useEffect(() => { if (!country) return; const key = `${country.id}:${mode}`; if (data[key] !== undefined) return; const ctrl = new AbortController(); setLoading(true); clientExplore .similar(country.id, mode, 10, ctrl.signal) .then((r) => setData((d) => ({ ...d, [key]: r }))) .catch(() => setData((d) => ({ ...d, [key]: null }))) .finally(() => { if (!ctrl.signal.aborted) setLoading(false); }); return () => ctrl.abort(); }, [country, mode, data]); const current = country ? data[`${country.id}:${mode}`] : undefined; const peers = current?.peers ?? []; return (
{country ? ( {country.flag} {country.name} → ) : null}
{country ? ( <>
{MODES.map((m) => ( ))}
{current === null || (current && peers.length === 0) ? (

{t('explore.similar.none', { name: country.name })}

) : (
    {peers.map((p) => (
  1. {p.rank} {p.country.flag} {p.country.name}
    {p.score != null ? fixed(p.score, 0) : t('common.na')}
  2. ))}
)}
) : (

{t('explore.similar.empty')}

)}
); }