/**
 * Definición de columnas para la tabla de Transportistas
 */
import type { Transporter } from '$lib/api/dashboard/a76/transporters';
import type { ColumnDef } from '@tanstack/table-core';
import { renderComponent } from '$lib/components/ui/data-table';
import DataTableActions from './data-table-actions.svelte';

export function createColumns(
    onSuccess: () => void,
    permissions: { canEdit: boolean; canDelete: boolean } = { canEdit: true, canDelete: true }
): ColumnDef<Transporter>[] {
    const cols: ColumnDef<Transporter>[] = [
        {
            accessorKey: 'transporter_key',
            header: 'Clave',
            cell: ({ row }) => {
                return row.original.transporter_key;
            }
        },
        {
            accessorKey: 'name',
            header: 'Nombre',
            cell: ({ row }) => {
                return row.original.name || '-';
            }
        },
        {
            accessorKey: 'short_name',
            header: 'Nombre Corto',
            cell: ({ row }) => {
                return row.original.short_name || '-';
            }
        },
        {
            accessorKey: 'rfc',
            header: 'RFC',
            cell: ({ row }) => {
                return row.original.rfc || '-';
            }
        },
        {
            accessorKey: 'caat_code',
            header: 'CAAT',
            cell: ({ row }) => {
                return row.original.caat_code || '-';
            }
        },
        {
            accessorKey: 'transport_code',
            header: 'Código Transporte',
            cell: ({ row }) => {
                return row.original.transport_code || '-';
            }
        }
    ];
    if (permissions.canEdit || permissions.canDelete) {
        cols.push({
            id: 'actions',
            header: 'Acciones',
            cell: ({ row }) => {
                return renderComponent(DataTableActions, {
                    item: row.original,
                    onSuccess,
                    canEdit: permissions.canEdit,
                    canDelete: permissions.canDelete
                });
            }
        });
    }
    return cols;
}
