'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _isUrl = require('is-url');

var _isUrl2 = _interopRequireDefault(_isUrl);

var _fontkit = require('@react-pdf/fontkit');

var _fontkit2 = _interopRequireDefault(_fontkit);

var _standard = require('./standard');

var _standard2 = _interopRequireDefault(_standard);

var _font = require('../utils/font');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var fonts = {};
var hyphenationCallback = void 0;

var register = function register(src, _ref) {
  var family = _ref.family,
      otherOptions = _objectWithoutProperties(_ref, ['family']);

  fonts[family] = _extends({
    src: src,
    loaded: false,
    data: null
  }, otherOptions);
};

var registerHyphenationCallback = function registerHyphenationCallback(callback) {
  hyphenationCallback = callback;
};

var getRegisteredFonts = function getRegisteredFonts() {
  return Object.keys(fonts);
};

var getFont = function getFont(family) {
  return fonts[family];
};

var getHyphenationCallback = function getHyphenationCallback() {
  return hyphenationCallback;
};

var load = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(fontFamily, doc) {
    var font, data;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            font = fonts[fontFamily];

            // We cache the font to avoid fetching it many time

            if (!(font && !font.data)) {
              _context.next = 10;
              break;
            }

            if (!(0, _isUrl2.default)(font.src)) {
              _context.next = 9;
              break;
            }

            _context.next = 5;
            return (0, _font.fetchFont)(font.src);

          case 5:
            data = _context.sent;

            font.data = _fontkit2.default.create(data);
            _context.next = 10;
            break;

          case 9:
            font.data = _fontkit2.default.openSync(font.src);

          case 10:

            // If the font wasn't added to the document yet (aka. loaded), we do.
            // This prevents calling `registerFont` many times for the same font.
            // Fonts loaded state will be resetted after document is closed.
            if (font && !font.loaded) {
              font.loaded = true;
              doc.registerFont(fontFamily, font.data);
            }

            if (!(!font && !_standard2.default.includes(fontFamily))) {
              _context.next = 13;
              break;
            }

            throw new Error('Font familiy not registered: ' + fontFamily + '. Please register it calling Font.register() method.');

          case 13:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, undefined);
  }));

  return function load(_x, _x2) {
    return _ref2.apply(this, arguments);
  };
}();

var reset = function reset() {
  for (var font in fonts) {
    if (fonts.hasOwnProperty(font)) {
      fonts[font].loaded = false;
    }
  }
};

var clear = function clear() {
  fonts = {};
};

exports.default = {
  register: register,
  getRegisteredFonts: getRegisteredFonts,
  registerHyphenationCallback: registerHyphenationCallback,
  getHyphenationCallback: getHyphenationCallback,
  getFont: getFont,
  load: load,
  clear: clear,
  reset: reset
};