"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const conditionalFormatting_1 = require("../types/conditionalFormatting");
const field_1 = require("../types/field");
const filter_1 = require("../types/filter");
const conditionalFormatting_2 = require("./conditionalFormatting");
// Mock field for testing
const mockNumericField = {
    name: 'test_field',
    table: 'test_table',
    type: field_1.DimensionType.NUMBER,
    fieldType: field_1.FieldType.DIMENSION,
    sql: 'test_sql',
    tableLabel: 'Test Table',
    label: 'Test Field',
    hidden: false,
};
describe('hasMatchingConditionalRules', () => {
    describe('LESS_THAN_OR_EQUAL operator', () => {
        it('should return true when field value is less than rule value', () => {
            const config = (0, conditionalFormatting_2.createConditionalFormattingConfigWithSingleColor)('#ff0000');
            const rule = (0, conditionalFormatting_2.createConditionalFormattingRuleWithValues)();
            rule.operator = filter_1.FilterOperator.LESS_THAN_OR_EQUAL;
            rule.values = [10];
            config.rules = [rule];
            const result = (0, conditionalFormatting_2.hasMatchingConditionalRules)(mockNumericField, 5, // less than 10
            {}, config);
            expect(result).toBe(true);
        });
        it('should return true when field value equals rule value', () => {
            const config = (0, conditionalFormatting_2.createConditionalFormattingConfigWithSingleColor)('#ff0000');
            const rule = (0, conditionalFormatting_2.createConditionalFormattingRuleWithValues)();
            rule.operator = filter_1.FilterOperator.LESS_THAN_OR_EQUAL;
            rule.values = [10];
            config.rules = [rule];
            const result = (0, conditionalFormatting_2.hasMatchingConditionalRules)(mockNumericField, 10, // equals 10
            {}, config);
            expect(result).toBe(true);
        });
        it('should return false when field value is greater than rule value', () => {
            const config = (0, conditionalFormatting_2.createConditionalFormattingConfigWithSingleColor)('#ff0000');
            const rule = (0, conditionalFormatting_2.createConditionalFormattingRuleWithValues)();
            rule.operator = filter_1.FilterOperator.LESS_THAN_OR_EQUAL;
            rule.values = [10];
            config.rules = [rule];
            const result = (0, conditionalFormatting_2.hasMatchingConditionalRules)(mockNumericField, 15, // greater than 10
            {}, config);
            expect(result).toBe(false);
        });
    });
    describe('GREATER_THAN_OR_EQUAL operator', () => {
        it('should return true when field value is greater than rule value', () => {
            const config = (0, conditionalFormatting_2.createConditionalFormattingConfigWithSingleColor)('#ff0000');
            const rule = (0, conditionalFormatting_2.createConditionalFormattingRuleWithValues)();
            rule.operator = filter_1.FilterOperator.GREATER_THAN_OR_EQUAL;
            rule.values = [10];
            config.rules = [rule];
            const result = (0, conditionalFormatting_2.hasMatchingConditionalRules)(mockNumericField, 15, // greater than 10
            {}, config);
            expect(result).toBe(true);
        });
        it('should return true when field value equals rule value', () => {
            const config = (0, conditionalFormatting_2.createConditionalFormattingConfigWithSingleColor)('#ff0000');
            const rule = (0, conditionalFormatting_2.createConditionalFormattingRuleWithValues)();
            rule.operator = filter_1.FilterOperator.GREATER_THAN_OR_EQUAL;
            rule.values = [10];
            config.rules = [rule];
            const result = (0, conditionalFormatting_2.hasMatchingConditionalRules)(mockNumericField, 10, // equals 10
            {}, config);
            expect(result).toBe(true);
        });
        it('should return false when field value is less than rule value', () => {
            const config = (0, conditionalFormatting_2.createConditionalFormattingConfigWithSingleColor)('#ff0000');
            const rule = (0, conditionalFormatting_2.createConditionalFormattingRuleWithValues)();
            rule.operator = filter_1.FilterOperator.GREATER_THAN_OR_EQUAL;
            rule.values = [10];
            config.rules = [rule];
            const result = (0, conditionalFormatting_2.hasMatchingConditionalRules)(mockNumericField, 5, // less than 10
            {}, config);
            expect(result).toBe(false);
        });
    });
    describe('Edge cases', () => {
        it('should handle multiple values with LESS_THAN_OR_EQUAL', () => {
            const config = (0, conditionalFormatting_2.createConditionalFormattingConfigWithSingleColor)('#ff0000');
            const rule = (0, conditionalFormatting_2.createConditionalFormattingRuleWithValues)();
            rule.operator = filter_1.FilterOperator.LESS_THAN_OR_EQUAL;
            rule.values = [5, 15];
            config.rules = [rule];
            // Should match if value is <= any of the rule values
            const result = (0, conditionalFormatting_2.hasMatchingConditionalRules)(mockNumericField, 10, // <= 15 (second value)
            {}, config);
            expect(result).toBe(true);
        });
        it('should handle multiple values with GREATER_THAN_OR_EQUAL', () => {
            const config = (0, conditionalFormatting_2.createConditionalFormattingConfigWithSingleColor)('#ff0000');
            const rule = (0, conditionalFormatting_2.createConditionalFormattingRuleWithValues)();
            rule.operator = filter_1.FilterOperator.GREATER_THAN_OR_EQUAL;
            rule.values = [5, 15];
            config.rules = [rule];
            // Should match if value is >= any of the rule values
            const result = (0, conditionalFormatting_2.hasMatchingConditionalRules)(mockNumericField, 10, // >= 5 (first value)
            {}, config);
            expect(result).toBe(true);
        });
    });
});
describe('getConditionalFormattingConfig', () => {
    it('selects the last matching rule for a specific apply target', () => {
        const cellConfig = (0, conditionalFormatting_2.createConditionalFormattingConfigWithSingleColor)('#00ff00');
        cellConfig.rules[0].operator = filter_1.FilterOperator.GREATER_THAN_OR_EQUAL;
        cellConfig.rules[0].values = [0];
        const textConfig = (0, conditionalFormatting_2.createConditionalFormattingConfigWithSingleColor)('#ff0000');
        textConfig.applyTo = conditionalFormatting_1.ConditionalFormattingColorApplyTo.TEXT;
        textConfig.rules[0].operator = filter_1.FilterOperator.LESS_THAN_OR_EQUAL;
        textConfig.rules[0].values = [10];
        const conditionalFormattings = [cellConfig, textConfig];
        expect((0, conditionalFormatting_2.getConditionalFormattingConfig)({
            field: mockNumericField,
            value: 5,
            minMaxMap: {},
            conditionalFormattings,
            applyTo: conditionalFormatting_1.ConditionalFormattingColorApplyTo.CELL,
        })).toBe(cellConfig);
        expect((0, conditionalFormatting_2.getConditionalFormattingConfig)({
            field: mockNumericField,
            value: 5,
            minMaxMap: {},
            conditionalFormattings,
            applyTo: conditionalFormatting_1.ConditionalFormattingColorApplyTo.TEXT,
        })).toBe(textConfig);
        expect((0, conditionalFormatting_2.getConditionalFormattingConfig)({
            field: mockNumericField,
            value: 5,
            minMaxMap: {},
            conditionalFormattings,
        })).toBe(textConfig);
    });
});
describe('getPivotRowContextKey', () => {
    it('is stable regardless of key insertion order', () => {
        const a = (0, conditionalFormatting_2.getPivotRowContextKey)({ dim_b: 'y', dim_a: 'x' });
        const b = (0, conditionalFormatting_2.getPivotRowContextKey)({ dim_a: 'x', dim_b: 'y' });
        expect(a).toEqual(b);
    });
    it('distinguishes different values', () => {
        expect((0, conditionalFormatting_2.getPivotRowContextKey)({ dim_a: 'x' })).not.toEqual((0, conditionalFormatting_2.getPivotRowContextKey)({ dim_a: 'z' }));
    });
    it('distinguishes different fields with equal values', () => {
        expect((0, conditionalFormatting_2.getPivotRowContextKey)({ dim_a: 'x' })).not.toEqual((0, conditionalFormatting_2.getPivotRowContextKey)({ dim_b: 'x' }));
    });
    it('normalizes null/undefined consistently', () => {
        expect((0, conditionalFormatting_2.getPivotRowContextKey)({ dim_a: null })).toEqual((0, conditionalFormatting_2.getPivotRowContextKey)({ dim_a: undefined }));
    });
});
describe('getRowConditionalFormattingColor', () => {
    // Reuse the same numeric field fixture used by hasMatchingConditionalRules tests
    const triggerField = {
        name: 'status',
        table: 'orders',
        type: field_1.DimensionType.NUMBER,
        fieldType: field_1.FieldType.DIMENSION,
        sql: 'status',
        tableLabel: 'Orders',
        label: 'Status',
        hidden: false,
    };
    const fieldId = 'orders_status';
    const target = { fieldId };
    const makeMatchingConfig = (color) => {
        const config = (0, conditionalFormatting_2.createConditionalFormattingConfigWithSingleColor)(color, target);
        const rule = (0, conditionalFormatting_2.createConditionalFormattingRuleWithValues)();
        rule.operator = filter_1.FilterOperator.EQUALS;
        rule.values = [42];
        config.rules = [rule];
        config.applyTo = conditionalFormatting_1.ConditionalFormattingColorApplyTo.ROW;
        return config;
    };
    const rowFields = {
        [fieldId]: { field: triggerField, value: 42 },
    };
    it('returns the configured color when trigger field matches', () => {
        const config = makeMatchingConfig('#ff0000');
        const result = (0, conditionalFormatting_2.getRowConditionalFormattingColor)({
            conditionalFormattings: [config],
            rowFields,
            minMaxMap: {},
        });
        expect(result).toBe('#ff0000');
    });
    it('returns null when trigger field value does not match', () => {
        const config = makeMatchingConfig('#ff0000');
        const nonMatchingRowFields = {
            [fieldId]: { field: triggerField, value: 99 },
        };
        const result = (0, conditionalFormatting_2.getRowConditionalFormattingColor)({
            conditionalFormattings: [config],
            rowFields: nonMatchingRowFields,
            minMaxMap: {},
        });
        expect(result).toBeNull();
    });
    it('ignores configs with applyTo: CELL even when rule would match', () => {
        const config = makeMatchingConfig('#00ff00');
        config.applyTo = conditionalFormatting_1.ConditionalFormattingColorApplyTo.CELL;
        const result = (0, conditionalFormatting_2.getRowConditionalFormattingColor)({
            conditionalFormattings: [config],
            rowFields,
            minMaxMap: {},
        });
        expect(result).toBeNull();
    });
    it('returns null when config target is null', () => {
        const config = (0, conditionalFormatting_2.createConditionalFormattingConfigWithSingleColor)('#0000ff', null);
        const rule = (0, conditionalFormatting_2.createConditionalFormattingRuleWithValues)();
        rule.operator = filter_1.FilterOperator.EQUALS;
        rule.values = [42];
        config.rules = [rule];
        config.applyTo = conditionalFormatting_1.ConditionalFormattingColorApplyTo.ROW;
        const result = (0, conditionalFormatting_2.getRowConditionalFormattingColor)({
            conditionalFormattings: [config],
            rowFields,
            minMaxMap: {},
        });
        expect(result).toBeNull();
    });
    it('returns null when conditionalFormattings is undefined', () => {
        const result = (0, conditionalFormatting_2.getRowConditionalFormattingColor)({
            conditionalFormattings: undefined,
            rowFields,
            minMaxMap: {},
        });
        expect(result).toBeNull();
    });
    it('returns color of first matching ROW config when multiple exist', () => {
        const first = makeMatchingConfig('#aaaaaa');
        const second = makeMatchingConfig('#bbbbbb');
        const result = (0, conditionalFormatting_2.getRowConditionalFormattingColor)({
            conditionalFormattings: [first, second],
            rowFields,
            minMaxMap: {},
        });
        expect(result).toBe('#aaaaaa');
    });
});
//# sourceMappingURL=conditionalFormatting.test.js.map