const debug = require('debug')('ms-conf');
const Confidence = require('confidence');
const loadConfig = require('./load-config');

// uses confidence API to access store
let store;

// use this on sighup
function reload() {
  debug('reloading configuration');
  store = new Confidence.Store(loadConfig());
}

// hot-reload enabler
function enableReload() {
  debug('enabling sigusr');
  process.on('SIGUSR1', reload);
}

// hot-reload disabler
function disableReload() {
  debug('disabling sigusr');
  process.removeListener('SIGUSR1', reload);
}

function get(key, opts) {
  return store.get(key, opts);
}

function meta(key, opts) {
  return store.meta(key, opts);
}

// init first configuration
module.exports = exports = {
  reload,
  enableReload,
  disableReload,
  prependDefaultConfiguration: loadConfig.prependDefaultConfiguration,
};

// small util to ensure effective get calls
function ensureStoreWasLoaded(obj, name, fn) {
  Object.defineProperty(obj, name, {
    configurable: true,
    enumerable: true,
    value: function loadStore(...args) {
      if (!store) reload();
      Object.defineProperty(obj, name, {
        configurable: true,
        enumerable: true,
        value: fn,
      });
      return fn(...args);
    },
  });
}

ensureStoreWasLoaded(exports, 'get', get);
ensureStoreWasLoaded(exports, 'meta', meta);

@@ -1081,16 +1081,155 @@
 load();%0A
+%0A      // if it is the same property we can redefine it, otherwise%0A      // it must remain the same%0A      if (obj%5Bname%5D === loadStore) %7B%0A  
       Ob
@@ -1265,24 +1265,26 @@
 , %7B%0A        
+  
 configurable
@@ -1295,24 +1295,26 @@
 ue,%0A        
+ 
+ 
 enumerable: 
@@ -1323,24 +1323,26 @@
 ue,%0A        
+  
 value: fn,%0A 
@@ -1340,16 +1340,18 @@
 ue: fn,%0A
+  
       %7D)
@@ -1352,16 +1352,25 @@
     %7D);%0A
+      %7D%0A%0A
       re

verify overload