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 TransportMode = {
	key: string;
	name: string;
};

export function createColumns(
	onSuccess: () => void,
	permissions: { canEdit: boolean; canDelete: boolean } = { canEdit: true, canDelete: true }
): ColumnDef<TransportMode>[] {
	return [		
		{
			accessorKey: "key",
			header: "Clave",
			cell: ({ row }) => {
				const keySnippet = createRawSnippet<[{ key: string }]>((getKey) => {
					const { key } = getKey();
					return {
						render: () =>
							`<code class="relative rounded bg-muted px-[0.3rem] py-[0.2rem] font-mono text-sm font-semibold">${key}</code>`
					};
				});
				return renderSnippet(keySnippet, { key: row.original.key });
			}
		},
		{
			accessorKey: "name",
			header: "Nombre",
			cell: ({ row }) => {
				const nameSnippet = createRawSnippet<[{ name: string }]>((getName) => {
					const { name } = getName();
					return {
						render: () => `<div class="max-w-[500px] truncate">${name}</div>`
					};
				});
				return renderSnippet(nameSnippet, { name: row.original.name });
			}
		},
		{
			id: "actions",
			cell: ({ row }) => {
				return renderComponent(DataTableActions, {
					item: row.original,
					onSuccess,
					canEdit: permissions.canEdit,
					canDelete: permissions.canDelete
				});
			}
		}
	];
}

// Legacy export removed
