<script setup lang="ts">
import type { Navigation } from '~/types/api';

interface Props {
    link: Navigation;
    project?: number;
}

const props = defineProps<Props>();
const hasChildren = computed(() => {
    return props.link && props.link.children && props.link.children.length > 0 || false;
});

const collapsed = ref(!unifySlug(useRoute().params.slug).startsWith(props.link.path));

const emit = defineEmits(['navigate']);
</script>

<template>
    <div class="py-1" v-if="project && !isNaN(project)">
        <template v-if="hasChildren">
            <div class="flex gap-2 items-center cursor-pointer hover:text-opacity-75 text-light-100 dark:text-dark-100" @click="collapsed = hasChildren && !collapsed" :data-type="link.private ? 'privé' : undefined">
                <div v-if="hasChildren" class="w-4 h-4 transition-transform" :class="{'-rotate-90': collapsed}">
                    <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" width="24" height="24" viewBox="0 0 24 24" fill="none"
                        stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M3 8L12 17L21 8"></path>
                    </svg>
                </div>
                <div class="font-semibold xl:text-base text-sm">{{ link.title }}</div>
            </div>
            <div v-if="!collapsed">
                <NavigationLink @navigate="e => emit('navigate')" class="border-light-40 dark:border-dark-40 ms-2 ps-4 border-l" v-if="hasChildren" v-for="l of link.children" :link="l" :project="project" />
            </div>
        </template>
        <NuxtLink v-else @click="e => emit('navigate')" class="text-light-100 dark:text-dark-100 cursor-pointer hover:text-opacity-75 xl:text-base text-sm" :to="{ path: `/explorer/${project}/${link.path}`, force: true }"
            active-class="!text-accent-blue relative before:border-l-2 before:border-accent-blue before:absolute before:-top-1 before:-bottom-1 before:-left-4 before:-mx-px" :data-type="(link.type === 'Canvas' ? 'graph' : (link.private ? 'privé' : undefined))">
            <div class="">{{ link.title }}</div>
        </NuxtLink>
    </div>
</template>