/**
 * Definición de columnas para la tabla de Trailers
 */
import type { Trailer } from '$lib/api/dashboard/a76/trailers';
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<Trailer>[] {
    const cols: ColumnDef<Trailer>[] = [
        {
            accessorKey: 'trailer_number',
            header: 'Número de Trailer',
            cell: ({ row }) => {
                return row.original.trailer_number;
            }
        },
        {
            accessorKey: 'plate_number',
            header: 'Placas',
            cell: ({ row }) => {
                return row.original.plate_number || '-';
            }
        },
        {
            accessorKey: 'trailer_type_key',
            header: 'Tipo de Trailer',
            cell: ({ row }) => {
                return row.original.trailer_type_key || '-';
            }
        },
        {
            accessorKey: 'container_key',
            header: 'Contenedor',
            cell: ({ row }) => {
                return row.original.container_key || '-';
            }
        },
        {
            accessorKey: 'state',
            header: 'Estado',
            cell: ({ row }) => {
                return row.original.state || '-';
            }
        },
        {
            accessorKey: 'country',
            header: 'País',
            cell: ({ row }) => {
                return row.original.country || '-';
            }
        }
    ];
    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;
}
