<script lang="ts">
	import * as AlertDialog from '$lib/components/ui/alert-dialog';
	import { CircleAlert } from 'lucide-svelte';
	import { m } from '$lib/i18n/messages';

	let {
		open = $bindable(false),
		agentsCount = 0,
		clientsCount = 0,
		onAccept,
		onCancel
	}: {
		open?: boolean;
		agentsCount?: number;
		clientsCount?: number;
		onAccept?: () => void;
		onCancel?: () => void;
	} = $props();

	const showModal = $derived(agentsCount === 0 || clientsCount === 0);

	const message = $derived(
		agentsCount === 0 && clientsCount === 0
			? m['invoice_item_fa.prerequisites.message_both']()
			: agentsCount === 0
				? m['invoice_item_fa.prerequisites.message_agents']()
				: m['invoice_item_fa.prerequisites.message_clients']()
	);

	function handleOpenChange(newOpen: boolean) {
		open = newOpen;
	}

	function handleCancel() {
		onCancel?.();
	}

	function handleAccept() {
		onAccept?.();
	}
</script>

<AlertDialog.Root bind:open onOpenChange={handleOpenChange}>
	<AlertDialog.Content>
		<AlertDialog.Header>
			<div class="flex items-center gap-3">
				<CircleAlert class="h-6 w-6 shrink-0 text-amber-500" />
				<AlertDialog.Title>{m['invoice_item_fa.prerequisites.title']()}</AlertDialog.Title>
			</div>
			<AlertDialog.Description class="space-y-3 pt-1">
				<p>{message}</p>
				<p class="text-sm text-muted-foreground">
					{m['invoice_item_fa.prerequisites.register_hint']()}
					<a
						href="/dashboard/customs_brokers"
						class="font-medium text-primary underline underline-offset-4 hover:no-underline"
					>
						{m['invoice_item_fa.prerequisites.agents_link']()}
					</a>
					 {m['invoice_item_fa.prerequisites.and']()}
					<a
						href="/dashboard/clients_and_providers"
						class="font-medium text-primary underline underline-offset-4 hover:no-underline"
					>
						{m['invoice_item_fa.prerequisites.clients_link']()}
					</a>.
				</p>
			</AlertDialog.Description>
		</AlertDialog.Header>
		<AlertDialog.Footer>
			<AlertDialog.Cancel onclick={handleCancel}>{m['invoice_item_fa.prerequisites.cancel']()}</AlertDialog.Cancel>
			<AlertDialog.Action onclick={handleAccept}>{m['invoice_item_fa.prerequisites.accept']()}</AlertDialog.Action>
		</AlertDialog.Footer>
	</AlertDialog.Content>
</AlertDialog.Root>
