<script setup lang="ts">
/**
 * Loading overlay component.
 *
 * Displays a full-screen loading overlay when the UI store's isLoading
 * is true. Supports stacked loading calls - the overlay stays visible
 * until all showLoading() calls are balanced with hideLoading().
 */
import { useUiStore } from '@/stores/ui'

const ui = useUiStore()
</script>

<template>
  <Teleport to="body">
    <Transition name="fade">
      <div
        v-if="ui.isLoading"
        class="fixed inset-0 z-50 flex items-center justify-center bg-gray-900/80 backdrop-blur-sm"
      >
        <div class="flex flex-col items-center gap-4">
          <!-- Spinner -->
          <div class="relative">
            <div
              class="w-12 h-12 border-4 border-primary/30 rounded-full"
            />
            <div
              class="absolute inset-0 w-12 h-12 border-4 border-primary border-t-transparent rounded-full animate-spin"
            />
          </div>

          <!-- Optional message -->
          <p
            v-if="ui.loadingMessage"
            class="text-gray-300 text-sm"
          >
            {{ ui.loadingMessage }}
          </p>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<style scoped>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
