<script lang="ts">
	import EllipsisIcon from "@lucide/svelte/icons/ellipsis";
	import { Button } from "$lib/components/ui/button/index.js";
	import * as DropdownMenu from "$lib/components/ui/dropdown-menu/index.js";
	import type { InvoiceType } from "./columns.js";
	import CreateEditDialog from "./create-edit-dialog.svelte";
	import DetailsDialog from "./details-dialog.svelte";
	import DeleteDialog from "./delete-dialog.svelte";

	let { 
		item,
		onSuccess
	}: { 
		item: InvoiceType;
		onSuccess?: () => void;
	} = $props();

	let showDetailsDialog = $state(false);
	let showEditDialog = $state(false);
	let showDeleteDialog = $state(false);

	function handleCopyId() {
		navigator.clipboard.writeText(item.key.toString());
	}

	function handleViewDetails() {
		showDetailsDialog = true;
	}

	function handleEdit() {
		showEditDialog = true;
	}

	function handleDelete() {
		showDeleteDialog = true;
	}
</script>

<DropdownMenu.Root>
	<DropdownMenu.Trigger>
		{#snippet child({ props })}
			<Button {...props} variant="ghost" size="icon" class="relative size-8 p-0">
				<span class="sr-only">Abrir menú</span>
				<EllipsisIcon class="size-4" />
			</Button>
		{/snippet}
	</DropdownMenu.Trigger>
	<DropdownMenu.Content align="end">
		<DropdownMenu.Group>
			<DropdownMenu.Label>Acciones</DropdownMenu.Label>
			<DropdownMenu.Item onclick={handleCopyId}>
				Copiar Clave
			</DropdownMenu.Item>
		</DropdownMenu.Group>
		<DropdownMenu.Separator />
		<DropdownMenu.Item onclick={handleViewDetails}>Ver detalles</DropdownMenu.Item>
		<DropdownMenu.Item onclick={handleEdit}>Editar</DropdownMenu.Item>
		<DropdownMenu.Separator />
		<DropdownMenu.Item class="text-destructive" onclick={handleDelete}>Eliminar</DropdownMenu.Item>
	</DropdownMenu.Content>
</DropdownMenu.Root>

<!-- Dialogs -->
<DetailsDialog bind:open={showDetailsDialog} {item} />
<CreateEditDialog bind:open={showEditDialog} item={item} {onSuccess} />
<DeleteDialog bind:open={showDeleteDialog} {item} {onSuccess} />
