import { DisplayOrderItem } from '@isa/oms/data-access';

/**
 * Builds a map of order item subset IDs to their quantities from display order items.
 *
 * @param item - The display order item containing subset items
 * @returns A Map mapping order item subset IDs to their quantities
 *
 * @example
 * ```typescript
 * const item: DisplayOrderItem = {
 *   subsetItems: [
 *     { id: 1, quantity: 5 },
 *     { id: 2, quantity: 3 }
 *   ]
 * };
 * const result = buildItemQuantityMap(item);
 * // Returns: Map { 1 => 5, 2 => 3 }
 * result.get(1); // 5
 * ```
 */
export const buildItemQuantityMap = (
  item: DisplayOrderItem,
): Map<number, number> => {
  return (item.subsetItems ?? []).reduce((acc, subsetItem) => {
    if (subsetItem.id && subsetItem.quantity) {
      acc.set(subsetItem.id, subsetItem.quantity);
    }
    return acc;
  }, new Map<number, number>());
};
