const lithuanianMap: Record<string, string> = {
  ą: 'a',
  č: 'c',
  ę: 'e',
  ė: 'e',
  į: 'i',
  š: 's',
  ų: 'u',
  ū: 'u',
  ž: 'z',
};

export type SlugCar = {
  make: string;
  model: string;
  year: number;
  city: string;
};

export const groupNumber = (value: number) => new Intl.NumberFormat('lt-LT').format(value).replace(/\u00a0/g, ' ');

export const formatPrice = (price: number) => `${groupNumber(price)}\u00a0€`;

export const formatMileage = (mileage: number) => `${groupNumber(mileage)} km`;

export const formatPower = (kw: number) => `${kw} kW (${Math.round(kw * 1.35962)} AG)`;

export const formatEngineSize = (liters: number) => `${liters.toFixed(1).replace('.', ',')} l`;

export const slugify = (value: string) =>
  value
    .trim()
    .toLowerCase()
    .replace(/[ąčęėįšųūž]/g, (letter) => lithuanianMap[letter] ?? letter)
    .normalize('NFD')
    .replace(/[\u0300-\u036f]/g, '')
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-+|-+$/g, '');

export const slugifyCar = (car: SlugCar) => slugify(`${car.make}-${car.model}-${car.year}-${car.city}`);

export const stripMarkdown = (value: string) =>
  value
    .replace(/```[\s\S]*?```/g, ' ')
    .replace(/`([^`]+)`/g, '$1')
    .replace(/!\[[^\]]*\]\([^)]*\)/g, ' ')
    .replace(/\[([^\]]+)\]\([^)]*\)/g, '$1')
    .replace(/[*_>#~-]/g, '')
    .replace(/\s+/g, ' ')
    .trim();

export const excerpt = (value: string, limit = 155) => stripMarkdown(value).slice(0, limit);
