'use strict';

//setting default index name as db name as mongo connector 2.3 creates index with db name.
var indexTypeName = "documents";
var elasticIndexName;
var disableIndexing;

const searchConfig = (connectionName) =>{
    let esConfig = {};
    const defaultConnectionName = 'elasticsearch';
    const ds =  config.get('dataServices');

    if (connectionName){
        if (ds){
            esConfig =  ds[connectionName];
        }
    }
    else{
        if (ds){
            esConfig =  ds["defaultSearch"];
            if (esConfig && esConfig.providerName !== "elastic"){
                esConfig = {};
            }
        }
    }

    if (!esConfig){
        esConfig =  config.get(defaultConnectionName);
    }

    return esConfig;
};
module.exports = {
    indexTypeName: indexTypeName,
    elasticIndexName: function() {
        if (!elasticIndexName) {
            elasticIndexName = searchConfig().indexName;
        }

        return elasticIndexName;
    },
    disableIndexing: function() {
        if (!disableIndexing && disableIndexing !== false) {
            const sConfig = searchConfig();

            if (!sConfig) {
                disableIndexing = true;
            } else {
                const ds = config.get("dataServices");
                disableIndexing = (sConfig.disableIndexing === true ||
                    (ds.defaultSearch && ds.defaultSearch.providerName !== "elastic") );
            }
        }

        return disableIndexing;
    },

    searchConfig: searchConfig
};
