const PREFIX = 'Amidala: '

export const TRANSLATIONS = new Set([
  'All',
  'Back to top',
  'Buy now',
  'Click to sort',
  'Category',
  'Close',
  'Description',
  'Dismiss',
  'Enter your search term',
  'Features',
  'Go to: {0}',
  'Load more',
  'Next',
  'Next slide',
  'No results found.',
  'Pause playback',
  'Previous',
  'Previous slide',
  'Read more',
  'Reset filters',
  'Resume playback',
  'Search',
  'Skip to main content',
  'Sorted ascending - {0}',
  'Sorted descending - {0}',
  'Submit',
  'View product list',
  'Viscosity',
  '{0} results found',
  'Download on the {0}',
  'Get it on {0}',
  'Scan the QR code to download the app or click the link below'
].map(key => `${PREFIX}${key}`))

export const i18n = {}

export class TranslationError extends Error {
  constructor (key) {
    super(`Unknown dictionary key: ${key}`)
  }
}

export default function t (text, ...args) {
  const key = PREFIX + text

  if (!TRANSLATIONS.has(key)) {
    throw new TranslationError(key)
  }

  let result = i18n[key] ?? text

  for (const [ii, arg] of args.entries()) {
    result = result.replaceAll(`{${ii}}`, arg)
  }

  return result
}
