export class FilterEngine {
    constructor(filterPluginOptions) {
        this.filterFuncRule = [], this.filterValueRule = [], this.pluginOptions = filterPluginOptions;
    }
    applyFilter(state, table) {
        var _a;
        const {filters: filters} = state;
        this.filterFuncRule = [], this.filterValueRule = [], filters.forEach(((filter, field) => {
            if (filter.enable) if ("byValue" === filter.type) this.filterValueRule.push({
                filterKey: String(filter.field),
                filteredValues: filter.values
            }); else if ("byCondition" === filter.type) {
                const filterFunc = this.createFilterFunction(filter);
                this.filterFuncRule.push({
                    filterFunc: filterFunc,
                    fieldId: String(filter.field)
                });
            }
        })), table.updateFilterRules([ ...this.filterFuncRule, ...this.filterValueRule ], {
            clearRowHeightCache: !1,
            onFilterRecordsEnd: null === (_a = this.pluginOptions) || void 0 === _a ? void 0 : _a.onFilterRecordsEnd
        });
    }
    createFilterFunction(filter) {
        const {field: field, operator: operator, condition: condition} = filter;
        return operator && void 0 !== condition ? record => {
            const value = record[field];
            if (null == value) return !1;
            switch (operator) {
              case "equals":
                return 0 === this.compareValues(value, condition);

              case "notEquals":
                return 0 !== this.compareValues(value, condition);

              case "greaterThan":
                return this.compareValues(value, condition) > 0;

              case "lessThan":
                return this.compareValues(value, condition) < 0;

              case "greaterThanOrEqual":
                return this.compareValues(value, condition) >= 0;

              case "lessThanOrEqual":
                return this.compareValues(value, condition) <= 0;

              case "between":
                return !Array.isArray(condition) || 2 !== condition.length || this.compareValues(value, condition[0]) >= 0 && this.compareValues(value, condition[1]) <= 0;

              case "notBetween":
                return !Array.isArray(condition) || 2 !== condition.length || (this.compareValues(value, condition[0]) < 0 || this.compareValues(value, condition[1]) > 0);

              case "contains":
                return String(value).toLowerCase().includes(String(condition).toLowerCase());

              case "notContains":
                return !String(value).toLowerCase().includes(String(condition).toLowerCase());

              case "startsWith":
                return String(value).toLowerCase().startsWith(String(condition).toLowerCase());

              case "notStartsWith":
                return !String(value).toLowerCase().startsWith(String(condition).toLowerCase());

              case "endsWith":
                return String(value).toLowerCase().endsWith(String(condition).toLowerCase());

              case "notEndsWith":
                return !String(value).toLowerCase().endsWith(String(condition).toLowerCase());

              case "isChecked":
                return !0 === Boolean(value);

              case "isUnchecked":
                return !1 === Boolean(value);

              default:
                return !0;
            }
        } : () => !0;
    }
    compareValues(value, condition) {
        if ("number" == typeof value && "number" == typeof condition) return value === condition ? 0 : value > condition ? 1 : -1;
        if ("boolean" == typeof value && "boolean" == typeof condition) return value === condition ? 0 : -1;
        const valueStr = String(value).toLowerCase(), conditionStr = String(condition).toLowerCase();
        return isNaN(Number(valueStr)) || isNaN(Number(conditionStr)) ? valueStr === conditionStr ? 0 : valueStr > conditionStr ? 1 : -1 : Number(valueStr) === Number(conditionStr) ? 0 : Number(valueStr) > Number(conditionStr) ? 1 : -1;
    }
    clearAllFilter(table) {
        this.filterFuncRule = [], this.filterValueRule = [], table.updateFilterRules([]);
    }
    clearFilter(table, fieldId) {
        this.filterValueRule = this.filterValueRule.filter((rule => rule.filterKey !== fieldId)), 
        this.filterFuncRule = this.filterFuncRule.filter((rule => rule.fieldId !== fieldId)), 
        table.updateFilterRules([ ...this.filterFuncRule, ...this.filterValueRule ]);
    }
}
//# sourceMappingURL=filter-engine.js.map