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 = {}) { return { 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) })) : [] } } 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 = '') { const query = String(returnNo || '').trim() try { return normalizePackingPreview(await overseasRequest('GET', `${BASE}/packing/orders${query ? `?returnNo=${encoded(query)}` : ''}`)) } catch (error) { if (isNotFound(error)) return normalizePackingPreview() throw error } } 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`)) } function normalizePlanBox(box = {}) { return { boxNo: Number(box.boxNo || 0), items: Array.isArray(box.items) ? box.items.map((item) => ({ skuCode: String(item.skuCode || ''), quantity: count(item.quantity) })) : [], length: count(box.length), width: count(box.width), height: count(box.height), weight: count(box.weight), } } export function normalizeReturnPackingPlan(value = {}) { const skuLines = Array.isArray(value.skuLines) ? value.skuLines.map((item) => ({ skuCode: String(item.skuCode || ''), productNameZh: String(item.productNameZh || ''), quantity: count(item.quantity) })) : [] const boxes = Array.isArray(value.boxes) && value.boxes.length ? value.boxes.map(normalizePlanBox) : skuLines.map((item, index) => ({ boxNo: index + 1, items: [{ skuCode: item.skuCode, quantity: item.quantity }], length: 0, width: 0, height: 0, weight: 0 })) return { planNo: String(value.planNo || ''), status: String(value.status || 'READY'), mode: String(value.mode || 'WAREHOUSE_RECOMMENDATION'), customerName: String(value.customerName || ''), customerReferenceNo: String(value.customerReferenceNo || ''), returnNos: Array.isArray(value.returnNos) ? value.returnNos.map((item) => String(item || '')).filter(Boolean) : [], skuLines, boxes, remark: String(value.remark || ''), } } export async function listReturnPackingPlans() { try { const value = await overseasRequest('GET', `${BASE}/packing/plans`) const records = Array.isArray(value) ? value : Array.isArray(value?.records) ? value.records : [] return records.map(normalizeReturnPackingPlan) } catch (error) { if (isNotFound(error)) return []; throw error } } export async function getReturnPackingPlan(planNo) { return normalizeReturnPackingPlan(await overseasRequest('GET', `${BASE}/packing/plans/${encoded(planNo)}`)) } export async function mergeReturnPackingPlan(planNo, request) { const value = await overseasRequest('POST', `${BASE}/packing/plans/${encoded(planNo)}/complete`, request) return { ...value, labels: Array.isArray(value?.labels) ? value.labels.map(normalizeReturnLabel) : [normalizeReturnLabel(value?.label || value)], label: normalizeReturnLabel(value?.label || value) } }