<script lang="ts">
  import '../app.css';
  import { sessionManager } from '../lib/services/auth/session-manager.js';
  import { onMount } from 'svelte';
  import { browser } from '$app/environment';

  // Restore session immediately if in browser (before onMount)
  if (browser) {
    // Try to restore session synchronously if possible
    // This ensures session is restored before any components render
    (async () => {
      try {
        if (!sessionManager.isLoggedIn()) {
          await sessionManager.restoreSession();
        }
      } catch (error) {
        console.error('Failed to restore session:', error);
      }
    })();
  }

  // Also restore in onMount as fallback
  onMount(async () => {
    try {
      // Only restore if there's no active session
      // This prevents overwriting sessions that were just created during login
      if (!sessionManager.isLoggedIn()) {
        await sessionManager.restoreSession();
      }
    } catch (error) {
      console.error('Failed to restore session:', error);
    }
  });
</script>

<slot />
