import { api, type ApiResponse } from '$lib/api';
import type { CsvImportProfile } from '$lib/csv-import-session';

/**
 * Consulta el estado de un job CSV según el perfil de importación (misma lógica que pollStatus en csv-upload).
 */
export function fetchCsvImportStatus(
	jobId: string,
	profile: CsvImportProfile
): Promise<ApiResponse> {
	switch (profile) {
		case 'customs_brokers':
			return api.customsBrokerImports.status(jobId);
		case 'clients_providers':
			return api.clientProviderImports.status(jobId);
		case 'exchange_rates':
			return api.exchangeRateImports.status(jobId);
		case 'pedimentos':
			return api.pedimentosImports.status(jobId);
		case 'material_classes':
			return api.materialClassImports.status(jobId);
		case 'vehicles':
			return api.vehicleImports.status(jobId);
		case 'drivers':
			return api.driverImports.status(jobId);
		case 'trailers':
			return api.trailerImports.status(jobId);
		case 'transporters':
			return api.transporterImports.status(jobId);
		case 'part_numbers':
			return api.partNumberImports.status(jobId);
		case 'boms':
			return api.bomImports.status(jobId);
		case 'exportacion':
			return api.exportacionImports.status(jobId);
		case 'imports':
		default:
			return api.imports.status(jobId);
	}
}

export function isWaitingConfirmationPayload(data: unknown): boolean {
	if (!data || typeof data !== 'object') return false;
	const d = data as Record<string, unknown>;
	if (d.status === 'waiting_confirmation') return true;
	return typeof d.job_id === 'string' && typeof d.total_rows === 'number';
}
