function computeAvailableReturnQuantity(line) {
  const cap = typeof line.shippedQuantity === "number" && Number.isFinite(line.shippedQuantity) ? Math.min(line.quantity, line.shippedQuantity) : line.quantity;
  const raw = cap - line.returnedQuantity;
  if (!Number.isFinite(raw) || raw <= 0) return 0;
  return Math.max(0, Math.floor(raw + 1e-6));
}
function coerceQuantity(value) {
  if (typeof value === "number" && Number.isFinite(value)) return value;
  if (typeof value === "string" && value.trim().length) {
    const parsed = Number(value);
    if (Number.isFinite(parsed)) return parsed;
  }
  return 0;
}
function sumShippedQuantityByLine(shipments) {
  const shippedByLine = /* @__PURE__ */ new Map();
  if (!Array.isArray(shipments)) return shippedByLine;
  for (const shipment of shipments) {
    if (!shipment || typeof shipment !== "object") continue;
    const lineItems = shipment.items;
    if (!Array.isArray(lineItems)) continue;
    for (const lineItem of lineItems) {
      if (!lineItem || typeof lineItem !== "object") continue;
      const record = lineItem;
      const orderLineId = typeof record.orderLineId === "string" ? record.orderLineId : typeof record.order_line_id === "string" ? record.order_line_id : null;
      if (!orderLineId) continue;
      shippedByLine.set(orderLineId, (shippedByLine.get(orderLineId) ?? 0) + coerceQuantity(record.quantity));
    }
  }
  return shippedByLine;
}
export {
  computeAvailableReturnQuantity,
  sumShippedQuantityByLine
};
//# sourceMappingURL=returnQuantity.js.map
