<script setup lang="ts">
import {
    ContextMenu,
    ContextMenuTrigger,
    ContextMenuContent,
    ContextMenuItem,
} from '@/components/ui/context-menu';
import { deleteWarrenEntry } from '~/lib/api/warrens';
import type { DirectoryEntry } from '~/types';

const route = useRoute();
const warrenRoute = useWarrenRoute();

const { entry, disabled } = defineProps<{
    entry: DirectoryEntry;
    disabled: boolean;
}>();

const deleting = ref(false);

async function submitDelete() {
    deleting.value = true;

    await deleteWarrenEntry(warrenRoute.value, entry.name, entry.fileType);

    deleting.value = false;
}
</script>

<template>
    <ContextMenu>
        <ContextMenuTrigger>
            <NuxtLink
                :to="joinPaths(route.path, entry.name)"
                :class="['select-none', { 'pointer-events-none': disabled }]"
            >
                <Button
                    class="w-44 h-12"
                    variant="outline"
                    size="lg"
                    :disabled="disabled"
                >
                    <Icon :name="getFileIcon(entry.mimeType)" />
                    <span class="truncate">{{ entry.name }}</span>
                </Button>
            </NuxtLink>
        </ContextMenuTrigger>
        <ContextMenuContent>
            <ContextMenuItem @select="submitDelete">
                <Icon name="lucide:trash-2" />
                Delete
            </ContextMenuItem>
        </ContextMenuContent>
    </ContextMenu>
</template>
