'use strict';

function prepareESQuery(esQuery) {
    if(!esQuery) {
        esQuery = {
            query: {
                bool: {
                    must: []
                }
            }
        };
    }
    if(!esQuery.query) {
        esQuery.query = {
            bool: {
                must: []
            }
        };
    }
    if(!esQuery.query.bool) {
        esQuery.query.bool = {
            must: []
        };
    }
    if(!esQuery.query.bool.must) {
        esQuery.query.bool.must = [];
    }

    return esQuery;
};

function stringifyQuery(esQuery){
    // parse undefined and null values to `undefined`, `null`
    // see details: https://mantis.formbird.com/view.php?id=11986#c48621
    return JSON.stringify(esQuery, function(k, v) { return v == null ? `${v}` : v; });
}


module.exports = {
    prepareESQuery: prepareESQuery,
    stringifyQuery : stringifyQuery
};
