import type { Product } from '@/types'; import type { AppLocale } from './config'; type Translate = (key: string) => string; const verifiedServices: Record = { 34: 'Greece', 35: 'Netherlands', 36: 'Denmark' }; // Localize presentation only. Routing, prices, form schema IDs and submitted enum values remain API-owned. export function displayProduct(product: T, t: Translate): T { if (verifiedServices[product.id] !== product.country || product.city !== 'Dublin' || product.visa_type !== 'Tourist') return product; const tutorial = product.extra_fields?.tutorial; const card = product.extra_fields?.card_config; return { ...product, title: t(product.title), description: product.description ? t(product.description) : product.description, extra_fields: { ...product.extra_fields, ...(card && { card_config: { ...card, poster_title: card.poster_title ? t(card.poster_title) : card.poster_title, poster_tag: card.poster_tag ? t(card.poster_tag) : card.poster_tag, poster_sub: card.poster_sub ? t(card.poster_sub) : card.poster_sub, poster_highlight: card.poster_highlight ? t(card.poster_highlight) : card.poster_highlight, status_tag: card.status_tag ? t(card.status_tag) : card.status_tag, } }), ...(tutorial && { tutorial: { ...tutorial, modal_title: tutorial.modal_title ? t(tutorial.modal_title) : undefined, button_text: tutorial.button_text ? t(tutorial.button_text) : undefined, badge_text: tutorial.badge_text ? t(tutorial.badge_text) : undefined, summary: tutorial.summary ? t(tutorial.summary) : undefined, content: tutorial.content ? t(tutorial.content) : undefined, link_text: tutorial.link_text ? t(tutorial.link_text) : undefined, steps: tutorial.steps?.map(step => ({ ...step, title: t(step.title), desc: t(step.desc) })), } }), }, }; } const normalizeCountry = (name: string) => name.toLowerCase().replace(/[^a-z0-9]/g, ''); let englishRegionCodes: Map | undefined; const displayNames = new Map(); export function countryLabel(value: string, locale: AppLocale) { if (!value) return value; if (!englishRegionCodes) { englishRegionCodes = new Map(); const english = new Intl.DisplayNames(['en'], { type: 'region' }); for (let first = 65; first <= 90; first++) for (let second = 65; second <= 90; second++) { const code = String.fromCharCode(first, second); const name = english.of(code); if (name && name !== code) englishRegionCodes.set(normalizeCountry(name), code); } for (const [alias, code] of Object.entries({ 'Czech Republic': 'CZ', 'Congo (Brazzaville)': 'CG', 'Congo (Kinshasa)': 'CD', 'Burma': 'MM', 'Swaziland': 'SZ', 'Turkey': 'TR', 'Vatican City': 'VA' })) englishRegionCodes.set(normalizeCountry(alias), code); } const code = englishRegionCodes.get(normalizeCountry(value)); if (!code) return value; if (!displayNames.has(locale)) displayNames.set(locale, new Intl.DisplayNames([locale], { type: 'region' })); return displayNames.get(locale)?.of(code) || value; }