import { Verify } from '../validation/Verify';
var numberRegex = /[\d.]/;
var CwFilter = (function () {
    function CwFilter() {
    }
    CwFilter.transformValue = function (fieldValue) {
        var xform = fieldValue;
        if (Verify.exists(fieldValue)) {
            if (fieldValue === 'true') {
                xform = true;
            }
            else if (fieldValue === 'false') {
                xform = false;
            }
            else if (fieldValue.match(numberRegex)) {
                xform = Number.parseFloat(fieldValue);
            }
        }
        return xform;
    };
    CwFilter.isFiltered = function (obj, filterText) {
        var _this = this;
        var isFiltered = false;
        if (filterText !== '') {
            var filter_1 = filterText;
            var re = /([\w]*[:][\w]*)/g;
            var matches = filterText.match(re);
            if (matches != null) {
                // 'match' is now an array of the field filters.
                matches.forEach(function (match) {
                    var terms = match.split(':');
                    filter_1 = filter_1.replace(match, '');
                    if (!isFiltered) {
                        var fieldName = terms[0];
                        var fieldValue = terms[1];
                        var hasField = obj.hasOwnProperty(fieldName) || obj.hasOwnProperty('_' + fieldName);
                        if (hasField) {
                            try {
                                isFiltered = (obj[fieldName] !== fieldValue && obj[fieldName] !== _this.transformValue(fieldValue));
                            }
                            catch (e) {
                                // tslint:disable-next-line:no-console
                                console.log('Error while trying to check a field value while filtering.', e);
                            }
                        }
                    }
                });
            }
            filter_1 = filter_1.trim().toLowerCase();
            if (filter_1) {
                isFiltered = isFiltered || !(obj.name.toLowerCase().indexOf(filter_1) >= 0);
            }
        }
        return isFiltered;
    };
    return CwFilter;
}());
export { CwFilter };
//# sourceMappingURL=CwFilter.js.map