<script>
  import { setContext } from 'svelte';
  import { onNavigate } from '$app/navigation';
  import '../app.css';
  import logo from '$lib/assets/logo.png';

  import { AppBar } from '@skeletonlabs/skeleton';
  import { currentUser } from '$lib/store';

  let pageContext = $state({
    showMenu: false
  });
  setContext('page', pageContext);

  function toggleMenu(event) {
    event.preventDefault();
    pageContext.showMenu = !pageContext.showMenu;
  }

  onNavigate(() => {
    pageContext.showMenu = false;
  });

  let { children } = $props();
</script>

<AppBar class="app-bar">
  <svelte:fragment slot="lead">
    <button onclick={toggleMenu} class="cursor-pointer">
      <img class="w-8 h-8" alt="logo" src={logo} />
    </button>
  </svelte:fragment>
  <a href="/">pilcrow</a>
  <svelte:fragment slot="trail">
    {#if $currentUser}
      <div class="rounded-full bg-secondary-400 px-3 py-1">
        <a href="/me">@{$currentUser.username}</a>
      </div>
    {/if}
  </svelte:fragment>
</AppBar>

{@render children?.()}
