---
import { defaultLang, showDefaultLang } from '../i18n/ui';

// Always redirect to a specific language route, not a dynamic path
// This ensures we don't get into redirect loops
const redirectPath = `/${defaultLang}/`;

// Add a timestamp parameter to prevent caching of redirects which can cause loops
const timestamp = Date.now();
const redirectUrl = `${redirectPath}?t=${timestamp}`;
---

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="refresh" content={`0;url=${redirectUrl}`} />
    <script>
      // Prevent infinite redirects on the client side
      if (typeof window !== 'undefined') {
        const redirectCount = parseInt(sessionStorage.getItem('redirectCount') || '0', 10);
        if (redirectCount > 3) {
          console.warn('Too many redirects detected, stopping to prevent infinite loop');
          sessionStorage.setItem('redirectCount', '0');
          // Cancel the meta refresh redirect
          const metaRefresh = document.querySelector('meta[http-equiv="refresh"]');
          if (metaRefresh) {
            metaRefresh.remove();
          }
        } else {
          sessionStorage.setItem('redirectCount', (redirectCount + 1).toString());
        }
      }
    </script>
    <title>Redirecting...</title>
  </head>
  <body>
    <p>Redirecting to <a href={redirectUrl}>the homepage</a>...</p>
  </body>
</html>
