"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const filter_1 = require("../types/filter");
const filters_1 = require("./filters");
const filters_mock_1 = require("./filters.mock");
jest.mock('uuid', () => ({
    v4: jest.fn(() => 'uuid'),
}));
describe('addDashboardFiltersToMetricQuery', () => {
    test('should override the chart AND filter group with dashboard filters', async () => {
        const result = (0, filters_1.addDashboardFiltersToMetricQuery)(filters_mock_1.metricQueryWithAndFilters, filters_mock_1.dashboardFilters);
        expect(result).toEqual(filters_mock_1.expectedChartWithOverrideDashboardFilters);
    });
    test('should override the chart OR filter group with dashboard filters', async () => {
        const result = (0, filters_1.addDashboardFiltersToMetricQuery)(filters_mock_1.metricQueryWithOrFilters, filters_mock_1.dashboardFilters);
        expect(result).toEqual(filters_mock_1.expectedChartWithOverrideDashboardORFilters);
    });
});
describe('overrideChartFilter', () => {
    test('should override the chart and group filter', async () => {
        const result = (0, filters_1.overrideChartFilter)(filters_mock_1.chartAndFilterGroup, filters_mock_1.dashboardFilterWithSameTargetAndOperator, {});
        expect(result).toEqual({
            id: 'fillter-group-1',
            and: [
                {
                    id: '5',
                    target: { fieldId: 'field-1' },
                    values: ['1', '2', '3'],
                    disabled: false,
                    operator: filter_1.FilterOperator.EQUALS,
                },
                {
                    id: '2',
                    target: { fieldId: 'field-2' },
                    values: ['2'],
                    disabled: false,
                    operator: filter_1.FilterOperator.EQUALS,
                },
            ],
        });
    });
    test('should override the chart or group filter', async () => {
        const result = (0, filters_1.overrideChartFilter)(filters_mock_1.chartOrFilterGroup, filters_mock_1.dashboardFilterWithSameTargetAndOperator, {});
        expect(result).toEqual({
            id: 'fillter-group-1',
            or: [
                {
                    id: '5',
                    target: { fieldId: 'field-1' },
                    values: ['1', '2', '3'],
                    disabled: false,
                    operator: filter_1.FilterOperator.EQUALS,
                },
                {
                    id: '4',
                    target: { fieldId: 'field-2' },
                    values: ['2'],
                    disabled: false,
                    operator: filter_1.FilterOperator.EQUALS,
                },
            ],
        });
    });
    test('should override the chart group filter when operator is different', async () => {
        const result = (0, filters_1.overrideChartFilter)(filters_mock_1.chartAndFilterGroup, filters_mock_1.dashboardFilterWithSameTargetButDifferentOperator, {});
        expect(result).toEqual({
            id: 'fillter-group-1',
            and: [
                {
                    id: '5',
                    target: { fieldId: 'field-1' },
                    values: ['1', '2', '3'],
                    disabled: false,
                    operator: filter_1.FilterOperator.NOT_EQUALS,
                },
                {
                    id: '2',
                    target: { fieldId: 'field-2' },
                    values: ['2'],
                    disabled: false,
                    operator: filter_1.FilterOperator.EQUALS,
                },
            ],
        });
    });
});
describe('addFilterRule', () => {
    test('should add custom sql dimension filter to dimensions group', async () => {
        const result = (0, filters_1.addFilterRule)({
            filters: {},
            field: filters_mock_1.customSqlDimension,
        });
        expect(result).toEqual(filters_mock_1.expectedFiltersWithCustomSqlDimension);
    });
});
describe('createFilterRuleFromModelRequiredFilterRule', () => {
    test('should create a correct FilterRule', () => {
        const result = (0, filters_1.createFilterRuleFromModelRequiredFilterRule)((0, filters_mock_1.modelRequiredFilterRule)('dimension'), 'tableName');
        expect(result).toEqual((0, filters_mock_1.expectedRequiredResult)('dimension', 'tableName'));
    });
});
describe('isFilterRuleInQuery', () => {
    test('should correctly determine if a filter rule is in the query', () => {
        const filterGroupWithFilterRule = {
            id: 'mockGroupId',
            and: [filters_mock_1.filterRule],
        };
        const filterGroupWithoutFilterRule = {
            id: 'mockGroupId',
            and: [],
        };
        expect((0, filters_1.isFilterRuleInQuery)((0, filters_mock_1.dimension)('dim', 'table'), filters_mock_1.filterRule, filterGroupWithFilterRule)).toEqual(true);
        expect((0, filters_1.isFilterRuleInQuery)((0, filters_mock_1.dimension)('dim', 'table'), filters_mock_1.filterRule, filterGroupWithoutFilterRule)).toEqual(false);
    });
});
describe('reduceRequiredDimensionFiltersToFilterRules', () => {
    test('should correctly reduce required dimension filters to filter rules', () => {
        // Define mock data
        const mockRequiredFilters = [
            (0, filters_mock_1.modelRequiredFilterRule)('order_date_week'),
            (0, filters_mock_1.joinedModelRequiredFilterRule)('customers.created_at_week', 'customers'),
        ];
        const emptyFilters = {
            dimensions: {
                id: 'mockGroupId',
                and: [],
            },
        };
        const result = (0, filters_1.reduceRequiredDimensionFiltersToFilterRules)(mockRequiredFilters, emptyFilters.dimensions, filters_mock_1.mockExploreWithJoinedTable);
        const expectedFilterRuleResult = [
            (0, filters_mock_1.expectedRequiredResult)('order_date_week', 'orders'),
            (0, filters_mock_1.expectedRequiredResult)('created_at_week', 'customers'),
        ];
        expect(result).toEqual(expectedFilterRuleResult);
    });
});
describe('resetRequiredFilterRules', () => {
    test('should correctly reset required filter rules', () => {
        const filterGroup = {
            id: 'uuidGroup',
            and: [
                (0, filters_mock_1.expectedRequiredResult)('mockFieldRef1', 'table'),
                (0, filters_mock_1.expectedRequiredResult)('mockFieldRef2', 'table'),
            ],
        };
        const newFilterRules = (0, filters_1.resetRequiredFilterRules)(filterGroup, [
            'table_mockFieldRef1',
            'someOther',
        ]);
        expect(newFilterRules).toEqual(filters_mock_1.expectedRequiredResetResult);
    });
});
describe('trackWhichTimeBasedMetricFiltersToOverride', () => {
    test('should track fields to change when dashboard filter targets base time dimension', () => {
        const metricQueryDimensionFilters = {
            id: 'dim-filter-group',
            and: [
                {
                    id: 'month-filter',
                    target: { fieldId: 'order_date_month' },
                    operator: filter_1.FilterOperator.EQUALS,
                    values: ['2024-03'],
                },
                {
                    id: 'week-filter',
                    target: { fieldId: 'order_date_week' },
                    operator: filter_1.FilterOperator.EQUALS,
                    values: ['2024-03-25'],
                },
            ],
        };
        const dashboardFilter = {
            id: 'year-filter',
            label: 'Year Filter',
            target: {
                fieldId: 'order_date_year',
                tableName: 'orders',
            },
            operator: filter_1.FilterOperator.EQUALS,
            values: ['2024'],
        };
        const result = (0, filters_1.trackWhichTimeBasedMetricFiltersToOverride)(metricQueryDimensionFilters, dashboardFilter, filters_mock_1.mockExplore);
        expect(result.overrideData?.fieldsToChange).toEqual(expect.arrayContaining(['order_date_month', 'order_date_week']));
    });
    test('should not track fields when dashboard filter targets non-base dimension', () => {
        const metricQueryDimensionFilters = {
            id: 'dim-filter-group',
            and: [
                {
                    id: 'status-filter',
                    target: { fieldId: 'status' },
                    operator: filter_1.FilterOperator.EQUALS,
                    values: ['completed'],
                },
            ],
        };
        const dashboardFilter = {
            id: 'status-filter',
            label: 'Status Filter',
            target: {
                fieldId: 'status',
                tableName: 'orders',
            },
            operator: filter_1.FilterOperator.EQUALS,
            values: ['shipped'],
        };
        const result = (0, filters_1.trackWhichTimeBasedMetricFiltersToOverride)(metricQueryDimensionFilters, dashboardFilter, filters_mock_1.mockExplore);
        expect(result.overrideData?.fieldsToChange).toBeUndefined();
    });
    test('should handle multiple time intervals in metric query', () => {
        const metricQueryDimensionFilters = {
            id: 'dim-filter-group',
            or: [
                {
                    id: 'month-filter',
                    target: { fieldId: 'order_date_month' },
                    operator: filter_1.FilterOperator.EQUALS,
                    values: ['2024-03'],
                },
                {
                    id: 'week-filter',
                    target: { fieldId: 'order_date_week' },
                    operator: filter_1.FilterOperator.EQUALS,
                    values: ['2024-03-25'],
                },
                {
                    id: 'year-filter',
                    target: { fieldId: 'order_date_year' },
                    operator: filter_1.FilterOperator.EQUALS,
                    values: ['2024'],
                },
            ],
        };
        const dashboardFilter = {
            id: 'base-filter',
            label: 'Base Filter',
            target: {
                fieldId: 'order_date_year',
                tableName: 'orders',
            },
            operator: filter_1.FilterOperator.EQUALS,
            values: ['2024'],
        };
        const result = (0, filters_1.trackWhichTimeBasedMetricFiltersToOverride)(metricQueryDimensionFilters, dashboardFilter, filters_mock_1.mockExplore);
        expect(result.overrideData?.fieldsToChange).toEqual(expect.arrayContaining([
            'order_date_month',
            'order_date_week',
            'order_date_year',
        ]));
    });
    test('should return original filter when no matching time intervals', () => {
        const metricQueryDimensionFilters = {
            id: 'dim-filter-group',
            and: [
                {
                    id: 'status-filter',
                    target: { fieldId: 'status' },
                    operator: filter_1.FilterOperator.EQUALS,
                    values: ['completed'],
                },
            ],
        };
        const dashboardFilter = {
            id: 'year-filter',
            label: 'Year Filter',
            target: {
                fieldId: 'order_date_year',
                tableName: 'orders',
            },
            operator: filter_1.FilterOperator.EQUALS,
            values: ['2024'],
        };
        const result = (0, filters_1.trackWhichTimeBasedMetricFiltersToOverride)(metricQueryDimensionFilters, dashboardFilter, filters_mock_1.mockExplore);
        expect(result.overrideData?.fieldsToChange).toBeUndefined();
    });
});
describe('getDashboardFilterRulesForTileAndReferences', () => {
    test('should return filter rules when there is a match (isSqlColumn is true and fieldId is in references)', () => {
        const mockTileUuid = 'tile-123';
        const mockReferences = ['field-1', 'field-2'];
        const mockDashboardFilterRules = [
            {
                id: 'filter-1',
                label: 'Filter 1',
                target: {
                    fieldId: 'field-1',
                    tableName: 'table-1',
                    isSqlColumn: true,
                },
                operator: filter_1.FilterOperator.EQUALS,
                values: ['value-1'],
                tileTargets: {
                    [mockTileUuid]: {
                        fieldId: 'field-1',
                        tableName: 'table-1',
                        isSqlColumn: true,
                    },
                },
            },
        ];
        const result = (0, filters_1.getDashboardFilterRulesForTileAndReferences)(mockTileUuid, mockReferences, mockDashboardFilterRules);
        expect(result).toHaveLength(1);
        expect(result[0].id).toBe('filter-1');
        expect(result[0].target.fieldId).toBe('field-1');
        expect(result[0].target.isSqlColumn).toBe(true);
    });
    test('should not return filter rules when isSqlColumn is false even though fieldId is a match', () => {
        const mockTileUuid = 'tile-123';
        const mockReferences = ['field-1', 'field-2'];
        const mockDashboardFilterRules = [
            {
                id: 'filter-2',
                label: 'Filter 2',
                target: {
                    fieldId: 'field-2',
                    tableName: 'table-1',
                    isSqlColumn: false,
                },
                operator: filter_1.FilterOperator.EQUALS,
                values: ['value-2'],
                tileTargets: {
                    [mockTileUuid]: {
                        fieldId: 'field-2',
                        tableName: 'table-1',
                        isSqlColumn: false,
                    },
                },
            },
        ];
        const result = (0, filters_1.getDashboardFilterRulesForTileAndReferences)(mockTileUuid, mockReferences, mockDashboardFilterRules);
        // Verify filter-2 is not included (isSqlColumn is false but fieldId is a match)
        expect(result).toHaveLength(0);
    });
    test('should not return filter rules when isSqlColumn is true but fieldId does not match', () => {
        const mockTileUuid = 'tile-123';
        const mockReferences = ['field-1', 'field-2'];
        const mockDashboardFilterRules = [
            {
                id: 'filter-3',
                label: 'Filter 3',
                target: {
                    fieldId: 'field-3',
                    tableName: 'table-1',
                    isSqlColumn: true,
                },
                operator: filter_1.FilterOperator.EQUALS,
                values: ['value-3'],
                tileTargets: {
                    [mockTileUuid]: {
                        fieldId: 'field-3',
                        tableName: 'table-1',
                        isSqlColumn: true,
                    },
                },
            },
        ];
        const result = (0, filters_1.getDashboardFilterRulesForTileAndReferences)(mockTileUuid, mockReferences, mockDashboardFilterRules);
        // Verify filter-3 is not included (isSqlColumn is true but fieldId doesn't match)
        expect(result).toHaveLength(0);
    });
});
//# sourceMappingURL=filters.test.js.map