export default function(context) {
  console.log("initialize.........");
  let proto = "https";
  let url = "";
  let cookies = "";
  // Server-side
  if (process.server) {
    console.log("server side............");
    const { req } = context;
    let host = req.headers.host;
    let proto = "https";
    if (req.headers["x-forwarded-proto"]) {
      proto = req.headers["x-forwarded-proto"];
    } else if (req.headers["x-scheme"]) {
      proto = req.headers["x-scheme"];
    } else if (host.indexOf("local.") != -1) {
      proto = "http";
    }
    url = proto + "://" + host;
    cookies = req.headers.cookie;
  }
  // Client-side
  if (process.client) {
    console.log("client side............");
    url = document.location.href;
    let proto = "http";
    if (url.indexOf("https://") != -1) {
      proto = "https";
    }
    cookies = document.cookie;
  }
  let regx = new RegExp(
    "((?<branch>[a-z0-9_\\-]+)\\.)?(?<env>(www|prod|test|local|test-jishuin|test-rooter|prod-jishuin|prod-rooter|rooter|jishuin))\\.proginn\\.com(\\:(?<port>\\d+))?",
    "i"
  );
  console.log(regx);
  console.log(url);
  let groups = regx.exec(url).groups;
  let api = "";
  let jishuinUrl = "";
  let rooterUrl = "";
  let siteUrl = "";
  if (groups) {
    let { branch, env, port } = groups;
    switch (env) {
      case "www":
        api = "https://www.proginn.com";
        siteUrl = "https://www.proginn.com";
        jishuinUrl = "https://jishuin.proginn.com";
        rooterUrl = "https://rooter.proginn.com";
        break;
      case "test":
      case "test-jishuin":
      case "test-rooter":
        api = "https://" + branch + ".test.proginn.com";
        siteUrl = "https://" + branch + ".test.proginn.com";
        jishuinUrl = "https://" + branch + ".test-jishuin.proginn.com";
        rooterUrl = "https://" + branch + ".test-rooter.proginn.com";
        break;
      case "local":
        if (port && port != "80") {
          api = "http://local.proginn.com:" + port;
          siteUrl = "http://local.proginn.com:" + port;
          jishuinUrl = "http://local-jishuin.proginn.com";
          rooterUrl = "http://local-rooter.proginn.com";
        } else {
          api = "http://local.proginn.com";
          siteUrl = "http://local.proginn.com";
          jishuinUrl = "http://local-jishuin.proginn.com";
          rooterUrl = "http://local-rooter.proginn.com";
        }
        break;
      case "prod":
      case "prod-jishuin":
      case "prod-rooter":
        api = "https://" + branch + ".prod.proginn.com";
        siteUrl = "https://" + branch + ".prod.proginn.com";
        jishuinUrl = "https://" + branch + ".prod-jishuin.proginn.com";
        rooterUrl = "https://" + branch + ".prod-rooter.proginn.com";
        break;
      default:
        // 按生产处理
        api = "https://www.proginn.com";
        siteUrl = "https://www.proginn.com";
        jishuinUrl = "https://jishuin.proginn.com";
        rooterUrl = "https://rooter.proginn.com";
        break;
    }
  } else {
    // 按生产处理
    api = "https://www.proginn.com";
    siteUrl = "https://www.proginn.com";
    jishuinUrl = "https://jishuin.proginn.com";
    rooterUrl = "https://rooter.proginn.com";
  }
  context.store.dispatch("user/getInfo");
  context.store.commit("updateDomainConfig", {
    api: api,
    siteUrl: siteUrl,
    jishuinUrl: jishuinUrl,
    rooterUrl: rooterUrl
  });
}
