const path = require('path');
const debug = require('debug')('app:config');
let config = require('./local');

const NODE_ENV = process.env.NODE_ENV || 'development';

debug('Creating default configuration.');
const argv = JSON.parse(process.env.npm_config_argv);
const project = argv.remain.length > 0 ? argv.remain[0] : 'admin';

debug('Current Project -> ' + project);

let projectConfig = require(`../project/${project}/local`);

config = Object.assign(
  {
    env: NODE_ENV,
    path_base: path.resolve(__dirname, '..'),
    dir_client: 'src',
    dir_dist: 'dist',
    dir_lib: 'lib',
    dir_components: 'components',
    basename: '/',
    dir_project: 'project/' + project,
    api_path: `/api`,
  },
  config[NODE_ENV],
  projectConfig[NODE_ENV],
);

function base() {
  const args = [config.path_base].concat([].slice.call(arguments));
  return path.resolve.apply(path, args);
}
config.utils_paths = {
  base,
  client: base.bind(null, config.dir_client),
  components: base.bind(null, config.dir_components),
  dist: base.bind(null, config.dir_dist),
  lib: base.bind(null, config.dir_lib),
  project: base.bind(null, config.dir_project),
};

config.globals = {
  'process.env': {
    NODE_ENV: JSON.stringify(config.env),
  },
  NODE_ENV: config.env,
  __DEV__: config.env === 'development',
  __PROD__: config.env === 'production',
  __TEST__: config.env === 'test',
  __DEBUG__: config.env === 'development' || config.env === 'test',
  __API_PATH__: `\'${config.api_path}\'`,
  __BASE_NAME__: `\'${config.basename}\'`,
};

debug(`Looking for environment overrides for NODE_ENV '${config.env}'.`);
const environments = require('./environments');

const overrides = environments[config.env];
if (overrides) {
  debug('Found overrides, applying to default configuration.');
  Object.assign(config, overrides());
} else {
  debug('No environment overrides found, defaults will be used.');
}
module.exports = config;
