// @flow

/**
 * This is a helper function to generate links for the With column of DisplayTable.
 */

const withLinkGenerator = (id: string, db: string, name: ?string) => {
  let baseUrl
  if (process.env.NODE_ENV === "production") {
    baseUrl = `${process.env.REACT_APP_BASENAME}/`
  } else {
    baseUrl = "/"
  }
  if (id === undefined) {
    return "#"
  }
  // if using converted gene name, link to gene page not uniprot
  if (name) {
    if (name.includes(" ")) {
      return `https://www.ebi.ac.uk/QuickGO/term/GO:${id}`
    }
    if (name.match(/^([A-Z0-9]*$)/)) {
      return `https://www.uniprot.org/uniprot/${id}`
    }
    return `${baseUrl}${name}`
  }
  switch (db) {
    case "CHEBI": {
      return `https://www.ebi.ac.uk/chebi/searchId.do?chebiId=CHEBI:${id}`
    }
    case "DDB":
      return `${baseUrl}${id}`
    case "dictyBase":
      return `${baseUrl}${id}`
    case "FB":
      return `http://flybase.org/reports/${id}.html`
    case "GO":
      return `https://www.ebi.ac.uk/QuickGO/term/GO:${id}`
    case "InterPro":
      return `http://www.ebi.ac.uk/interpro/entry/${id}`
    case "MGI":
      return `http://www.informatics.jax.org/marker/${id}`
    case "PANTHER":
      return `http://www.pantree.org/node/annotationNode.jsp?id=${id}`
    case "PomBase":
      return `https://www.pombase.org/spombe/result/${id}`
    case "RGD":
      return `https://rgd.mcw.edu/rgdweb/report/gene/main.html?id=${id}`
    case "SGD":
      return `https://www.yeastgenome.org/locus/${id}`
    case "SO":
      return `http://www.sequenceontology.org/browser/current_svn/term/SO:${id}`
    case "TAIR":
      return `https://www.arabidopsis.org/servlets/TairObject?accession=${id}`
    case "UniProtKB":
      return `https://www.uniprot.org/uniprot/${id}`
    case "UniProtKB-KW":
      return `https://www.uniprot.org/keywords/${id}`
    case "UniProtKB-SubCell":
      return `https://www.uniprot.org/locations/${id}`
    case "WB":
      return `https://wormbase.org/search/cds/${id}`
    case "ZFIN":
      return `https://zfin.org/${id}`
    default:
      return "#"
  }
}

export default withLinkGenerator

@@ -171,153 +171,8 @@
 %3E %7B%0A
-  let baseUrl%0A  if (process.env.NODE_ENV === %22production%22) %7B%0A    baseUrl = %60$%7Bprocess.env.REACT_APP_BASENAME%7D/%60%0A  %7D else %7B%0A    baseUrl = %22/%22%0A  %7D%0A
   if
@@ -496,26 +496,17 @@
 return %60
-$%7BbaseUrl%7D
+/
 $%7Bname%7D%60
@@ -653,34 +653,25 @@
     return %60
-$%7BbaseUrl%7D
+/
 $%7Bid%7D%60%0A    c
@@ -705,18 +705,9 @@
 rn %60
-$%7BbaseUrl%7D
+/
 $%7Bid

remove basename from go tab extension/with links