export function orderGrossTotal(order) {
  return order.items.reduce(
    (acc, item) => acc + item.price * item.quantity_ordered,
    0
  );
}

export function getPaymentMethodRaw(order) {
  return String(order.paymentMethod || order.payment_method || "").toLowerCase();
}

/** Pending wire-transfer orders that still need payment (show Girocode row). */
export function isWireGirocodeEligible(order) {
  const amount = Math.round(orderGrossTotal(order) * 100) / 100;
  return (
    order.status === "pending" &&
    getPaymentMethodRaw(order) === "wire" &&
    amount >= 0.01
  );
}

export function hasPendingWirePaymentOrder(orders) {
  return Array.isArray(orders) && orders.some(isWireGirocodeEligible);
}

/** Browser CustomEvent name: detail.pending is boolean */
export const WIRE_PAYMENT_PENDING_EVENT = "wirePaymentOrdersPending";
