import { Condition } from "./parser.ts";

// inclusive range
export type MaterialRange = {
    x: [number, number],
    m: [number, number],
    a: [number, number],
    s: [number, number],
}

export function partition(cond: Condition, mRange: MaterialRange):
    null | MaterialRange | [MaterialRange, MaterialRange] {
    const [min, max] = mRange[cond.quality];
    if (cond.cond === "<") {
        if (min >= cond.value) return null;
        if (max < cond.value) return mRange;
        return [
            { // false condition
                ...mRange,
                [cond.quality]: [cond.value, max],
            },
            { // true condition
                ...mRange,
                [cond.quality]: [min, cond.value - 1],
            },
        ];
    }
    else { // if >
        if (max <= cond.value) return null;
        if (min > cond.value) return mRange;
        return [{ // false condition
            ...mRange,
            [cond.quality]: [min, cond.value],
        }, { // true condition
            ...mRange,
            [cond.quality]: [cond.value + 1, max],
        }];
    }
}