<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useProjectCanvasStore } from '../stores/projectCanvas'
import Canvas from '../components/Canvas.vue'
import ProjectCanvasPage from './ProjectCanvasPage.vue'

const projectCanvasStore = useProjectCanvasStore()
const loading = ref(true)
const showDefaultCanvas = ref(false)
const defaultCanvasId = ref<string | null>(null)

onMounted(async () => {
  const defaultCanvas = await projectCanvasStore.fetchDefaultCanvas()

  if (defaultCanvas) {
    showDefaultCanvas.value = true
    defaultCanvasId.value = defaultCanvas.id
  } else {
    showDefaultCanvas.value = false
  }

  loading.value = false
})
</script>

<template>
  <div v-if="loading" class="home-loading">
    <div class="spinner"></div>
  </div>

  <ProjectCanvasPage
    v-else-if="showDefaultCanvas && defaultCanvasId"
    :id="defaultCanvasId"
  />

  <Canvas v-else />
</template>

<style scoped>
.home-loading {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-primary);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border-color);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}
</style>
