export function buildQuery(filter) {
    if (!filter['where']) { // shorthand query
        var where = Object.keys(filter)[0];
        var equal = filter[where];
        delete filter[where]; // remove shorthand value from filter
        filter['where'] = where;
        filter['equal'] = equal;
    }
    return filter;
}
export function buildAdvancedFilter(query) {
    var advancedFilter;
    // build advanced filter
    var _a = query, where = _a.where, equal = _a.equal, exists = _a.exists, contains = _a.contains, lt = _a.lt, lte = _a.lte, gt = _a.gt, gte = _a.gte, childExists = _a.childExists, childEqual = _a.childEqual;
    if (!!equal) { // where/equal
        advancedFilter = function (item) { return (!!item[where] && item[where] === equal); };
    }
    else if (typeof exists === 'boolean') { // where/exists/not exists
        advancedFilter = function (item) { return (!!exists ? !!item[where] : !item[where]); };
    }
    else if (!!contains) { // where/contains
        advancedFilter = function (item) { return (!!item[where] &&
            typeof item[where] === 'string' &&
            item[where].indexOf(contains) > -1); };
    }
    else if (!!lt) { // where/less than
        advancedFilter = function (item) { return (!!item[where] &&
            typeof item[where] === 'number' &&
            item[where] < lt); };
    }
    else if (!!lte) { // where/less than or equal
        advancedFilter = function (item) { return (!!item[where] &&
            typeof item[where] === 'number' &&
            item[where] <= lte); };
    }
    else if (!!gt) { // where/greater than
        advancedFilter = function (item) { return (!!item[where] &&
            typeof item[where] === 'number' &&
            item[where] > gt); };
    }
    else if (!!gte) { // where/greater than or equal
        advancedFilter = function (item) { return (!!item[where] &&
            typeof item[where] === 'number' &&
            item[where] >= gte); };
    }
    else if (!!childExists) { // where/child exists, not exists
        var notExists_1 = childExists.substr(0, 1) === '!';
        var child_1 = notExists_1 ? childExists.replace('!', '') : childExists;
        advancedFilter = function (item) {
            if (!item[where] && notExists_1) {
                return true; // child always not exists
            }
            else if (!!item[where]) {
                if (item[where] instanceof Array) {
                    return notExists_1 ?
                        (item[where].indexOf(child_1) < 0) :
                        (item[where].indexOf(child_1) > -1);
                }
                else if (item[where] instanceof Object) {
                    return notExists_1 ? !item[where][child_1] : !!item[where][child_1];
                }
            }
            return false;
        };
    }
    else if (!!childEqual) { // where/child equal, not equal
        var notEqual_1;
        var childKey_1;
        var childValue_1;
        if (childEqual.indexOf('!=') > -1) {
            notEqual_1 = true;
            var keyValue = childEqual.split('!=').filter(Boolean);
            childKey_1 = keyValue[0];
            childValue_1 = keyValue[1];
        }
        else {
            var keyValue = childEqual.split('=').filter(Boolean);
            childKey_1 = keyValue[0];
            childValue_1 = keyValue[1];
        }
        advancedFilter = function (item) {
            if (!item[where] && notEqual_1) {
                return true; // always not equal
            }
            else if (!!item[where]) {
                return (item[where] instanceof Object &&
                    (notEqual_1 ?
                        (!item[where][childKey_1] || item[where][childKey_1] !== childValue_1) :
                        (!!item[where][childKey_1] && item[where][childKey_1] === childValue_1)));
            }
            return false;
        };
    }
    return advancedFilter;
}
