import { describe, it, expect } from 'vitest';
import { buildItemQuantityMap } from './build-item-quantity-map.helper';
import { DisplayOrderItem } from '@isa/oms/data-access';
import { QuantityUnitType } from '@isa/common/data-access';

describe('buildItemQuantityMap', () => {
  it('should build a map with valid subset items', () => {
    // Arrange
    const item: DisplayOrderItem = {
      quantityUnitType: QuantityUnitType.Pieces,
      subsetItems: [
        { id: 1, quantity: 5 },
        { id: 2, quantity: 3 },
        { id: 3, quantity: 10 },
      ],
    } as DisplayOrderItem;

    // Act
    const result = buildItemQuantityMap(item);

    // Assert
    expect(result).toBeInstanceOf(Map);
    expect(result.size).toBe(3);
    expect(result.get(1)).toBe(5);
    expect(result.get(2)).toBe(3);
    expect(result.get(3)).toBe(10);
  });

  it('should return empty map when subsetItems is undefined', () => {
    // Arrange
    const item: DisplayOrderItem = {
      quantityUnitType: QuantityUnitType.Pieces,
    } as DisplayOrderItem;

    // Act
    const result = buildItemQuantityMap(item);

    // Assert
    expect(result).toBeInstanceOf(Map);
    expect(result.size).toBe(0);
  });

  it('should return empty map when subsetItems is empty array', () => {
    // Arrange
    const item: DisplayOrderItem = {
      quantityUnitType: QuantityUnitType.Pieces,
      subsetItems: [],
    } as DisplayOrderItem;

    // Act
    const result = buildItemQuantityMap(item);

    // Assert
    expect(result).toBeInstanceOf(Map);
    expect(result.size).toBe(0);
  });

  it('should skip subset items with missing id', () => {
    // Arrange
    const item: DisplayOrderItem = {
      quantityUnitType: QuantityUnitType.Pieces,
      subsetItems: [
        { id: 1, quantity: 5 },
        { id: undefined, quantity: 3 },
        { id: 2, quantity: 10 },
      ],
    } as DisplayOrderItem;

    // Act
    const result = buildItemQuantityMap(item);

    // Assert
    expect(result).toBeInstanceOf(Map);
    expect(result.size).toBe(2);
    expect(result.get(1)).toBe(5);
    expect(result.get(2)).toBe(10);
  });

  it('should skip subset items with missing quantity', () => {
    // Arrange
    const item: DisplayOrderItem = {
      quantityUnitType: QuantityUnitType.Pieces,
      subsetItems: [
        { id: 1, quantity: 5 },
        { id: 2, quantity: undefined },
        { id: 3, quantity: 10 },
      ],
    } as DisplayOrderItem;

    // Act
    const result = buildItemQuantityMap(item);

    // Assert
    expect(result).toBeInstanceOf(Map);
    expect(result.size).toBe(2);
    expect(result.get(1)).toBe(5);
    expect(result.get(3)).toBe(10);
  });

  it('should skip subset items with zero quantity', () => {
    // Arrange
    const item: DisplayOrderItem = {
      quantityUnitType: QuantityUnitType.Pieces,
      subsetItems: [
        { id: 1, quantity: 5 },
        { id: 2, quantity: 0 },
        { id: 3, quantity: 10 },
      ],
    } as DisplayOrderItem;

    // Act
    const result = buildItemQuantityMap(item);

    // Assert
    expect(result).toBeInstanceOf(Map);
    expect(result.size).toBe(2);
    expect(result.get(1)).toBe(5);
    expect(result.get(3)).toBe(10);
  });

  it('should handle mix of valid and invalid subset items', () => {
    // Arrange
    const item: DisplayOrderItem = {
      quantityUnitType: QuantityUnitType.Pieces,
      subsetItems: [
        { id: 1, quantity: 5 },
        { id: undefined, quantity: 3 },
        { id: 2, quantity: undefined },
        { id: 3, quantity: 0 },
        { id: 4, quantity: 7 },
      ],
    } as DisplayOrderItem;

    // Act
    const result = buildItemQuantityMap(item);

    // Assert
    expect(result).toBeInstanceOf(Map);
    expect(result.size).toBe(2);
    expect(result.get(1)).toBe(5);
    expect(result.get(4)).toBe(7);
  });

  it('should handle single subset item', () => {
    // Arrange
    const item: DisplayOrderItem = {
      quantityUnitType: QuantityUnitType.Pieces,
      subsetItems: [{ id: 42, quantity: 99 }],
    } as DisplayOrderItem;

    // Act
    const result = buildItemQuantityMap(item);

    // Assert
    expect(result).toBeInstanceOf(Map);
    expect(result.size).toBe(1);
    expect(result.get(42)).toBe(99);
  });
});
