var cached = {};
export default function canUseCSSFont(fontName) {
  var _doc$fonts, _doc$fonts2, _doc$fonts3, _doc$fonts4;
  var doc = document;
  if (cached.hasOwnProperty(fontName)) {
    return Promise.resolve(cached[fontName]);
  }
  if (!((_doc$fonts = doc.fonts) !== null && _doc$fonts !== void 0 && _doc$fonts.check) || !((_doc$fonts2 = doc.fonts) !== null && _doc$fonts2 !== void 0 && _doc$fonts2.ready) || !((_doc$fonts3 = doc.fonts) !== null && _doc$fonts3 !== void 0 && _doc$fonts3.status) || !((_doc$fonts4 = doc.fonts) !== null && _doc$fonts4 !== void 0 && _doc$fonts4.values)) {
    // Feature is not supported, install the CSS anyway
    // https://developer.mozilla.org/en-US/docs/Web/API/FontFaceSet/check#Browser_compatibility
    // TODO: Polyfill this.
    console.log('FontFaceSet is not supported');
    return Promise.resolve(false);
  }
  return new Promise(function (resolve) {
    // https://stackoverflow.com/questions/5680013/how-to-be-notified-once-a-web-font-has-loaded
    // All fonts in use by visible text have loaded.
    var _check = function check() {
      if (doc.fonts.status !== 'loaded') {
        setTimeout(_check, 350);
        return;
      }
      // Do not use `doc.fonts.check()` because it may return falsey result.
      var fontFaces = Array.from(doc.fonts.values());
      var matched = fontFaces.find(function (ff) {
        return ff.family === fontName;
      });
      var result = !!matched;
      cached[fontName] = result;
      resolve(result);
    };
    doc.fonts.ready.then(_check);
  });
}