<script lang="ts">
  import '@fontsource/inter/latin-400.css';
  import '@fontsource/inter/latin-500.css';
  import '@fontsource/inter/latin-600.css';
  import '@fontsource/inter/latin-700.css';
  import '$lib/styles/theme.css';
  import '$lib/theme';
  import { beforeNavigate, afterNavigate } from '$app/navigation';
  import { page } from '$app/state';
  import AppShell from '$lib/components/AppShell.svelte';
  import CustomerPortalShell from '$lib/components/CustomerPortalShell.svelte';
  import Toast from '$lib/components/Toast.svelte';
  import { clientSession } from '$lib/session';
  import { isCustomerPortalSession } from '$lib/workspace-access';
  import { toast } from '$lib/toast';

  let { children } = $props();

  const isPrintableRoute = $derived(page.url.pathname.startsWith('/mix-calculator/') && page.url.pathname.endsWith('/print'));
  // B2B ordering customers get the dedicated, stripped-down portal shell;
  // internal staff and costing-portal users keep the full workspace shell.
  const isCustomerPortal = $derived(isCustomerPortalSession($clientSession));

  let navToastId: string | null = null;
  let navTimer: ReturnType<typeof setTimeout> | null = null;

  beforeNavigate(() => {
    navTimer = setTimeout(() => {
      navToastId = toast.loading('Loading…');
      navTimer = null;
    }, 150);
  });

  afterNavigate(() => {
    if (navTimer !== null) {
      clearTimeout(navTimer);
      navTimer = null;
    }
    if (navToastId) {
      toast.dismiss(navToastId);
      navToastId = null;
    }
  });
</script>

{#if isPrintableRoute}
  {@render children()}
{:else if isCustomerPortal}
  <CustomerPortalShell>
    {@render children()}
  </CustomerPortalShell>
{:else}
  <AppShell>
    {@render children()}
  </AppShell>
{/if}

<Toast />

