import fs from 'fs'
import Nconf from 'nconf-lite'
import { Util } from 'service-core'

const nconf = new Nconf()

// Helper method for global usage.
nconf.inTest = () => nconf.get('NODE_ENV') === 'test'

// Config follow the following priority check order:
// 1. Enviroment variables
// 2. package.json
// 3. config/config.json
// 4. config/config.default.json

// Load enviroment variables as first priority
nconf.env({
  separator: '__',
  whitelist: [
    'NODE_ENV',
    'server__port',
    'server__host',
    'bunyan__name',
    'frontend__url',
    'fileSize',
    'name',
    'NODE_VERSION',
  ],
  parseValues: true,
})


// Load empty overrides that can be overwritten later
nconf.overrides({})

let util = new Util(import.meta.url)
let pckg = JSON.parse(fs.readFileSync(util.getPathFromRoot(`../package.json`)))

nconf.defaults({
  "name": pckg.name,
  "version": pckg.version,
  "NODE_ENV": "development",
  "server": {
    "port": 4040,
    "host": "0.0.0.0"
  },
  "bunyan": {
    "name": "storage-upload",
    "streams": [{
        "stream": "process.stdout",
        "level": "debug"
      }
    ]
  },
  "sites": {
  },
  "uploadFolder": "./public",
  "fileSize": 524288000
})

export default nconf
