import { describe, it, expect } from 'vitest';
import { mergeRewardSelectionItems } from './merge-reward-selection-items.helper';
import { ShoppingCartItem } from '@isa/checkout/data-access';

describe('mergeRewardSelectionItems', () => {
  it('should merge items from both carts', () => {
    const shoppingCartItems: ShoppingCartItem[] = [
      {
        product: { ean: '123' },
        quantity: 2,
      } as ShoppingCartItem,
    ];

    const rewardShoppingCartItems: ShoppingCartItem[] = [
      {
        product: { ean: '123' },
        quantity: 1,
      } as ShoppingCartItem,
    ];

    const result = mergeRewardSelectionItems(
      shoppingCartItems,
      rewardShoppingCartItems,
    );

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

  it('should handle items only in shopping cart', () => {
    const shoppingCartItems: ShoppingCartItem[] = [
      {
        product: { ean: '123' },
        quantity: 2,
      } as ShoppingCartItem,
    ];

    const rewardShoppingCartItems: ShoppingCartItem[] = [];

    const result = mergeRewardSelectionItems(
      shoppingCartItems,
      rewardShoppingCartItems,
    );

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

  it('should handle items only in reward cart', () => {
    const shoppingCartItems: ShoppingCartItem[] = [];

    const rewardShoppingCartItems: ShoppingCartItem[] = [
      {
        product: { ean: '456' },
        quantity: 1,
      } as ShoppingCartItem,
    ];

    const result = mergeRewardSelectionItems(
      shoppingCartItems,
      rewardShoppingCartItems,
    );

    expect(result).toHaveLength(1);
    expect(result[0].cartQuantity).toBe(0);
    expect(result[0].rewardCartQuantity).toBe(1);
  });

  it('should handle empty arrays', () => {
    const result = mergeRewardSelectionItems([], []);

    expect(result).toHaveLength(0);
  });

  it('should initialize catalog fields as undefined', () => {
    const shoppingCartItems: ShoppingCartItem[] = [
      {
        product: { ean: '123' },
        quantity: 1,
      } as ShoppingCartItem,
    ];

    const result = mergeRewardSelectionItems(shoppingCartItems, []);

    expect(result[0].catalogPrice).toBeUndefined();
    expect(result[0].availabilityPrice).toBeUndefined();
    expect(result[0].catalogRewardPoints).toBeUndefined();
  });
});
