'use strict';

const elasticConnection   = require('../../../search/ElasticConnection');
const UtilElasticSearch = require('../../../../server/utils/UtilElasticSearch.js');
const healthCheckConstants     = require('../../../../shared/constants/HealthCheckConstants');

let esClient;

function getEsClient() {
  if (!esClient) {
    esClient = elasticConnection.esConnection(UtilElasticSearch.searchConfig());
  }

  return esClient;
}

exports.check =  (options) => {
  return new Promise((resolve, reject) => {
    const esClient = getEsClient();
    esClient.cluster.health({}, (err, ping) => {
      if (err) {
        reject(err);
      } else {
        resolve(healthCheckConstants.OK);
      }
    });
  });
};
