import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
import defaultConfig from './config.default';

export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<EggAppConfig>;

  config.cluster = {
    listen: {
      port: 7500
    }
  };
  
  config.mongoose = {
    url: 'mongodb://127.0.0.1:27017/zyz', // 内网
    options: { useNewUrlParser: true, useUnifiedTopology: true },
  };
  config.gmmongoose = {
    url: 'mongodb://127.0.0.1:27017/zyzgm', // 内网
    options: { useNewUrlParser: true, useUnifiedTopology: true },
  };
  config.redis = {
    url: '127.0.0.1', // 内网
    pw: ''
  };

  let regions = [    // 大区数据
    { id: 1, env: 'development', name: "测试", domain: 'http://127.0.0.1:9000' },
  ];
  config.regions = regions;

  let httpProxy: any = {};
  for(let { env, domain } of regions) {
    httpProxy[`/api/${env}/`] = {
      target: domain,
      changeOrigin: true,
      secure: true,
      pathRewrite: function(path) {
        console.log('proxy', path, path.replace(`/api/${env}/`, '/api/'))
        return path.replace(`/api/${env}/`, '/api/') 
      }
    }
  }
  config.proxy = httpProxy;

  // the return config will combines to EggAppConfig
  return {
    ...defaultConfig(appInfo),
    ...config,
  };
};
