"use strict";

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); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ConnectedConfiguratorScreen = exports.Configurator = void 0;
var _react = _interopRequireWildcard(require("react"));
var React = _react;
var _ComponentContainer = require("../../../Framework/ComponentContainer");
var _Selectors = require("../../Reducers/Configurator/Selectors");
var _Selectors2 = require("../../Reducers/DesignArea/Selectors");
var uiActions = _interopRequireWildcard(require("../../Reducers/UI/Actions"));
var _Error = _interopRequireDefault(require("../../Shared/Containers/Error"));
var _ProductionErrorPage = _interopRequireDefault(require("../../Error/ProductionErrorPage"));
var _AlertMessages = _interopRequireDefault(require("../../Shared/Containers/AlertMessages"));
var _LandscapeInformation = _interopRequireDefault(require("./Components/LandscapeInformation"));
var _Confirm = _interopRequireDefault(require("./Containers/Confirm"));
var _SaveConfiguration = _interopRequireDefault(require("../../Shared/Containers/SaveConfiguration"));
var _SendEmail = _interopRequireDefault(require("../../Shared/Containers/SendEmail"));
var _PdfDownload = _interopRequireDefault(require("../../Shared/Containers/PdfDownload"));
var _LoadConfiguration = _interopRequireDefault(require("../../Shared/Containers/LoadConfiguration"));
var _FallbackVisualization = _interopRequireDefault(require("./Containers/FallbackVisualization"));
var _LicenseNotice = _interopRequireDefault(require("../../Shared/Containers/LicenseNotice"));
var _DefaultVisualizationProvider = require("../../Modules/Visualization/DefaultVisualizationProvider");
var _ThemeProvider = _interopRequireDefault(require("./ThemeProvider"));
var _Visualization = _interopRequireDefault(require("./Containers/Visualization"));
var _FullScreenView = _interopRequireDefault(require("./Containers/FullScreenView"));
var _GlobalLoader = _interopRequireDefault(require("../../Shared/Containers/GlobalLoader"));
var _AmountPrice = _interopRequireDefault(require("./Containers/AmountPrice"));
var _NoVisualization = _interopRequireDefault(require("./Components/NoVisualization/NoVisualization"));
var _Selectors3 = require("../../Reducers/ConfigurationMode/Selectors");
require("./Components/Styles.scss");
var _ScreenPropTypes = _interopRequireDefault(require("./ScreenPropTypes"));
var _configuration = require("../../configuration");
var _DesignerScreen = require("../../Modules/Designer/DesignerScreen");
var _CreatorScreen = require("../../Modules/Creator/CreatorScreen");
var _useMediaQuery = _interopRequireDefault(require("@material-ui/core/useMediaQuery"));
var _ServiceLocator = require("../../ServiceLocator");
var _reactRouter = require("react-router");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
/**
 * Main Screen of the configurator
 */
