"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _get = _interopRequireDefault(require("lodash/get"));
var _debounce = _interopRequireDefault(require("lodash/debounce"));
var _ComponentContainer = require("../../../../Framework/ComponentContainer");
var _i18n = require("../../../../Framework/i18n");
var _createSelector = _interopRequireDefault(require("../../../../Utils/Function/createSelector"));
var _ServiceLocator = require("../../../ServiceLocator");
var _DesignerSelectionToolbox = _interopRequireDefault(require("../Components/DesignerSelectionToolbox/DesignerSelectionToolbox"));
var _Selectors = require("../../../Reducers/DesignArea/Selectors");
var _Actions = require("../../../Reducers/Fonts/Actions");
var _Selectors2 = require("../../../Reducers/Fonts/Selectors");
var _Actions2 = require("../../../Reducers/UI/Actions");
var _FontFaceRepository = require("../Utils/FontFaceRepository");
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 getFonts = (0, _createSelector["default"])(function (state) {
  return state.fonts;
}, function (fonts) {
  return fonts.map(function (font) {
    return _objectSpread(_objectSpread({}, font), {}, {
      value: font.family
    });
  });
});
var loadFonts = function loadFonts(dispatch) {
  return function (textItem) {
    if (!textItem || !textItem.content) {
      return;
    }
    var toggleLoader = function toggleLoader(show) {
      return dispatch((0, _Actions2.setVisible)('globalLoader', show));
    };
    var showLoaderTimeout = window.setTimeout(function () {
      return toggleLoader(true);
    }, 300);
    _FontFaceRepository.FontFaceRepository.collectFromHTML(textItem.content)["catch"](function (e) {
      // eslint-disable-next-line no-console
      console.error('Error while loading fonts.', e);
    })["finally"](function () {
      textItem.build({
        triggerChange: true
      });
      clearTimeout(showLoaderTimeout);
      toggleLoader(false);
    });
  };
};
var mapStateToProps = function mapStateToProps(state) {
  return {
    Component: _DesignerSelectionToolbox["default"],
    fonts: getFonts(state),
    colorDisabled: (0, _Selectors.isColoringDisabled)(state),
    isActiveBulkName: (0, _get["default"])((0, _Selectors.getSelectedDesignProductionMethod)(state), 'allowBulkNames'),
    isPortraitMode: state.device.screen.orientation === 'portrait',
    defaultFont: (0, _Selectors2.getDefaultFont)(state),
    initialColor: (0, _Selectors.getVectorizedImageColor)(state)
  };
};
var mapDispatchToProps = function mapDispatchToProps(dispatch) {
  return {
    updateVisualization: function updateVisualization(canvas) {
      canvas.selectedObject.x += 1;
      canvas.selectedObject.x -= 1;
    },
    openImageEditDialog: function openImageEditDialog(identifier, canvas) {
      var placeholderImageUpdate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
      return _ServiceLocator.Services.designer.openImageEditDialog(identifier, canvas, {
        placeholderImageUpdate: placeholderImageUpdate
      });
    },
    getTextContent: function getTextContent(_ref) {
      var isBulkName = _ref.isBulkName;
      return _ServiceLocator.Services.designer.createColorizedTextContent((0, _i18n.t)(isBulkName ? 'textControlbox.bulknames' : 'addVisualElement.initialText'), 1, {
        withPlaceholder: !isBulkName
      });
    },
    addCustomFont: function addCustomFont(customFont) {
      dispatch((0, _Actions.addCustomFont)(customFont));
    },
    loadFonts: (0, _debounce["default"])(loadFonts(dispatch), 300)
  };
};
var _default = exports["default"] = (0, _ComponentContainer.containerConnect)(mapStateToProps, mapDispatchToProps);