<script lang="ts" setup>
import { ref } from 'vue'

import type { Team } from './types'

const { teams } = defineProps<{
  teams: Team[]
}>()

const activeTeam = ref<Team>(teams[0])

function getAssetUrl(path: string) {
  if (!path)
    return ''
  if (path.startsWith('http'))
    return path
  return `${import.meta.env.VITE_API_BASE || 'http://localhost:8080'}${path}`
}
</script>

<template>
  <UiSidebarMenu>
    <UiSidebarMenuItem>
      <UiSidebarMenuButton size="lg" class="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground">
        <div
          class="flex items-center justify-center rounded-lg aspect-square size-8 overflow-hidden shrink-0"
          :class="typeof activeTeam.logo === 'string' && activeTeam.logo ? '' : 'bg-sidebar-primary text-sidebar-primary-foreground'"
        >
          <img v-if="typeof activeTeam.logo === 'string' && activeTeam.logo" :src="getAssetUrl(activeTeam.logo)" class="size-full object-contain" alt="" />
          <component v-else-if="typeof activeTeam.logo === 'function'" :is="activeTeam.logo" class="size-4" />
        </div>
        <div class="grid flex-1 text-sm leading-tight group-data-[collapsible=icon]:hidden">
          <span class="font-semibold truncate">{{ activeTeam.name }}</span>
        </div>
      </UiSidebarMenuButton>
    </UiSidebarMenuItem>
  </UiSidebarMenu>
</template>