var Configurator = exports.Configurator = function Configurator(props) {
  var imageOnly = (0, _useMediaQuery["default"])('(min-aspect-ratio: 13/9) and (min-height: 280px) and (max-width: 950px)');
  (0, _react.useEffect)(function () {
    if (imageOnly) {
      _ServiceLocator.Services.visualization.enableDesignAreaInteraction(false);
    } else {
      _ServiceLocator.Services.visualization.initDesignAreaInteraction();
    }
  }, [imageOnly]);
  var _useParams = (0, _reactRouter.useParams)(),
    identifier = _useParams.identifier,
    code = _useParams.code;
  (0, _react.useEffect)(function () {
    if (identifier && identifier !== props.loadIdentifier || code && code !== props.loadCode) {
      _ServiceLocator.Services.configurator.initialize();
    }
  }, [identifier, code]);
  var fatalError = props.fatalError;
  if (fatalError) {
    return /*#__PURE__*/React.createElement(_ProductionErrorPage["default"], null);
  }
  var children = props.children;
  if (children instanceof Function) {
    return children(props);
  }
  var selectedConfigurationMode = props.selectedConfigurationMode,
    CreatorScreen = props.CreatorScreen,
    DesignerScreen = props.DesignerScreen;
  var ScreenComponent = {
    creator: CreatorScreen,
    designer: DesignerScreen
  }[selectedConfigurationMode] || function () {
    return null;
  };
  var ThemeProvider = props.ThemeProvider,
    VisualizationProvider = props.VisualizationProvider,
    customElements = props.customElements,
    renderLandscapeInformation = props.renderLandscapeInformation;
  return /*#__PURE__*/React.createElement(ThemeProvider, null, /*#__PURE__*/React.createElement(VisualizationProvider, null, renderLandscapeInformation(), /*#__PURE__*/React.createElement(ScreenComponent, _extends({
    imageOnly: imageOnly
  }, props)), customElements.root));
};
// eslint-disable-next-line react/prop-types
var renderVisualization = function renderVisualization(_ref) {
  var fullScreenViewProps = _ref.fullScreenViewProps;
  return /*#__PURE__*/React.createElement(_FullScreenView["default"], fullScreenViewProps, /*#__PURE__*/React.createElement(React.Suspense, {
    fallback: /*#__PURE__*/React.createElement("div", null, "Loading Visualization Component...")
  }, /*#__PURE__*/React.createElement(_Visualization["default"], {
    BrokenComponent: _NoVisualization["default"]
  })));
};
Configurator.defaultProps = {
  // Render props
  renderLandscapeInformation: function renderLandscapeInformation() {
    return /*#__PURE__*/React.createElement(_LandscapeInformation["default"], null);
  },
  renderError: function renderError() {
    return /*#__PURE__*/React.createElement(_Error["default"], null);
  },
  renderAlertMessages: function renderAlertMessages() {
    return /*#__PURE__*/React.createElement(_AlertMessages["default"], null);
  },
  renderConfirm: function renderConfirm() {
    return /*#__PURE__*/React.createElement(_Confirm["default"], null);
  },
  renderSaveConfiguration: function renderSaveConfiguration() {
    return /*#__PURE__*/React.createElement(_SaveConfiguration["default"], null);
  },
  renderLoadConfiguration: function renderLoadConfiguration() {
    return /*#__PURE__*/React.createElement(_LoadConfiguration["default"], null);
  },
  renderSendEmail: function renderSendEmail() {
    return /*#__PURE__*/React.createElement(_SendEmail["default"], null);
  },
  renderPdfDownload: function renderPdfDownload() {
    return /*#__PURE__*/React.createElement(_PdfDownload["default"], null);
  },
  renderLoader: function renderLoader() {
    return /*#__PURE__*/React.createElement(_GlobalLoader["default"], null);
  },
  renderAmountPrice: function renderAmountPrice(props) {
    return /*#__PURE__*/React.createElement(_AmountPrice["default"], props);
  },
  renderLicenseNotice: function renderLicenseNotice(props) {
    return /*#__PURE__*/React.createElement(_LicenseNotice["default"], props);
  },
  renderVisualization: renderVisualization,
  // Components
  DesignerScreen: _DesignerScreen.DesignerScreen,
  CreatorScreen: _CreatorScreen.CreatorScreen,
  VisualizationProvider: _DefaultVisualizationProvider.DefaultVisualizationProvider,
  FallbackVisualization: _FallbackVisualization["default"],
  ThemeProvider: _ThemeProvider["default"],
  customElements: {},
  activeDesignArea: false,
  isPortraitMode: false
};
var mapStateToProps = function mapStateToProps(state) {
  var activeDesignArea = (0, _Selectors2.isAnyDesignAreaSelected)(state);
  var fallbackMode = 'fallback' === (0, _Selectors.getCurrentVisualizationSettings)(state);
  var showDesigner = activeDesignArea && state.ui.visibilityMap.designer;
  var showProductViewButton = activeDesignArea;
  var fatalError = (0, _Selectors2.isThereAFatalError)(state);
  var is3DvisualMode = (0, _Selectors.visualizationModeCheck)(state).is3D();
  var isPortraitMode = state.device.screen.orientation === 'portrait';
  var selectedConfigurationMode = (0, _Selectors3.getSelectedConfigurationMode)(state);
  var showConfigurationModeSwitch = (0, _Selectors3.getConfigurationModeOptions)(state).length > 0;
  var dock = (0, _configuration.getConf)('dock');
  var vertical = dock ? dock === 'left' : state.device.screen.orientation === 'landscape';
  var showAddToCartButton = state.ui.visibilityMap.addToCartButton;
  var showConfigurationVariantSelection = state.ui.visibilityMap.configurationvariantSelection;
  var visualizationOverlayMode = (0, _Selectors.visualizationModeCheck)(state).is2DOverlay();
  var _state$configurator = state.configurator,
    loadIdentifier = _state$configurator.loadIdentifier,
    loadCode = _state$configurator.loadCode;
  var configuration = {
    showAddToCartButton: (0, _configuration.getConf)('showAddToCartButton'),
    editCustomTitle: (0, _configuration.getConf)('editCustomTitleBeforeSave'),
    showInitialOptionClassification: (0, _configuration.getConf)('showInitialOptionclassification'),
    showStartHere: (0, _configuration.getConf)('showStartHere')
  };
  return {
    Component: Configurator,
    selectedConfigurationMode: selectedConfigurationMode,
    showConfigurationModeSwitch: showConfigurationModeSwitch,
    activeDesignArea: activeDesignArea,
    fallbackMode: fallbackMode,
    showDesigner: showDesigner,
    showProductViewButton: showProductViewButton,
    is3DvisualMode: is3DvisualMode,
    isPortraitMode: isPortraitMode,
    fatalError: fatalError,
    vertical: vertical,
    showAddToCartButton: showAddToCartButton,
    showConfigurationVariantSelection: showConfigurationVariantSelection,
    configuration: configuration,
    visualizationOverlayMode: visualizationOverlayMode,
    loadIdentifier: loadIdentifier,
    loadCode: loadCode
  };
};
var mapDispatchToProps = function mapDispatchToProps(dispatch) {
  return {
    dispatchAction: function dispatchAction(action) {
      return dispatch(action);
    },
    setVisible: function setVisible(name, show) {
      return dispatch(uiActions.setVisible(name, show));
    }
  };
};
var ConnectedConfiguratorScreen = exports.ConnectedConfiguratorScreen = (0, _ComponentContainer.containerConnect)(mapStateToProps, mapDispatchToProps);