import type { ColumnDef } from "@tanstack/table-core";
import { renderComponent, renderSnippet } from "$lib/components/ui/data-table/index.js";
import { createRawSnippet } from "svelte";
import DataTableActions from "./data-table-actions.svelte";

export type CustomsWarehouse = {
	key: string;
	customs: string;
	fiscalized_warehouse: string;
};

export function createColumns(
	onSuccess?: () => void,
	{ canEdit = true, canDelete = true }: { canEdit?: boolean; canDelete?: boolean } = {}
): ColumnDef<CustomsWarehouse>[] {
	return [		
		{
			accessorKey: "key",
			header: "Clave",
			cell: ({ row }) => {
				const codeSnippet = createRawSnippet<[{ code: string }]>((getCode) => {
					const { code } = getCode();
					return {
						render: () =>
							`<code class="relative rounded bg-muted px-[0.3rem] py-[0.2rem] font-mono text-sm">${code}</code>`
					};
				});
				return renderSnippet(codeSnippet, { code: row.original.key });
			}
		},
		{
			accessorKey: "customs",
			header: "Aduana",
			cell: ({ row }) => {
				const customsSnippet = createRawSnippet<[{ customs: string }]>((getCustoms) => {
					const { customs } = getCustoms();
					return {
						render: () => `<div class="max-w-[250px] truncate">${customs}</div>`
					};
				});
				return renderSnippet(customsSnippet, { customs: row.original.customs });
			}
		},
		{
			accessorKey: "fiscalized_warehouse",
			header: "Recinto Fiscalizado",
			cell: ({ row }) => {
				const warehouseSnippet = createRawSnippet<[{ warehouse: string }]>((getWarehouse) => {
					const { warehouse } = getWarehouse();
					return {
						render: () => `<div class="max-w-[400px] truncate">${warehouse}</div>`
					};
				});
				return renderSnippet(warehouseSnippet, { warehouse: row.original.fiscalized_warehouse });
			}
		},
		{
			id: "actions",
			cell: ({ row }) => {
				return renderComponent(DataTableActions, { 
					item: row.original, 
					onSuccess,
					canEdit,
					canDelete
				});
			}
		}
	];
}

// Mantener compatibilidad hacia atrás
export const columns = createColumns();
