{{ define "layouts/fragments/topbar" }}
    <nav class="mx-auto space-x-4 px-6 py-2">
        <div class="flex justify-between p-0 items-center">
            <div id="left-items">
              <a href="/" hx-boost="true" class="text-2xl no-underline hover:no-underline flex items-center gap-2">
                {{ template "fragments/logotypeSmall" }}
              </a>
            </div>

            <div id="right-items" class="flex items-center gap-4">
                {{ with .LoggedInUser }}
                    {{ block "newButton" . }} {{ end }}
                    {{ template "notifications/fragments/bell" }}
                    {{ block "searchButton" . }} {{ end }}
                    {{ block "profileDropdown" . }} {{ end }}
                {{ else }}
                    <a href="/login">login</a>
                    <span class="text-gray-500 dark:text-gray-400">or</span>
                    <a href="/signup" class="btn-create py-0 hover:no-underline hover:text-white flex items-center gap-2">
                      join now {{ i "arrow-right" "size-4" }}
                    </a>
                {{ end }}
            </div>
        </div>
    </nav>
{{ end }}

{{ define "newButton" }}
<details class="relative inline-block text-left nav-dropdown">
    <summary class="btn-create py-0 cursor-pointer list-none flex items-center gap-2">
      {{ i "plus" "w-4 h-4" }} <span class="hidden md:inline">new</span>
    </summary>
    <div class="absolute flex flex-col right-0 mt-3 p-4 rounded w-48 bg-white dark:bg-gray-800 dark:text-white border border-gray-200 dark:border-gray-700">
        <a href="/repo/new" class="flex items-center gap-2">
          {{ i "book-plus" "w-4 h-4" }}
          new repository
        </a>
        <a href="/strings/new" class="flex items-center gap-2">
          {{ i "line-squiggle" "w-4 h-4" }}
          new string
        </a>
    </div>
</details>
{{ end }}

{{ define "searchButton" }}
<a href="/search">{{ i "search" "size-5 text-gray-500 dark:text-gray-400" }}</a>
{{ end }}

{{ define "profileDropdown" }}
<details class="relative inline-block text-left nav-dropdown">
    <summary class="cursor-pointer list-none flex items-center gap-1">
        {{ $user := .Active.Did }}
        {{ template "user/fragments/pic" (list $user "size-6") }}
        <span class="hidden md:inline">{{ $user | resolve | truncateAt30 }}</span>
    </summary>
    <div class="absolute right-0 mt-4 rounded bg-white dark:bg-gray-800 dark:text-white border border-gray-200 dark:border-gray-700 shadow-lg z-50 text-sm" style="width: 14rem;">
        {{ $active := .Active.Did }}
        {{ $linkStyle := "flex items-center gap-3 px-4 py-2 hover:no-underline hover:bg-gray-50 hover:dark:bg-gray-700/50" }}

        {{ $others := .Accounts | otherAccounts $active }}
        {{ if $others }}
          <div class="text-sm text-gray-500 dark:text-gray-400 px-3 py-1 pt-2">switch account</div>
          {{ range $others }}
            <button
                type="button"
                hx-post="/account/switch"
                hx-vals='{"did": "{{ .Did }}"}'
                hx-swap="none"
                class="{{$linkStyle}} w-full text-left pl-3"
            >
                {{ template "user/fragments/pic" (list .Did "size-6") }}
                <span class="truncate flex-1">{{ .Did | resolve }}</span>
            </button>
          {{ end }}
        {{ end }}

        <a href="/login?mode=add_account" class="{{$linkStyle}} pl-3">
          <div class="size-6 rounded-full bg-gray-100 dark:bg-gray-700 flex items-center justify-center">
            {{ i "plus" "size-3" }}
          </div>

          <div class="text-left flex-1 min-w-0 block truncate">
            add account
          </div>
        </a>

        <div class="border-t border-gray-200 dark:border-gray-700">
            <a href="/{{ $active }}" class="{{$linkStyle}}">
              {{ i "user" "size-4" }}
              profile
            </a>
            <a href="/{{ $active }}?tab=repos" class="{{$linkStyle}}">
              {{ i "book-marked" "size-4" }}
              repositories
            </a>
            <a href="/{{ $active }}?tab=strings" class="{{$linkStyle}}">
              {{ i "line-squiggle" "size-4" }}
              strings
            </a>
            <a href="/settings" class="{{$linkStyle}}">
              {{ i "cog" "size-4" }}
              settings
            </a>
            <a href="#"
               hx-post="/logout"
               hx-swap="none"
               class="{{$linkStyle}} text-red-400 hover:text-red-400 hover:bg-red-100 dark:hover:bg-red-700/20 pb-2">
              {{ i "log-out" "size-4" }}
              logout
            </a>
        </div>
    </div>
</details>

<script>
document.addEventListener('click', function(event) {
    const dropdowns = document.querySelectorAll('.nav-dropdown');
    dropdowns.forEach(function(dropdown) {
        if (!dropdown.contains(event.target)) {
            dropdown.removeAttribute('open');
        }
    });
});
</script>
{{ end }}
