import { overseasRequest } from './overseas.js' const BASE = '/pda/v1/last-leg/return-inventory' const encoded = (value) => encodeURIComponent(String(value || '').trim()) const count = (value) => Number.isFinite(Number(value)) ? Math.max(0, Number(value)) : 0 function isNotFound(error) { const code = String(error?.code || '').toUpperCase() return code === 'A0404' || code === '404' } function normalizeItems(items) { return Array.isArray(items) ? items.map((item) => ({ skuCode: String(item.skuCode || ''), productNameZh: String(item.productNameZh || ''), quantity: count(item.quantity ?? item.availableQuantity) })) : [] } export function normalizePackingPreview(value = {}) { value = value && typeof value === 'object' ? value : {} const normalizeCustomer = (customer = {}) => ({ customerId: customer.customerId == null ? '' : String(customer.customerId), orders: Array.isArray(customer.orders) ? customer.orders.map((item) => ({ returnNo: String(item.returnNo || ''), customerReferenceNo: String(item.customerReferenceNo || ''), status: String(item.status || '') })) : [], skus: Array.isArray(customer.skus) ? customer.skus.map((item) => ({ skuCode: String(item.skuCode || ''), productNameZh: String(item.productNameZh || ''), availableQuantity: count(item.availableQuantity) })) : [] }) const result = { customerKey: value.customerId == null ? '' : String(value.customerId), orders: Array.isArray(value.orders) ? value.orders.map((item) => ({ returnNo: String(item.returnNo || ''), customerReferenceNo: String(item.customerReferenceNo || ''), status: String(item.status || '') })) : [], skus: Array.isArray(value.skus) ? value.skus.map((item) => ({ skuCode: String(item.skuCode || ''), productNameZh: String(item.productNameZh || ''), availableQuantity: count(item.availableQuantity) })) : [] } if (Array.isArray(value.customers)) result.customers = value.customers.map(normalizeCustomer).filter((item) => item.customerId) else if (result.customerKey && result.orders.length) { // Older service versions returned the single-customer shape. Keep the // PDA behavior consistent by exposing it through the grouped contract. result.customers = [{ customerId: result.customerKey, orders: result.orders, skus: result.skus }] } return result } export function normalizeReturnLabel(value = {}) { return { cartonMark: String(value.cartonMark || ''), sourceBatchNo: String(value.sourceBatchNo || ''), sourceReturnNo: String(value.sourceReturnNo || ''), totalQuantity: count(value.totalQuantity), length: count(value.length), width: count(value.width), height: count(value.height), weight: count(value.weight), items: normalizeItems(value.items), generatedAt: value.generatedAt || null, printableHtml: String(value.printableHtml || '') } } export async function findPackingOrders(returnNo = '', mode = 'RETURN_NO') { const query = String(returnNo || '').trim() try { return normalizePackingPreview(await overseasRequest('GET', `${BASE}/packing/orders${query ? `?${mode === 'RETURN_NO' ? 'returnNo' : 'skuCode'}=${encoded(query)}` : ''}`)) } catch (error) { if (isNotFound(error)) return normalizePackingPreview() throw error } } export async function listPackingPlans() { const value = await overseasRequest('GET', `${BASE}/packing/plans`) return Array.isArray(value) ? value.map(normalizePackingPlan) : [] } export async function completePackingPlan(planNo, request) { const value = await overseasRequest('POST', `${BASE}/packing/plans/${encoded(planNo)}/complete`, request) return { planNo: String(value?.planNo || planNo), labels: Array.isArray(value?.labels) ? value.labels.map(normalizeReturnLabel) : [] } } export function normalizePackingPlan(value = {}) { value = { ...value, completionCartons: Array.isArray(value.completionCartons) ? value.completionCartons.map(normalizeReturnLabel) : [], transfers: Array.isArray(value.transfers) ? value.transfers : [], completedAt: value.completedAt || null } return { completionCartons: value.completionCartons, transfers: value.transfers, completedAt: value.completedAt, planNo: String(value.planNo || ''), customerId: String(value.customerId || ''), returnNos: Array.isArray(value.returnNos) ? value.returnNos.map(String) : [], sourceCartonMarks: Array.isArray(value.sourceCartonMarks) ? value.sourceCartonMarks.map(String) : [], sources: Array.isArray(value.sources) ? value.sources : [], mode: String(value.mode || ''), status: String(value.status || ''), boxes: Array.isArray(value.boxes) ? value.boxes : [], remark: String(value.remark || '') } } export async function getPackingPlan(planNo) { return normalizePackingPlan(await overseasRequest('GET', `${BASE}/packing/plans/${encoded(planNo)}`)) } export async function scanReturnCarton(mark) { return overseasRequest('GET', `${BASE}/cartons/${encoded(mark)}`) } export async function mergeReturnBoxes(request) { const value = await overseasRequest('POST', `${BASE}/cartons/merge-boxes`, request) return { labels: [normalizeReturnLabel(value?.label)] } } export async function packReturnSkus(request) { const value = await overseasRequest('POST', `${BASE}/cartons/pack`, request) return { labels: Array.isArray(value?.labels) ? value.labels.map(normalizeReturnLabel) : [] } } export async function putawayReturnCarton(mark, request) { return overseasRequest('POST', `${BASE}/cartons/${encoded(mark)}/putaway`, request) } export async function mergeReturnCartons(request) { const value = await overseasRequest('POST', `${BASE}/cartons/merge`, request) return { cartonMark: String(value?.cartonMark || ''), skuCode: String(value?.skuCode || ''), totalQuantity: count(value?.totalQuantity), label: normalizeReturnLabel(value?.label) } } export async function getReturnCartonLabel(cartonMark) { return normalizeReturnLabel(await overseasRequest('GET', `${BASE}/cartons/${encoded(cartonMark)}/label`)) } export async function confirmReturnLabel(cartonMark) { return normalizeReturnLabel(await overseasRequest('POST', `${BASE}/cartons/${encoded(cartonMark)}/confirm-label`)) }