import { describe, it, expect } from 'vitest';
import { groupByBranch } from './group-by-branch.helper';
import { RewardSelectionItem } from '@isa/checkout/data-access';

describe('groupByBranch', () => {
  it('should not group items for Versand order type', () => {
    const items: RewardSelectionItem[] = [
      {
        item: {
          product: { name: 'Product A' },
        },
        cartQuantity: 1,
        rewardCartQuantity: 0,
      } as RewardSelectionItem,
      {
        item: {
          product: { name: 'Product B' },
        },
        cartQuantity: 1,
        rewardCartQuantity: 0,
      } as RewardSelectionItem,
    ];

    const result = groupByBranch('Versand', items);

    expect(result).toHaveLength(1);
    expect(result[0].branchId).toBeUndefined();
    expect(result[0].items).toHaveLength(2);
  });

  it('should group items by branch for Abholung order type', () => {
    const items: RewardSelectionItem[] = [
      {
        item: {
          product: { name: 'Product A' },
          destination: {
            data: {
              targetBranch: {
                id: 1,
                data: { name: 'Branch 1' },
              },
            },
          },
        },
        cartQuantity: 1,
        rewardCartQuantity: 0,
      } as RewardSelectionItem,
      {
        item: {
          product: { name: 'Product B' },
          destination: {
            data: {
              targetBranch: {
                id: 2,
                data: { name: 'Branch 2' },
              },
            },
          },
        },
        cartQuantity: 1,
        rewardCartQuantity: 0,
      } as RewardSelectionItem,
    ];

    const result = groupByBranch('Abholung', items);

    expect(result).toHaveLength(2);
    expect(result[0].branchId).toBe(1);
    expect(result[1].branchId).toBe(2);
  });

  it('should sort items by product name within groups', () => {
    const items: RewardSelectionItem[] = [
      {
        item: {
          product: { name: 'Zebra' },
        },
        cartQuantity: 1,
        rewardCartQuantity: 0,
      } as RewardSelectionItem,
      {
        item: {
          product: { name: 'Apple' },
        },
        cartQuantity: 1,
        rewardCartQuantity: 0,
      } as RewardSelectionItem,
    ];

    const result = groupByBranch('Versand', items);

    expect(result[0].items[0].item.product.name).toBe('Apple');
    expect(result[0].items[1].item.product.name).toBe('Zebra');
  });
});
