<script lang="ts">
  import { onMount } from 'svelte';
  import { afterNavigate, disableScrollHandling } from '$app/navigation';
  import { initClickTracking, trackPageView } from '$lib/analytics';
  import '$lib/styles/variables.css';
  import '$lib/styles/base.css';
  import '$lib/styles/layout.css';
  import '$lib/styles/typography.css';
  import '$lib/styles/buttons.css';
  import '$lib/styles/forms.css';
  import '$lib/styles/sections.css';
  import '$lib/styles/responsive.css';

  onMount(() => initClickTracking());

  afterNavigate(({ from, to }) => {
    if (!from || !to || to.url.hash) {
      return;
    }

    if (from.url.pathname !== to.url.pathname) {
      disableScrollHandling();

      if (document.activeElement instanceof HTMLElement) {
        document.activeElement.blur();
      }

      requestAnimationFrame(() => {
        requestAnimationFrame(() => {
          window.scrollTo({ top: 0, left: 0, behavior: 'auto' });
          document.documentElement.scrollTop = 0;
          document.body.scrollTop = 0;
        });
      });

      trackPageView(to.url.pathname + to.url.search, document.title);
    }
  });
</script>

<slot />
