import { apiGet, apiPost } from './client' import type { SegmentationListResponse, SegmentationModelsResponse, SegmentationQaRequest, SegmentationQaResult, SegmentationRunListResponse, SegmentationRunRead, SegmentationRunRequest, SegmentationRunResponse, } from '../../types' function queryString(params: Record): string { const searchParams = new URLSearchParams() Object.entries(params).forEach(([key, value]) => { if (value !== null && value !== undefined && value !== '') { searchParams.set(key, String(value)) } }) const query = searchParams.toString() return query ? `?${query}` : '' } export const segmentationApi = { listModels: (): Promise => apiGet('/api/v1/segmentation/models'), run: (payload: SegmentationRunRequest): Promise => apiPost(`/api/v1/segmentation/run?project_id=${encodeURIComponent(payload.project_id)}`, payload), listRuns: (params: { project_id?: string | null; dataset_id?: string | null } = {}): Promise => apiGet(`/api/v1/segmentation/runs${queryString(params)}`), getRun: (analysisRunId: string): Promise => apiGet(`/api/v1/segmentation/runs/${analysisRunId}`), listSegmentations: ( analysisRunId: string, params: { project_id: string; dataset_id?: string | null; class_name?: string | null; min_confidence?: number | null }, ): Promise => apiGet(`/api/v1/segmentation/runs/${analysisRunId}/segmentations${queryString(params)}`), getRunGeoJson: ( analysisRunId: string, params: { project_id: string; class_name?: string | null; min_confidence?: number | null }, ): Promise => apiGet(`/api/v1/segmentation/runs/${analysisRunId}/geojson${queryString(params)}`), compareWithReference: (analysisRunId: string, projectId: string, payload: SegmentationQaRequest): Promise => apiPost(`/api/v1/segmentation/runs/${analysisRunId}/qa/reference?project_id=${encodeURIComponent(projectId)}`, payload), }