// SPDX-License-Identifier: LGPL-3.0-or-later
// https://stackoverflow.com/a/1026087/9567349
function capitalizeFirstLetter(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}

/**
 * Convert SMILES to chemical formula.
 * Only support SMILES generated by ReacNetGenerator.
 * @param {string} smi smiles
 * @returns {string} formula
 */
function getFormula(smi) {
  // consider [Ca] [C] [c]
  const reg = /\[([a-zA-Z][a-z]?)\]/g;
  const atom_types =
      [...smi.matchAll(reg) ].map(m => capitalizeFirstLetter(m[1]));
  atom_types.sort();
  const conut = {};
  atom_types.forEach(atom => {
    if (atom in conut) {
      conut[atom] += 1;
    } else {
      conut[atom] = 1;
    }
  });
  let formula = '';
  for (const atom in conut) {
    formula += atom;
    if (conut[atom] > 1) {
      formula += conut[atom];
    }
  }
  return formula;
}

module.exports = {getFormula};
