import { Operator } from '@aurorajs.dev/core-common';
import { buildFilterWhere } from './build-filter-where';

describe('buildFilterWhere', () => {
  it('returns undefined when filters is empty', () => {
    expect(buildFilterWhere([])).toBeUndefined();
  });

  it('returns a single condition object when only one filter is provided', () => {
    expect(
      buildFilterWhere([
        { column: 'name', columnLabel: 'Name', operator: 'contains', value: 'tag' },
      ]),
    ).toEqual({ name: { [Operator.iLike]: '%tag%' } });
  });

  it('combines multiple filters with [and]', () => {
    expect(
      buildFilterWhere([
        { column: 'name', columnLabel: 'Name', operator: 'contains', value: 'tag' },
        { column: 'code', columnLabel: 'Code', operator: 'equals', value: 'X' },
      ]),
    ).toEqual({
      [Operator.and]: [{ name: { [Operator.iLike]: '%tag%' } }, { code: { [Operator.eq]: 'X' } }],
    });
  });

  it('wraps dotted column ids in $...$ for relation columns', () => {
    expect(
      buildFilterWhere([
        {
          column: 'boundedContext.name',
          columnLabel: 'Bounded Context',
          operator: 'contains',
          value: 'iam',
        },
      ]),
    ).toEqual({ '$boundedContext.name$': { [Operator.iLike]: '%iam%' } });
  });

  it('translates startsWith / endsWith to iLike with the right wildcards', () => {
    expect(
      buildFilterWhere([
        { column: 'name', columnLabel: 'Name', operator: 'startsWith', value: 'a' },
        { column: 'code', columnLabel: 'Code', operator: 'endsWith', value: 'z' },
      ]),
    ).toEqual({
      [Operator.and]: [{ name: { [Operator.iLike]: 'a%' } }, { code: { [Operator.iLike]: '%z' } }],
    });
  });
});
