"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getDefaultFont = getDefaultFont;
exports.getFontMapping = exports.getDefaultFontParams = void 0;
exports.getUsedCustomFontsInEditor = getUsedCustomFontsInEditor;
var _first = _interopRequireDefault(require("lodash/first"));
var _each = _interopRequireDefault(require("lodash/each"));
var _find = _interopRequireDefault(require("lodash/find"));
var _get = _interopRequireDefault(require("lodash/get"));
var _createSelector = _interopRequireDefault(require("../../../Utils/Function/createSelector"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var escapeQuotes = function escapeQuotes(str) {
  return str.replace(/(['"])/g, '\\$1');
};
function getDefaultFont(state) {
  var fonts = state.fonts;
  return (0, _find["default"])(fonts, {
    isDefault: true
  }) || (0, _first["default"])(fonts);
}
var getDefaultFontParams = exports.getDefaultFontParams = function getDefaultFontParams(state) {
  var stringify = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  var defaultFont = getDefaultFont(state);
  var fontParams = {
    'font-family': "\"".concat(escapeQuotes(defaultFont.family), "\""),
    'font-weight': 'normal',
    'font-style': 'normal'
  };
  return stringify ? Object.keys(fontParams).map(function (prop) {
    return "".concat(prop, ": ").concat(fontParams[prop], ";");
  }).join(' ') : fontParams;
};
var getFontMapping = exports.getFontMapping = (0, _createSelector["default"])(function (_ref) {
  var fonts = _ref.fonts;
  return fonts;
}, function (fonts) {
  var typeToUrlProp = {
    regular: 'urlRegular',
    bold: 'urlBold',
    italic: 'urlItalic',
    boldItalic: 'urlBoldItalic'
  };
  var typeKeys = Object.keys(typeToUrlProp);
  var fontMapping = {};
  (0, _each["default"])(fonts, function (font) {
    fontMapping[font.family] = {};
    (0, _each["default"])(typeKeys, function (typeKey) {
      var fontUrl = font[typeToUrlProp[typeKey]];
      if (fontUrl) {
        fontMapping[font.family][typeKey] = fontUrl;
      }
    });
  });
  return fontMapping;
});
function getUsedCustomFontsInEditor(state) {
  var usedCustomFonts = [];
  var designdata = (0, _get["default"])(state, 'configurator.configuration.designdata') || {};
  Object.values(designdata).forEach(function (designAreaData) {
    var objects = (0, _get["default"])(designAreaData, 'canvasData.objects') || [];
    objects.filter(function (_ref2) {
      var type = _ref2.type;
      return type === 'Text';
    }).forEach(function (_ref3) {
      var fontItem = _ref3.fontItem;
      if (fontItem.isCustomFont) {
        var customFontObj = _objectSpread(_objectSpread({}, getDefaultFont(state)), fontItem);
        !(0, _find["default"])(usedCustomFonts, customFontObj) && usedCustomFonts.push(customFontObj);
      }
    });
  });
  return usedCustomFonts;
}