
import superagent from 'superagent';
import util from 'src/util/util';
import ui from 'src/util/ui';
import _ from 'lodash';

module.exports = {
  search(term) {
    return superagent.get(`https://www.chemexper.com/search/reference/json2/quick/${encodeURIComponent(term)}`)
      .then(function (result) {
        result = result.body && result.body.entry;
        if (!result) {
          ui.showNotification('No results in reference DB', 'warn');
          return Promise.resolve([]);
        }
        var list = [];
        for (var i = 0; i < result.length; i++) {
          if (result[i] && result[i].value) {
            var val = result[i].value;
            val.code = val.catalogID;
            list.push({
              id: i,
              name: (val && val.iupac && val.iupac[0]) ? val.iupac[0].value : '',
              row: val
            });
          }
        }
        return list;
      }).then((data) => data.map(fromChemexper)).then(
        (data) => data.sort((a, b) => {
          let rn1 = (a.$content.identifier.cas.length > 0) ?
            Number(a.$content.identifier.cas[0].value.replace(/-/g, '')) :
            Number.MAX_SAFE_INTEGER;
          let rn2 = (b.$content.identifier.cas.length > 0) ?
            Number(b.$content.identifier.cas[0].value.replace(/-/g, '')) :
            Number.MAX_SAFE_INTEGER;
          return rn1 - rn2;
        })
      );
  }
};

function fromChemexper(chemexper) {
  const mol = chemexper.row.mol;
  const mf = chemexper.row.mf && chemexper.row.mf[0] && chemexper.row.mf[0].value.value;
  const cas = chemexper.row.rn && chemexper.row.rn.map((rn) => ({ value: numberToCas(rn.value.value) }));
  if (!chemexper.row.iupac) chemexper.row.iupac = [];
  return {
    $content: {
      general: {
        molfile: mol && mol[0] && mol[0].value.value,
        description: chemexper.name,
        name: chemexper.row.iupac,
        mf
      },
      identifier: {
        cas
      },
      stock: {
        catalogNumber: chemexper.row.code
      },
      physical: {
        density: chemexper.row.density,
        mp: chemexper.row.mp,
        bp: chemexper.row.bp
      }
    },
    id: util.getNextUniqueId(true),
    names: _.uniq([chemexper.name, ...chemexper.row.iupac.map((i) => i.value)]),
    source: 'reference'
  };
}


function numberToCas(nb) {
  nb = String(nb);
  return `${nb.slice(0, -3)}-${nb.slice(-3, -1)}-${nb.slice(-1)}`;
}

@@ -1,13 +1,12 @@
-%0A
 import super
@@ -180,25 +180,50 @@
 gent
-.get(%60https://www
+%0A      .get(%0A        %60https://mastersearch
 .che
@@ -287,15 +287,42 @@
 ent(
-term)%7D%60
+%0A          term%0A        )%7D%60%0A      
 )%0A  
@@ -803,17 +803,16 @@
   name: 
-(
 val && v
@@ -835,17 +835,16 @@
 iupac%5B0%5D
-)
  ? val.i
@@ -953,16 +953,23 @@
       %7D)
+%0A      
 .then((d
@@ -1004,23 +1004,21 @@
 er))
-.then(
 %0A      
-  
+.then(
 (dat
@@ -1018,24 +1018,32 @@
 en((data) =%3E
+%0A       
  data.sort((
@@ -1072,18 +1072,29 @@
 et rn1 =
+%0A           
  
-(
 a.$conte
@@ -1113,36 +1113,36 @@
 r.cas.length %3E 0
-) ?%0A
+%0A   
             Numb
@@ -1128,32 +1128,33 @@
 0%0A              
+?
  Number(a.$conte
@@ -1190,35 +1190,34 @@
 place(/-/g, ''))
- :
 %0A
+ 
             Numb
@@ -1203,32 +1203,35 @@
 '))%0A            
+  :
  Number.MAX_SAFE
@@ -1259,18 +1259,29 @@
 et rn2 =
+%0A           
  
-(
 b.$conte
@@ -1312,12 +1312,10 @@
  %3E 0
-) ?
 %0A
+ 
     
@@ -1317,24 +1317,27 @@
             
+  ?
  Number(b.$c
@@ -1385,18 +1385,16 @@
 /g, ''))
- :
 %0A       
@@ -1397,16 +1397,20 @@
         
+   :
  Number.
@@ -1564,16 +1564,20 @@
 nst mf =
+%0A   
  chemexp
@@ -1658,16 +1658,20 @@
 st cas =
+%0A   
  chemexp
@@ -1682,16 +1682,20 @@
 ow.rn &&
+%0A   
  chemexp
@@ -2391,17 +2391,16 @@
   %7D;%0A%7D%0A%0A
-%0A
 function

use mastersearch.chemexper.com rather than www.chemexper.com