import {expect} from "chai"
import {generateRangeComparisonExpression} from "../../../source/text/util.mjs";

describe('generateRangeComparisonExpression', () => {
    it('should generate correct comparison expression for single values', () => {
        const expression = '1,3,5';
        const valueName = 'x';
        const result = generateRangeComparisonExpression(expression, valueName);
        expect(result).to.equal('(x==1) || (x==3) || (x==5)');
    });

    it('should generate correct comparison expression for ranges', () => {
        const expression = '1-3,6-8';
        const valueName = 'x';
        const result = generateRangeComparisonExpression(expression, valueName);
        expect(result).to.equal('(x>=1 && x<=3) || (x>=6 && x<=8)');
    });

    it('should generate correct comparison expression for mixed ranges and single values', () => {
        const expression = '1-3,5,7-9';
        const valueName = 'x';
        const result = generateRangeComparisonExpression(expression, valueName);
        expect(result).to.equal('(x>=1 && x<=3) || (x==5) || (x>=7 && x<=9)');
    });

    it('should throw an error for invalid range', () => {
        const expression = '1-3,5-4';
        const valueName = 'x';
        expect(() => generateRangeComparisonExpression(expression, valueName)).to.throw(`Invalid range '5-4'`);
    });


    it('should throw an error for invalid value', () => {
        const expression = '1-3,a';
        const valueName = 'x';
        expect(() => generateRangeComparisonExpression(expression, valueName)).to.throw('Invalid value');
    });

    it('should generate correct comparison expression with custom operators', () => {
        const expression = '1-3,5';
        const valueName = 'x';
        const options = {
            andOp: 'AND',
            orOp: 'OR',
            eqOp: '===',
            geOp: '>=',
            leOp: '<=',
        };
        const result = generateRangeComparisonExpression(expression, valueName, options);
        expect(result).to.equal('(x>=1 AND x<=3) OR (x===5)');
    });

    it('should generate correct comparison expression with urlEncode option', () => {
        const testCases = [
            {
                expression: '1,3,5',
                valueName: 'x',
                expected: '(x%3D%3D1)%20%7C%7C%20(x%3D%3D3)%20%7C%7C%20(x%3D%3D5)',
            },
            {
                expression: '-10',
                valueName: 'x',
                expected: 'x%3C%3D10',
            },
            {
                expression: '10-',
                valueName: 'x',
                expected: 'x%3E%3D10',
            },
            {
                expression: '1-3,6-8',
                valueName: 'y',
                expected: '(y%3E%3D1%20%26%26%20y%3C%3D3)%20%7C%7C%20(y%3E%3D6%20%26%26%20y%3C%3D8)',
            },
            {
                expression: '1-3,5,7-9',
                valueName: 'z',
                expected: '(z%3E%3D1%20%26%26%20z%3C%3D3)%20%7C%7C%20(z%3D%3D5)%20%7C%7C%20(z%3E%3D7%20%26%26%20z%3C%3D9)',
            },
        ];

        testCases.forEach(({expression, valueName, expected}) => {
            const result = generateRangeComparisonExpression(expression, valueName, {urlEncode: true});
            expect(result).to.equal(expected);
        });
    });

    it('should generate correct comparison expression for open-ended ranges with urlEncode option', () => {
        const testCases = [
            {
                expression: '10-',
                valueName: 'x',
                expected: 'x%3E%3D10',
            },
            {
                expression: '-10',
                valueName: 'y',
                expected: 'y%3C%3D10',
            },
        ];

        testCases.forEach(({expression, valueName, expected}) => {
            const result = generateRangeComparisonExpression(expression, valueName, {urlEncode: true});
            expect(result).to.equal(expected);
        });
    });


});
