import {
  getOrderTypeFeature,
  RewardSelectionItem,
} from '@isa/checkout/data-access';

const ORDER_TYPES_WITH_BRANCH_DISTINCTION = ['Abholung', 'Rücklage'] as const;

export const shouldShowGrouping = (items: RewardSelectionItem[]): boolean => {
  if (items.length <= 1) return false;

  // Prüfe ob alle Items denselben OrderType haben
  const firstOrderType = getOrderTypeFeature(items[0]?.item.features);
  const hasDifferentOrderTypes = items.some(
    (item) => getOrderTypeFeature(item.item.features) !== firstOrderType,
  );

  if (hasDifferentOrderTypes) return true;

  // Wenn alle denselben OrderType haben, prüfe ob verschiedene Branches existieren
  // (nur relevant für Abholung/Rücklage)
  if (ORDER_TYPES_WITH_BRANCH_DISTINCTION.includes(firstOrderType as any)) {
    const firstBranchId = items[0]?.item.destination?.data?.targetBranch?.id;
    const hasDifferentBranches = items.some(
      (item) => item.item.destination?.data?.targetBranch?.id !== firstBranchId,
    );
    return hasDifferentBranches;
  }

  return false;
};
