import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import type { InboundMeasurementDetail, InboundMeasurementDraftSaved, InboundMeasurementSubmitted, CartonMeasurementResult, ConfirmedPackageCountRequest, SaveInboundMeasurementDraftRequest, SubmitInboundMeasurementRequest, } from './types' function measurementPath(inboundId: string) { return `/platform/v1/first-leg/inbounds/${encodeURIComponent(inboundId)}/measurement` } export async function updateConfirmedPackageCount( customerId: string, inboundId: string, itemId: string, payload: ConfirmedPackageCountRequest, ) { const response = await http.put, ApiEnvelope>( `${measurementPath(inboundId)}/items/${segment(itemId)}/confirmed-package-count`, payload, { params: { tenantId: customerId } }, ) return unwrapApiResponse(response) } function segment(value: string) { return encodeURIComponent(value) } export async function getInboundMeasurement(customerId: string, inboundId: string) { // Device-scanned fields expected from the backend: originalPackageCount and // items[].boxCode, machineNo, measuredAt. Keep this compatibility mapping here. const response = await http.get, ApiEnvelope>( measurementPath(inboundId), { params: { tenantId: customerId } }, ) return unwrapApiResponse(response) } export async function saveInboundMeasurementDraft( customerId: string, inboundId: string, payload: SaveInboundMeasurementDraftRequest, ) { const response = await http.put, ApiEnvelope>( `${measurementPath(inboundId)}/draft`, payload, { params: { tenantId: customerId } }, ) return unwrapApiResponse(response) } export async function submitInboundMeasurement( customerId: string, inboundId: string, payload: SubmitInboundMeasurementRequest, ) { const response = await http.post, ApiEnvelope>( `${measurementPath(inboundId)}/submit`, payload, { params: { tenantId: customerId } }, ) return unwrapApiResponse(response) } export function downloadInboundMeasurementImage(customerId: string, inboundId: string, fileId: string) { return http.get( `/platform/v1/files/measurements/${segment(customerId)}/${segment(inboundId)}/${segment(fileId)}`, { responseType: 'blob' }, ) }