"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getDrugName = void 0;

var _moneyClip = require("money-clip");

const {
  set,
  get,
  getAll
} = (0, _moneyClip.getConfiguredCache)({
  maxAge: Infinity,
  version: 1,
  name: 'drug-name-cache'
});
const drugData = {};
getAll().then(dbValues => {
  Object.assign(drugData, dbValues);
});

const fetchDrug = id => fetch("https://rxnav.nlm.nih.gov/REST/rxcui/".concat(id, ".json")).then(res => res.json()).then(drugData => drugData.idGroup.name);

const getDrugName = id => {
  let found = drugData[id];

  if (found) {
    return Promise.resolve(found);
  }

  return get(id).then(res => {
    if (res) {
      drugData[id] = res;
      return res;
    }

    return fetchDrug(id).then(res => {
      if (res) {
        drugData[id] = res;
        set(id, res);
        return res;
      }
    });
  });
};

exports.getDrugName = getDrugName;