"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const errors_1 = require("./errors");
const filter_1 = require("./filter");
const filterGrammar_1 = require("./filterGrammar");
const filterGrammarConversion_1 = require("./filterGrammarConversion");
describe('convertMetricFilterToDbt', () => {
    it('should return undefined if filters are undefined', () => {
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(undefined)).toBeUndefined();
    });
    it('should return an empty array if filters are an empty array', () => {
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)([])).toEqual([]);
    });
    it('should convert EQUALS filter correctly', () => {
        const filters = [
            {
                target: { fieldRef: 'customers.customer_id' },
                id: '1',
                operator: filter_1.FilterOperator.EQUALS,
                values: ['value1'],
            },
        ];
        const expected = [
            { customer_id: 'value1' },
        ];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toEqual(expected);
    });
    it('should convert EQUALS with multiple values correctly', () => {
        const filters = [
            {
                target: { fieldRef: 'customers.customer_id' },
                id: '1',
                operator: filter_1.FilterOperator.EQUALS,
                values: ['value1', 'value2'],
            },
        ];
        const expected = [
            { customer_id: ['value1', 'value2'] },
        ];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toEqual(expected);
    });
    it('should convert NOT_EQUALS filter correctly', () => {
        const filters = [
            {
                target: { fieldRef: 'field2' },
                id: '1',
                operator: filter_1.FilterOperator.NOT_EQUALS,
                values: ['value2'],
            },
        ];
        const expected = [
            { field2: '!value2' },
        ];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toEqual(expected);
    });
    it('should convert NULL filters correctly', () => {
        const filters = [
            {
                target: { fieldRef: 'field1' },
                id: '1',
                operator: filter_1.FilterOperator.NULL,
                values: [],
            },
            {
                target: { fieldRef: 'field2' },
                id: '1',
                operator: filter_1.FilterOperator.NOT_NULL,
                values: [],
            },
        ];
        const expected = [
            { field1: 'null' },
            { field2: '!null' },
        ];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toEqual(expected);
    });
    it('should convert boolean filters correctly', () => {
        const filters = [
            {
                target: { fieldRef: 'field2' },
                id: '1',
                operator: filter_1.FilterOperator.EQUALS,
                values: [false],
            },
            {
                target: { fieldRef: 'field3' },
                id: '1',
                operator: filter_1.FilterOperator.NOT_EQUALS,
                values: [true],
            },
        ];
        const expected = [
            { field2: 'false' },
            { field3: '!true' },
        ];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toEqual(expected);
    });
    it('should convert INCLUDE filters correctly', () => {
        const filters = [
            {
                target: { fieldRef: 'field1' },
                id: '1',
                operator: filter_1.FilterOperator.INCLUDE,
                values: ['katie'],
            },
        ];
        const expected = [
            { field1: '%katie%' },
        ];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toEqual(expected);
    });
    it('should convert STARTS_WITH and ENDS_WITH filters correctly', () => {
        const filters = [
            {
                target: { fieldRef: 'field1' },
                id: '1',
                operator: filter_1.FilterOperator.STARTS_WITH,
                values: ['katie'],
            },
            {
                target: { fieldRef: 'field2' },
                id: '2',
                operator: filter_1.FilterOperator.ENDS_WITH,
                values: ['katie'],
            },
        ];
        const expected = [
            { field1: 'katie%' },
            { field2: '%katie' },
        ];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toEqual(expected);
    });
    it('should convert GREATER_THAN and GREATER_THAN_OR_EQUAL filters correctly', () => {
        const filters = [
            {
                target: { fieldRef: 'field1' },
                id: '1',
                operator: filter_1.FilterOperator.GREATER_THAN,
                values: ['4'],
            },
            {
                target: { fieldRef: 'field2' },
                id: '2',
                operator: filter_1.FilterOperator.GREATER_THAN_OR_EQUAL,
                values: ['5'],
            },
        ];
        const expected = [
            { field1: '> 4' },
            { field2: '>= 5' },
        ];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toEqual(expected);
    });
    it('should convert IN_THE_NEXT and IN_THE_PAST filters correctly', () => {
        const filters = [
            {
                target: { fieldRef: 'field1' },
                id: '1',
                operator: filter_1.FilterOperator.IN_THE_NEXT,
                values: ['14'],
                settings: {
                    unitOfTime: filter_1.UnitOfTime.days,
                    completed: false,
                },
            },
            {
                target: { fieldRef: 'field4' },
                id: '2',
                operator: filter_1.FilterOperator.IN_THE_PAST,
                values: [14],
                settings: {
                    unitOfTime: filter_1.UnitOfTime.months,
                    completed: false,
                },
            },
        ];
        const expected = [
            { field1: 'inTheNext 14 days' },
            { field4: 'inThePast 14 months' },
        ];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toEqual(expected);
    });
    it('should throw error on convert IN_THE_NEXT and IN_THE_PAST with completed', () => {
        const filters = [
            {
                target: { fieldRef: 'field1' },
                id: '1',
                operator: filter_1.FilterOperator.IN_THE_NEXT,
                values: ['14'],
                settings: {
                    unitOfTime: filter_1.UnitOfTime.days,
                    completed: true,
                },
            },
        ];
        expect(() => (0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toThrow(errors_1.NotImplementedError);
    });
    it('should handle filters with undefined or empty values', () => {
        const filters = [
            {
                target: { fieldRef: 'field3' },
                id: '1',
                operator: filter_1.FilterOperator.EQUALS,
                values: [],
            },
            {
                target: { fieldRef: 'field4' },
                id: '2',
                operator: filter_1.FilterOperator.EQUALS,
                values: [undefined],
            },
        ];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toEqual([]);
    });
    it('should throw NotImplementedError for unsupported operators', () => {
        const filters = [
            {
                target: { fieldRef: 'field5' },
                id: '1',
                operator: filter_1.FilterOperator.IN_BETWEEN,
                values: ['value3', 'value4'],
            },
        ];
        expect(() => (0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toThrow('No function implemented to convert custom metric filter to dbt: inBetween');
    });
});
describe('convert from filterGrammar', () => {
    it('should return undefined if filters are undefined', () => {
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(undefined)).toBeUndefined();
    });
    it('should convert EQUALS filter correctly', () => {
        const rawFilters = [{ name: 'pedram' }];
        const filters = (0, filterGrammar_1.parseFilters)(rawFilters);
        expect(filters[0].operator).toEqual('equals');
        expect(filters[0].values).toEqual(['pedram']);
        expect(filters[0].target.fieldRef).toEqual('name');
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toEqual(rawFilters);
    });
    it('should convert NOT_EQUALS filter correctly', () => {
        const rawFilters = [{ name: '!pedram' }];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)((0, filterGrammar_1.parseFilters)(rawFilters))).toEqual(rawFilters);
    });
    it('should convert EQUALS with multiple values correctly', () => {
        const rawFilters = [{ customer_id: ['1', '2'] }];
        const filters = (0, filterGrammar_1.parseFilters)(rawFilters);
        expect(filters[0].values).toEqual(['1', '2']);
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)(filters)).toEqual(rawFilters);
    });
    it('should convert NULL filter correctly', () => {
        const rawFilters = [{ customer_id: 'null' }];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)((0, filterGrammar_1.parseFilters)(rawFilters))).toEqual(rawFilters);
    });
    it('should convert IN_THE_NEXT filter correctly', () => {
        const rawFilters = [{ timestamp: 'inTheNext 14 days' }];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)((0, filterGrammar_1.parseFilters)(rawFilters))).toEqual(rawFilters);
    });
    it('should convert STARTS_WITH filter correctly', () => {
        const rawFilters = [{ name: 'katie%' }];
        expect((0, filterGrammarConversion_1.convertMetricFilterToDbt)((0, filterGrammar_1.parseFilters)(rawFilters))).toEqual(rawFilters);
    });
});
//# sourceMappingURL=filterGrammarConversion.test.js.map