import { getReceiptItemAction } from './get-receipt-item-action.helper';
import { ReceiptItem } from '../../models/receipt-item';
import { Product } from '../../schemas/product.schema';
import { Quantity } from '../../models/quantity';

describe('getReceiptItemAction', () => {
  const product: Product = {
    name: 'Test Product',
    contributors: 'Author',
    catalogProductNumber: '123',
    ean: '1234567890123',
    format: 'Hardcover',
    formatDetail: 'Detail',
    volume: '1',
    manufacturer: 'Test Publisher',
  };
  const quantity: Quantity = { quantity: 1 };

  const baseItem: ReceiptItem = {
    id: 1,
    product,
    quantity,
    receiptNumber: 'R-001',
    actions: [],
  };

  it('should return undefined if actions property is missing', () => {
    const item = { ...baseItem };
    delete (item as { actions?: unknown }).actions;
    expect(
      getReceiptItemAction(item as ReceiptItem, 'canReturn'),
    ).toBeUndefined();
  });

  it('should return undefined if no action with the given key exists', () => {
    const item = { ...baseItem, actions: [{ key: 'other', value: 'true' }] };
    expect(getReceiptItemAction(item, 'canReturn')).toBeUndefined();
  });

  it('should return the action object if the key exists', () => {
    const action = { key: 'canReturn', value: 'true' };
    const item = {
      ...baseItem,
      actions: [action, { key: 'other', value: 'false' }],
    };
    expect(getReceiptItemAction(item, 'canReturn')).toBe(action);
  });

  it('should return the first matching action if multiple exist', () => {
    const action1 = { key: 'canReturn', value: 'true' };
    const action2 = { key: 'canReturn', value: 'false' };
    const item = { ...baseItem, actions: [action1, action2] };
    expect(getReceiptItemAction(item, 'canReturn')).toBe(action1);
  });
});
