import { autoinject } from "aurelia-framework";
import { Config } from "../config";
import * as breeze from 'breeze-client';

@autoinject
export class PredicateUtils {
    constructor(private config: Config) { }

    public getPredicates(filterText: string, filterProperty: string, propType: breeze.DataTypeSymbol, queryOp: breeze.FilterQueryOpSymbol): Array<breeze.Predicate> {
        let predicates: Array<breeze.Predicate> = new Array<breeze.Predicate>();
        let filterTexts: Array<string> = filterText.split(this.config?.agGridConfig?.stringSeparatorAgGridFilter ?? '');
        if (filterTexts.length > 0) {
            let predicatesTemp = filterTexts
                .filter(filterText => filterText.length > 0 && false == ((propType?.isNumeric ?? false) && isNaN(filterText)))
                .map(filterText => new breeze.Predicate(filterProperty, queryOp, { value: filterText, isLiteral: true, dataType: propType }));
            if (predicatesTemp.length > 0) {
                predicates.push(...predicatesTemp);
            }
        }
        return predicates;
    }
}