import isNull from 'lodash/isNull'

import { history } from 'config/history'
import { client } from 'config/clients'

const KEY = 'lang'

export const getLanguageUrlParam = () => {
  const lang = new URLSearchParams(history.location.search).get(KEY)
  return (lang !== 'null' && !isNull(lang)) ? lang : client.defaultLanguage
}

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