---
import BaseLayout from '@layouts/Base.astro';
import Main from '../../web-check-live/main.tsx';
import '../../web-check-live/styles/index.css';

export const prerender = false;

const { pathname, search } = new URL(Astro.request.url);

const searchUrl = new URLSearchParams(search).get('url');

if (searchUrl) {
  Astro.redirect(`/check/${encodeURIComponent(searchUrl)}`);
}

---

<BaseLayout>
  <Main {pathname} client:load />
</BaseLayout>

<script>
  // Fallback, if Astro hasn't initialized the RC comp yet, then check the url
  // And if form has been submitted with ?url=, redirect to the results page
  const searchParams = new URL(window.location.href).searchParams;
  if (searchParams.has('url')) {
    window.location.href = `/check/${encodeURIComponent(searchParams.get('url') || '')}`;
  }

  // And add a manual no-react form submit handler
  const form = document.querySelector<HTMLFormElement>('form');
  if (form) {
    form.addEventListener('submit', function(event: Event) {
      event.preventDefault();
      const input = (this as HTMLFormElement).querySelector<HTMLInputElement>('input[name="url"]');  
      if (input && input.value) {
        window.location.href = `/check/${encodeURIComponent(input.value)}`;
      }
    });
  }

</script>
