"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.receiveFonts = exports.fetchFonts = exports.addFontFamilyToDOM = exports.addCustomFonts = exports.addCustomFont = exports.RECEIVE_FONTS = exports.ADD_FONTS = exports.ADD_FONT = void 0;
var _getUrl = require("../../Utils/getUrl");
var _Actions = require("../Actions");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
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 _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 RECEIVE_FONTS = exports.RECEIVE_FONTS = 'RECEIVE_FONTS';
var ADD_FONT = exports.ADD_FONT = 'ADD_FONT';
var ADD_FONTS = exports.ADD_FONTS = 'ADD_FONTS';
var addFontFamilyToDOM = exports.addFontFamilyToDOM = function addFontFamilyToDOM(name, url, bold, italic) {
  var fontFaceProperties = ['font-family: "' + name + '";', 'src: url("' + url + '");'];
  if (bold) {
    fontFaceProperties.push('font-weight: bold;');
  }
  if (italic) {
    fontFaceProperties.push('font-style: italic;');
  }
  var style = document.createElement('style');
  style.appendChild(document.createTextNode('@font-face { ' + fontFaceProperties.join(' ') + ' }'));
  document.head.appendChild(style);
};
var receiveFonts = exports.receiveFonts = function receiveFonts(data) {
  return function (dispatch) {
    var fonts = [];
    var _iterator = _createForOfIteratorHelper(data),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var customFont = _step.value;
        var font = {
          name: customFont.name,
          family: customFont.name,
          regular: false,
          bold: false,
          italic: false,
          boldItalic: false,
          isDefault: customFont.isDefault
        };
        var fontFields = [{
          field: 'fileNameRegular',
          property: 'regular',
          bold: false,
          italic: false
        }, {
          field: 'fileNameBold',
          property: 'bold',
          bold: true,
          italic: false
        }, {
          field: 'fileNameItalic',
          property: 'italic',
          bold: false,
          italic: true
        }, {
          field: 'fileNameBoldItalic',
          property: 'boldItalic',
          bold: true,
          italic: true
        }];
        for (var _i = 0, _fontFields = fontFields; _i < _fontFields.length; _i++) {
          var fontField = _fontFields[_i];
          var fontPath = customFont[fontField.field];
          if (fontPath) {
            var fontUrl = (0, _getUrl.getUrl)(fontPath);
            var urlFieldName = 'url' + fontField.property.charAt(0).toUpperCase() + fontField.property.slice(1);
            font[fontField.property] = true;
            font[urlFieldName] = fontUrl;
            addFontFamilyToDOM(font.family, fontUrl, fontField.bold, fontField.italic);
          }
        }
        fonts.push(font);
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
    dispatch({
      type: RECEIVE_FONTS,
      data: fonts
    });
  };
};
var fetchFonts = exports.fetchFonts = function fetchFonts() {
  return (0, _Actions.fetchDataAsync)('designer/fonts', receiveFonts);
};
var addCustomFont = exports.addCustomFont = function addCustomFont(customFont) {
  return {
    type: ADD_FONT,
    data: customFont
  };
};
var addCustomFonts = exports.addCustomFonts = function addCustomFonts(customFonts) {
  return {
    type: ADD_FONTS,
    data: customFonts
  };
};