import type { Languages } from 'config/languages'
import { history } from 'config/history'

import { isSupportedLang } from 'helpers/isSupportedLang'

const KEY = 'lang'

export const getLanguageUrlParam = () => {
  const lang = new URLSearchParams(history.location.search).get(KEY)
  return lang && isSupportedLang(lang) ? lang : undefined
}

export const addLanguageUrlParam = (lang: Languages, url: string) => {
  const urlObject = new URL(url)
  urlObject.searchParams.set(KEY, lang)
  return urlObject.toString()
}
