import { expect } from "chai";
import { generateRangeComparisonExpression } from "../../../source/text/generate-range-comparison-expression.mjs";

describe('generateRangeComparisonExpression', function () {

    it('soll Einzelwerte korrekt behandeln', function () {
        expect(generateRangeComparisonExpression("5", "x")).to.equal("x==5");
        expect(generateRangeComparisonExpression("0", "age")).to.equal("age==0");
        expect(generateRangeComparisonExpression("-3", "score")).to.equal("score<=3");
        expect(generateRangeComparisonExpression("7.5", "n")).to.equal("n==7.5");
    });

    it('soll einfache inklusive Ranges korrekt behandeln', function () {
        expect(generateRangeComparisonExpression("4-6", "x")).to.equal("x>=4 && x<=6");
        expect(generateRangeComparisonExpression("10-15", "a")).to.equal("a>=10 && a<=15");
        expect(generateRangeComparisonExpression("-5-5", "y")).to.equal("y>=-5 && y<=5");
        expect(generateRangeComparisonExpression("4.1-6.2", "x")).to.equal("x>=4.1 && x<=6.2");
    });

    it('soll exklusive Ranges (mit ~) korrekt behandeln', function () {
        expect(generateRangeComparisonExpression("4~6", "x")).to.equal("x>4 && x<6");
        expect(generateRangeComparisonExpression("10~15", "a")).to.equal("a>10 && a<15");
        expect(generateRangeComparisonExpression("-4~4", "y")).to.equal("y>-4 && y<4");
    });

    it('soll offene Intervalle links korrekt behandeln', function () {
        expect(generateRangeComparisonExpression("-6", "x")).to.equal("x<=6");
        expect(generateRangeComparisonExpression("-2", "y")).to.equal("y<=2");
        expect(generateRangeComparisonExpression("  -3 ", "z")).to.equal("z<=3");
    });

    it('soll offene Intervalle rechts korrekt behandeln', function () {
        expect(generateRangeComparisonExpression("4-", "x")).to.equal("x>=4");
        expect(generateRangeComparisonExpression("10-", "z")).to.equal("z>=10");
        expect(generateRangeComparisonExpression(" -5- ", "y")).to.equal("y>=-5");
    });

    it('soll die Operatoren < und > korrekt behandeln', function () {
        expect(generateRangeComparisonExpression(">4", "x")).to.equal("x>4");
        expect(generateRangeComparisonExpression("<6", "x")).to.equal("x<6");
        expect(generateRangeComparisonExpression(">-4.2", "n")).to.equal("n>-4.2");
        expect(generateRangeComparisonExpression("--2", "x")).to.equal("x<=-2");
    });

    it('soll mehrere Bedingungen per Komma ODER-verknüpfen', function () {
        expect(generateRangeComparisonExpression("4-6,10,20~25", "x"))
            .to.equal("(x>=4 && x<=6) || (x==10) || (x>20 && x<25)");
        expect(generateRangeComparisonExpression("0,5-10", "y"))
            .to.equal("(y==0) || (y>=5 && y<=10)");
        expect(generateRangeComparisonExpression(">1,<3", "z"))
            .to.equal("(z>1) || (z<3)");
    });

    it('soll urlEncode korrekt unterstützen', function () {
        expect(generateRangeComparisonExpression("4-6", "x", { urlEncode: true }))
            .to.equal("x%3E%3D4%20%26%26%20x%3C%3D6");
        expect(generateRangeComparisonExpression("10", "n", { urlEncode: true }))
            .to.equal("n%3D%3D10");
    });

    it('soll Fehler werfen bei falschen Eingaben', function () {
        expect(() => generateRangeComparisonExpression("", "x")).to.throw("Invalid input");
        expect(() => generateRangeComparisonExpression("abc", "x")).to.throw("Invalid value 'abc'");
        expect(() => generateRangeComparisonExpression("4-3", "x")).to.throw("Invalid range '4-3'");
        expect(() => generateRangeComparisonExpression("4-foo", "x")).to.throw("Invalid value '4-foo'");
        expect(() => generateRangeComparisonExpression("4~foo", "x")).to.throw("Invalid value '4~foo'");
        expect(() => generateRangeComparisonExpression(">", "x")).to.throw("Invalid value in '>'");
        expect(() => generateRangeComparisonExpression("<", "x")).to.throw("Invalid value in '<'");
        expect(() => generateRangeComparisonExpression("--", "x")).to.throw("Invalid range '--'");
        expect(() => generateRangeComparisonExpression("-foo", "x")).to.throw("Invalid value '-foo'");
        expect(() => generateRangeComparisonExpression("foo-", "x")).to.throw("Invalid value 'foo-'");
        expect(() => generateRangeComparisonExpression("5~", "x")).to.not.throw();
        expect(() => generateRangeComparisonExpression("~5", "x")).to.not.throw();
        expect(() => generateRangeComparisonExpression(">", "")).to.throw("Invalid value in '>'");
        expect(() => generateRangeComparisonExpression("1,,2", "x")).not.to.throw();
        expect(() => generateRangeComparisonExpression(",", "x")).to.not.throw();
    });

    it('soll Leerzeichen robust behandeln', function () {
        expect(generateRangeComparisonExpression(" 4 - 6 ,  8 ", "x"))
            .to.equal("(x>=4 && x<=6) || (x==8)");
        expect(generateRangeComparisonExpression("   -10--5   ", "temp"))
            .to.equal("temp>=-10 && temp<=-5");
    });

    it('soll negative Werte korrekt behandeln', function () {
        expect(generateRangeComparisonExpression("-10--5", "temp"))
            .to.equal("temp>=-10 && temp<=-5");
        expect(generateRangeComparisonExpression("> -3", "z")).to.equal("z>-3");
        expect(generateRangeComparisonExpression(" -4~4 ", "y")).to.equal("y>-4 && y<4");
        expect(generateRangeComparisonExpression("-5", "x")).to.equal("x<=5");

    });
});
