<script setup lang="ts">
import { addNote, setActiveNote, rootNote } from '@/composables/useNotes'
import { user } from '@/composables/useFirebase'
import { initialized } from '@/composables/useFirebase'

const loading = inject<boolean>('loading')

const props = defineProps<{
  sideBarCollapsed: boolean
}>()

const emit = defineEmits<{
  toggleSideBar: []
}>()

const searchActive = ref<boolean>(false)

const authUI: any = inject('firebaseAuthUI')
const authModalInitialStateOpen = ref<boolean>(authUI.isPendingRedirect())
</script>
<template>
  <div
    class="fixed left-0 right-0 top-0 z-[500] flex h-[50px] bg-primary"
    :class="searchActive && 'search-active'"
  >
    <div class="mx-auto flex w-full max-w-app items-center py-2.5 text-white">
      <div
        class="search-active-hide flex items-center pl-3"
        :class="sideBarCollapsed ? 'w-fit' : 'max-sm:w-fit md:w-sidebar md:pr-3'"
      >
        <Hamburger
          :side-bar-collapsed="props.sideBarCollapsed"
          @toggle-side-bar="emit('toggleSideBar')"
        />
        <Logo
          class="ml-auto pl-5 text-2xl hover:drop-shadow"
          id="logo"
          @click="setActiveNote(rootNote?.id)"
        />
      </div>
      <div class="flex h-full flex-grow flex-row items-center gap-2 pl-5 pr-3">
        <template v-if="!loading">
          <SearchBar @active="(active) => (searchActive = active)" />
          <button
            class="search-active-hide btn-outline btn-sm btn py-1 text-white"
            @click="addNote('Untitled new note', '', true)"
          >
            <i class="fas fa-plus-circle text-[1.1rem]" />
          </button>
          <Modal v-if="initialized && !user" :open="authModalInitialStateOpen">
            <template #activator="{ open }">
              <button
                class="search-active-hide btn-outline btn-sm btn py-1 text-white"
                @click="open"
              >
                Sign in
              </button>
            </template>
            <template #title>Sign in</template>
            <template #default="{ close }">
              <Auth @signedIn="close" />
            </template>
            <template #actions="{ close }">
              <button class="btn-sm btn" @click="close">Close</button>
            </template>
          </Modal>
          <Settings v-else-if="user" />
        </template>
        <SkeletonTopBar v-else />
      </div>
    </div>
  </div>
</template>
<style lang="scss">
#logo {
  @apply cursor-pointer transition-all duration-200 hover:text-primary;
}
#logo:hover {
  text-shadow: 0 0 5px white, 0 0 10px white, 0 0 15px white;
}
.btn-outline {
  @apply hover:border-white hover:bg-white hover:text-primary focus-visible:outline-white;
}

.search-active {
  .search-active-hide {
    @apply max-sm:hidden;
  }
}
</style>
