import type { Port } from '$lib/api/dashboard/a76/general_catalogs/ports';
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<Port>[] {
    return [
        {
            accessorKey: 'port_code',
            header: 'Código Puerto',
        },
        {
            accessorKey: 'description',
            header: 'Descripción',
            cell: ({ row }) => row.original.description || '-'
        },
        {
            accessorKey: 'location_code',
            header: 'Código Ubicación',
        },
        {
            accessorKey: 'location_description',
            header: 'Ubicación',
            cell: ({ row }) => row.original.location_description || '-'
        },
        {
            accessorKey: 'port_type',
            header: 'Tipo',
            cell: ({ row }) => {
                const type = row.original.port_type;
                if (type === 'ENTRY') return 'Entrada';
                if (type === 'EXIT') return 'Salida';
                if (type === 'BOTH') return 'Ambos';
                return type;
            }
        },
        {
            id: 'actions',
            header: 'Acciones',
            cell: ({ row }) => {
                return renderComponent(DataTableActions, {
                    item: row.original,
                    onSuccess,
                    canEdit: permissions.canEdit,
                    canDelete: permissions.canDelete
                });
            }
        }
    ];
}
