export const FREE_SHIPPING_THRESHOLD_CENTS = 15000;

export function hasFreeShipping(subtotal?: number | null) {
  return typeof subtotal === "number" && subtotal >= FREE_SHIPPING_THRESHOLD_CENTS;
}

export function getEffectiveShippingTotal(input: {
  subtotal?: number | null;
  shippingTotal?: number | null;
}) {
  if (hasFreeShipping(input.subtotal)) {
    return 0;
  }

  return typeof input.shippingTotal === "number" ? input.shippingTotal : null;
}

export function getEffectiveCartTotal(input: {
  subtotal?: number | null;
  shippingTotal?: number | null;
  total?: number | null;
}) {
  if (typeof input.total !== "number") {
    return null;
  }

  if (!hasFreeShipping(input.subtotal)) {
    return input.total;
  }

  const shippingTotal = typeof input.shippingTotal === "number" ? input.shippingTotal : 0;
  return Math.max(0, input.total - shippingTotal);
}
