import { describe, it, expect } from '@jest/globals';
import { getMainActions } from './get-main-actions.helper';
import { DBHOrderItemListItem } from '../schemas';

describe('getMainActions', () => {
  it('should return actions from first item when enabled is true', () => {
    // Arrange
    const items: DBHOrderItemListItem[] = [
      {
        actions: [
          { key: 'action1', value: 'Action 1', enabled: true },
          { key: 'action2', value: 'Action 2', enabled: true },
        ],
      } as DBHOrderItemListItem,
    ];

    // Act
    const result = getMainActions(items);

    // Assert
    expect(result).toEqual([
      { key: 'action1', value: 'Action 1', enabled: true },
      { key: 'action2', value: 'Action 2', enabled: true },
    ]);
  });

  it('should only include actions where enabled is true', () => {
    // Arrange
    const items: DBHOrderItemListItem[] = [
      {
        actions: [
          { key: 'action1', value: 'Action 1' },
          { key: 'action2', value: 'Action 2', enabled: true },
          { key: 'action3', value: 'Action 3', enabled: false },
        ],
      } as DBHOrderItemListItem,
    ];

    // Act
    const result = getMainActions(items);

    // Assert
    expect(result).toEqual([
      { key: 'action2', value: 'Action 2', enabled: true },
    ]);
  });

  it('should filter out actions containing FETCHED_PARTIAL when isPartial is true', () => {
    // Arrange
    const items: DBHOrderItemListItem[] = [
      {
        actions: [
          {
            key: 'action1',
            value: 'Action 1',
            command: 'DO_SOMETHING',
            enabled: true,
          },
          {
            key: 'action2',
            value: 'Action 2',
            command: 'FETCHED_PARTIAL_ACTION',
            enabled: true,
          },
          {
            key: 'action3',
            value: 'Action 3',
            command: 'DO_ANOTHER_THING',
            enabled: true,
          },
        ],
      } as DBHOrderItemListItem,
    ];

    // Act
    const result = getMainActions(items, true);

    // Assert
    expect(result).toEqual([
      {
        key: 'action1',
        value: 'Action 1',
        command: 'DO_SOMETHING',
        enabled: true,
      },
      {
        key: 'action3',
        value: 'Action 3',
        command: 'DO_ANOTHER_THING',
        enabled: true,
      },
    ]);
  });

  it('should include FETCHED_PARTIAL actions when isPartial is false', () => {
    // Arrange
    const items: DBHOrderItemListItem[] = [
      {
        actions: [
          {
            key: 'action1',
            value: 'Action 1',
            command: 'DO_SOMETHING',
            enabled: true,
          },
          {
            key: 'action2',
            value: 'Action 2',
            command: 'FETCHED_PARTIAL_ACTION',
            enabled: true,
          },
        ],
      } as DBHOrderItemListItem,
    ];

    // Act
    const result = getMainActions(items, false);

    // Assert
    expect(result).toEqual([
      {
        key: 'action1',
        value: 'Action 1',
        command: 'DO_SOMETHING',
        enabled: true,
      },
      {
        key: 'action2',
        value: 'Action 2',
        command: 'FETCHED_PARTIAL_ACTION',
        enabled: true,
      },
    ]);
  });

  it('should return empty array when items is undefined', () => {
    // Arrange
    const items = undefined as unknown as DBHOrderItemListItem[];

    // Act
    const result = getMainActions(items);

    // Assert
    expect(result).toEqual([]);
  });

  it('should return empty array when items array is empty', () => {
    // Arrange
    const items: DBHOrderItemListItem[] = [];

    // Act
    const result = getMainActions(items);

    // Assert
    expect(result).toEqual([]);
  });

  it('should return empty array when first item has no actions', () => {
    // Arrange
    const items: DBHOrderItemListItem[] = [{} as DBHOrderItemListItem];

    // Act
    const result = getMainActions(items);

    // Assert
    expect(result).toEqual([]);
  });

  it('should return empty array when first item actions is undefined', () => {
    // Arrange
    const items: DBHOrderItemListItem[] = [
      { actions: undefined } as DBHOrderItemListItem,
    ];

    // Act
    const result = getMainActions(items);

    // Assert
    expect(result).toEqual([]);
  });

  it('should combine both filters correctly', () => {
    // Arrange
    const items: DBHOrderItemListItem[] = [
      {
        actions: [
          {
            key: 'action1',
            value: 'Action 1',
            command: 'DO_SOMETHING',
            enabled: true,
          },
          {
            key: 'action2',
            value: 'Action 2',
            command: 'FETCHED_PARTIAL',
            enabled: true,
          },
          {
            key: 'action3',
            value: 'Action 3',
            command: 'FETCHED_PARTIAL_ACTION',
            enabled: true,
          },
          { key: 'action4', value: 'Action 4', enabled: false },
          { key: 'action5', value: 'Action 5', command: 'DO_ANOTHER_THING' },
        ],
      } as DBHOrderItemListItem,
    ];

    // Act
    const result = getMainActions(items, true);

    // Assert
    expect(result).toEqual([
      {
        key: 'action1',
        value: 'Action 1',
        command: 'DO_SOMETHING',
        enabled: true,
      },
    ]);
  });
});
