"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.removeQuotes = exports.FontFaceRepository = void 0;
var _fontfaceobserver = _interopRequireDefault(require("fontfaceobserver"));
var _DOM = require("redhotmagma-graphics-editor/Utils/DOM");
var _configuration = require("../../../configuration");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var removeQuotes = exports.removeQuotes = function removeQuotes(str) {
  return str && str.replace(/"/g, '');
};
var FontFaceRepository = exports.FontFaceRepository = function (_ref) {
  var loadTimeout = _ref.loadTimeout;
  var repository = new Map();
  var request = function request(name) {
    var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    var key = [name].concat(_toConsumableArray(Object.values(params))).join('-');
    if (!repository.has(key)) {
      var fontFaceObserver = new _fontfaceobserver["default"](name, params);
      repository.set(key, fontFaceObserver.load(null, loadTimeout));
    }
    return repository.get(key);
  };
  var collectFromElement = function collectFromElement(element) {
    var fonts = [];
    (0, _DOM.traverse)(element, function (node) {
      if (node.style && node.style.fontFamily) {
        var _node$style = node.style,
          fontFamily = _node$style.fontFamily,
          fontWeight = _node$style.fontWeight,
          fontStyle = _node$style.fontStyle;
        fonts.push(request(removeQuotes(fontFamily), {
          weight: fontWeight || 'normal',
          style: fontStyle || 'normal'
        }));
      }
    });
    return Promise.all(fonts);
  };
  var collectFromHTML = function collectFromHTML(htmlStr) {
    var element = document.createElement('div');
    element.innerHTML = htmlStr;
    return collectFromElement(element);
  };
  return {
    request: request,
    collectFromElement: collectFromElement,
    collectFromHTML: collectFromHTML
  };
}({
  loadTimeout: (0, _configuration.getConf)('network.timeout', 60000)
});