<script setup lang="ts">
import {
    ContextMenu,
    ContextMenuTrigger,
    ContextMenuContent,
    ContextMenuItem,
} from '@/components/ui/context-menu';
import { deleteWarrenDirectory, deleteWarrenFile } 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;

    if (entry.fileType === 'directory') {
        await deleteWarrenDirectory(warrenRoute.value, entry.name);
    } else {
        await deleteWarrenFile(warrenRoute.value, entry.name);
    }

    deleting.value = false;
}
</script>

<template>
    <ContextMenu>
        <ContextMenuTrigger>
            <NuxtLink
                :to="joinPaths(route.path, entry.name)"
                :class="[
                    'select-none',
                    {
                        'pointer-events-none':
                            disabled || entry.fileType === 'file',
                    },
                ]"
            >
                <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>
