import { InputGroup, InputType } from '../../types';
import { filterGroupMapping } from './filter-group.mapping';
import * as schemaModule from '../schemas/filter-group.schema';

describe('filterGroupMapping', () => {
  const mockSchemaParser = jest.fn().mockImplementation((input) => input);

  beforeEach(() => {
    jest.clearAllMocks();

    // Mock the schema parse method to avoid validation errors in tests
    jest.spyOn(schemaModule.FilterGroupSchema, 'parse').mockImplementation(mockSchemaParser);
  });

  it('should map minimal input group correctly', () => {
    // Arrange
    const group: InputGroup = {
      group: 'testGroup',
      label: 'Test Group',
      input: [],
    };

    // Act
    const result = filterGroupMapping(group);

    // Assert
    expect(mockSchemaParser).toHaveBeenCalledWith({
      group: 'testGroup',
      label: 'Test Group',
      description: undefined,
    });

    expect(result).toEqual({
      group: 'testGroup',
      label: 'Test Group',
      description: undefined,
    });
  });

  it('should map complete input group correctly', () => {
    // Arrange
    const group: InputGroup = {
      group: 'testGroup',
      label: 'Test Group',
      description: 'Test Description',
      input: [],
    };

    // Act
    const result = filterGroupMapping(group);

    // Assert
    expect(mockSchemaParser).toHaveBeenCalledWith({
      group: 'testGroup',
      label: 'Test Group',
      description: 'Test Description',
    });

    expect(result).toEqual({
      group: 'testGroup',
      label: 'Test Group',
      description: 'Test Description',
    });
  });

  it('should ignore input property in the mapping result', () => {
    // Arrange
    const group: InputGroup = {
      group: 'testGroup',
      label: 'Test Group',
      input: [
        { key: 'input1', label: 'Input 1', type: InputType.Text },
        { key: 'input2', label: 'Input 2', type: InputType.Checkbox },
      ],
    };

    // Act
    const result = filterGroupMapping(group);

    // Assert
    expect(mockSchemaParser).toHaveBeenCalledWith({
      group: 'testGroup',
      label: 'Test Group',
      description: undefined,
    });

    expect(result).toEqual({
      group: 'testGroup',
      label: 'Test Group',
      description: undefined,
    });
    expect(result).not.toHaveProperty('input');
  });

  it('should handle schema validation errors', () => {
    // Arrange
    const schemaError = new Error('Schema validation failed');
    jest.spyOn(schemaModule.FilterGroupSchema, 'parse').mockImplementation(() => {
      throw schemaError;
    });

    const group: InputGroup = {
      group: 'testGroup',
      label: 'Test Group',
      input: [],
    };

    // Act & Assert
    expect(() => filterGroupMapping(group)).toThrow(schemaError);
  });
});
