import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import type { CarrierScheduleQueryDetail, CarrierScheduleQueryFilter, CarrierScheduleQueryHistory, CarrierScheduleNodeFact, CarrierScheduleQueryPage, CarrierScheduleQueryResource, CarrierScheduleQueryView, } from '../types' const basePath = '/platform/v1/carrier-schedule-queries' function optionalId(value: unknown): string | null { return value == null ? null : String(value) } function numberValue(value: unknown, fallback = 0): number { const parsed = Number(value); return Number.isFinite(parsed) ? parsed : fallback } function resource(raw: CarrierScheduleQueryResource): CarrierScheduleQueryResource { return { ...raw, id: String(raw.id), customerId: String(raw.customerId), oceanBookingId: String(raw.oceanBookingId), containerBookingId: optionalId(raw.containerBookingId), resultPortScheduleId: optionalId(raw.resultPortScheduleId), version: numberValue(raw.version), } } function view(raw: CarrierScheduleQueryView): CarrierScheduleQueryView { return { ...raw, query: resource(raw.query) } } function node(raw: CarrierScheduleNodeFact): CarrierScheduleNodeFact { return { ...raw, id: String(raw.id), nodeSequence: numberValue(raw.nodeSequence) } } function history(raw: CarrierScheduleQueryHistory): CarrierScheduleQueryHistory { return { ...raw, id: String(raw.id), fromVersion: raw.fromVersion == null ? null : numberValue(raw.fromVersion), toVersion: raw.toVersion == null ? null : numberValue(raw.toVersion), operatorId: optionalId(raw.operatorId) } } export async function listCarrierScheduleQueries(filter: CarrierScheduleQueryFilter): Promise { const response = await http.get, ApiEnvelope>(basePath, { params: filter }) const result = unwrapApiResponse(response) return { ...result, records: (result.records || []).map(view), page: numberValue(result.page, filter.page), size: numberValue(result.size, filter.size), total: numberValue(result.total), pages: numberValue(result.pages) } } export async function getCarrierScheduleQuery(queryId: string): Promise { const response = await http.get, ApiEnvelope>(`${basePath}/${encodeURIComponent(queryId)}`) const result = unwrapApiResponse(response) return { ...result, query: view(result.query), nodes: (result.nodes || []).map(node), histories: (result.histories || []).map(history) } }