export default defineNuxtRouteMiddleware((to, from) => {
  // important: routes do not exist in middleware, never (in)directly use e.g. useRoute

  const {getLocaleVariant, buildPrefix} = useContentPrefix()
  const localeVariant = getLocaleVariant(to.path)

  if (to.matched.length && localeVariant) return

  if (!to.matched.length && localeVariant) {
    const [locale, variant] = localeVariant
    return navigateTo(buildPrefix(locale, variant))
  }

  const {preferredLocale, preferredVariant} = useContentPreference()
  return navigateTo(buildPrefix(preferredLocale.value, preferredVariant.value))
})
