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 CurrencyType = {
	code: string;
	currency_name: string;
	country_description: string;
};

export function createColumns(
	onSuccess: () => void,
	permissions: { canEdit: boolean; canDelete: boolean } = { canEdit: true, canDelete: true }
): ColumnDef<CurrencyType>[] {
	return [		
		{
			accessorKey: "code",
			header: "Código",
			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.code });
			}
		},
		{
			accessorKey: "currency_name",
			header: "Nombre de Moneda",
			cell: ({ row }) => {
				const nameSnippet = createRawSnippet<[{ name: string }]>((getName) => {
					const { name } = getName();
					return {
						render: () => `<div class="max-w-[200px] truncate font-medium">${name}</div>`
					};
				});
				return renderSnippet(nameSnippet, { name: row.original.currency_name });
			}
		},
		{
			accessorKey: "country_description",
			header: "País / Descripción",
			cell: ({ row }) => {
				const descSnippet = createRawSnippet<[{ description: string }]>((getDesc) => {
					const { description } = getDesc();
					return {
						render: () => `<div class="max-w-[300px] truncate">${description}</div>`
					};
				});
				return renderSnippet(descSnippet, { description: row.original.country_description });
			}
		},
		{
			id: "actions",
			cell: ({ row }) => {
				return renderComponent(DataTableActions, {
					item: row.original,
					onSuccess,
					canEdit: permissions.canEdit,
					canDelete: permissions.canDelete
				});
			}
		}
	];
}

// Legacy export removed
