catalog.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import type { Product } from '@/types';
  2. import type { AppLocale } from './config';
  3. type Translate = (key: string) => string;
  4. const verifiedServices: Record<number, string> = { 34: 'Greece', 35: 'Netherlands', 36: 'Denmark' };
  5. // Localize presentation only. Routing, prices, form schema IDs and submitted enum values remain API-owned.
  6. export function displayProduct<T extends Product>(product: T, t: Translate): T {
  7. if (verifiedServices[product.id] !== product.country || product.city !== 'Dublin' || product.visa_type !== 'Tourist') return product;
  8. const tutorial = product.extra_fields?.tutorial;
  9. const card = product.extra_fields?.card_config;
  10. return {
  11. ...product,
  12. title: t(product.title),
  13. description: product.description ? t(product.description) : product.description,
  14. extra_fields: {
  15. ...product.extra_fields,
  16. ...(card && { card_config: {
  17. ...card,
  18. poster_title: card.poster_title ? t(card.poster_title) : card.poster_title,
  19. poster_tag: card.poster_tag ? t(card.poster_tag) : card.poster_tag,
  20. poster_sub: card.poster_sub ? t(card.poster_sub) : card.poster_sub,
  21. poster_highlight: card.poster_highlight ? t(card.poster_highlight) : card.poster_highlight,
  22. status_tag: card.status_tag ? t(card.status_tag) : card.status_tag,
  23. } }),
  24. ...(tutorial && { tutorial: {
  25. ...tutorial,
  26. modal_title: tutorial.modal_title ? t(tutorial.modal_title) : undefined,
  27. button_text: tutorial.button_text ? t(tutorial.button_text) : undefined,
  28. badge_text: tutorial.badge_text ? t(tutorial.badge_text) : undefined,
  29. summary: tutorial.summary ? t(tutorial.summary) : undefined,
  30. content: tutorial.content ? t(tutorial.content) : undefined,
  31. link_text: tutorial.link_text ? t(tutorial.link_text) : undefined,
  32. steps: tutorial.steps?.map(step => ({ ...step, title: t(step.title), desc: t(step.desc) })),
  33. } }),
  34. },
  35. };
  36. }
  37. const normalizeCountry = (name: string) => name.toLowerCase().replace(/[^a-z0-9]/g, '');
  38. let englishRegionCodes: Map<string, string> | undefined;
  39. const displayNames = new Map<AppLocale, Intl.DisplayNames>();
  40. export function countryLabel(value: string, locale: AppLocale) {
  41. if (!value) return value;
  42. if (!englishRegionCodes) {
  43. englishRegionCodes = new Map();
  44. const english = new Intl.DisplayNames(['en'], { type: 'region' });
  45. for (let first = 65; first <= 90; first++) for (let second = 65; second <= 90; second++) {
  46. const code = String.fromCharCode(first, second);
  47. const name = english.of(code);
  48. if (name && name !== code) englishRegionCodes.set(normalizeCountry(name), code);
  49. }
  50. 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);
  51. }
  52. const code = englishRegionCodes.get(normalizeCountry(value));
  53. if (!code) return value;
  54. if (!displayNames.has(locale)) displayNames.set(locale, new Intl.DisplayNames([locale], { type: 'region' }));
  55. return displayNames.get(locale)?.of(code) || value;
  56. }