import { DisplayOrder } from '@isa/oms/data-access';
import { getOrderTypeFeature } from './get-order-type-feature.helper';
import { OrderType } from '../models';

/**
 * Groups display orders by their delivery type (orderType feature).
 *
 * @param orders - Array of DisplayOrder objects to group
 * @returns Map where keys are order types (Abholung, Rücklage, Versand, etc.)
 *          and values are arrays of orders with that type
 *
 * @example
 * ```typescript
 * const orders = [
 *   { id: 1, features: { orderType: 'Abholung' }, ... },
 *   { id: 2, features: { orderType: 'Abholung' }, ... },
 *   { id: 3, features: { orderType: 'Versand' }, ... }
 * ];
 *
 * const grouped = groupDisplayOrdersByDeliveryType(orders);
 * // Map {
 * //   'Abholung' => [order1, order2],
 * //   'Versand' => [order3]
 * // }
 * ```
 */
export function groupDisplayOrdersByDeliveryType(
  orders: DisplayOrder[],
): Map<OrderType | string, DisplayOrder[]> {
  return orders.reduce((map, order) => {
    const orderType = getOrderTypeFeature(order.features) ?? 'Unbekannt';
    if (!map.has(orderType)) {
      map.set(orderType, []);
    }
    map.get(orderType)!.push(order);
    return map;
  }, new Map<OrderType | string, DisplayOrder[]>());
}
