Liferay.Loader.define("style-book-web@1.0.34/js/style-book-editor/LayoutPreview", ['module', 'exports', 'require', 'frontend-js-react-web$react', './PreviewInfoBar', './StyleBookContext'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

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

    var _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    var _PreviewInfoBar = _interopRequireDefault(require("./PreviewInfoBar"));

    var _StyleBookContext = require("./StyleBookContext");

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

    function _getRequireWildcardCache() {
      if (typeof WeakMap !== "function") return null;var cache = new WeakMap();_getRequireWildcardCache = function _getRequireWildcardCache() {
        return cache;
      };return cache;
    }

    function _interopRequireWildcard(obj) {
      if (obj && obj.__esModule) {
        return obj;
      }if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
        return { default: obj };
      }var cache = _getRequireWildcardCache();if (cache && cache.has(obj)) {
        return cache.get(obj);
      }var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;if (desc && (desc.get || desc.set)) {
            Object.defineProperty(newObj, key, desc);
          } else {
            newObj[key] = obj[key];
          }
        }
      }newObj.default = obj;if (cache) {
        cache.set(obj, newObj);
      }return newObj;
    }

    /**
     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
     *
     * This library is free software; you can redistribute it and/or modify it under
     * the terms of the GNU Lesser General Public License as published by the Free
     * Software Foundation; either version 2.1 of the License, or (at your option)
     * any later version.
     *
     * This library is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
     * details.
     */
    function LayoutPreview() {
      var iframeRef = (0, _react.useRef)();

      var _useContext = (0, _react.useContext)(_StyleBookContext.StyleBookContext),
          _useContext$frontendT = _useContext.frontendTokensValues,
          frontendTokensValues = _useContext$frontendT === void 0 ? {} : _useContext$frontendT,
          previewLayout = _useContext.previewLayout;

      var loadFrontendTokenValues = (0, _react.useCallback)(function () {
        if (iframeRef.current) {
          var root = iframeRef.current.contentDocument.documentElement;

          if (root) {
            Object.values(frontendTokensValues).forEach(function (_ref) {
              var cssVariableMapping = _ref.cssVariableMapping,
                  value = _ref.value;
              root.style.setProperty("--".concat(cssVariableMapping), value);
            });
          }
        }
      }, [frontendTokensValues]);
      (0, _react.useEffect)(function () {
        loadFrontendTokenValues();
      }, [loadFrontendTokenValues, frontendTokensValues]);
      (0, _react.useEffect)(function () {
        if (iframeRef.current) {
          iframeRef.current.style['pointer-events'] = 'none';
        }
      }, [previewLayout]);
      return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
          className: "style-book-editor__page-preview"
        }, (previewLayout === null || previewLayout === void 0 ? void 0 : previewLayout.layoutURL) ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_PreviewInfoBar.default, null), /*#__PURE__*/_react.default.createElement("iframe", {
          className: "style-book-editor__page-preview-frame",
          onLoad: function onLoad() {
            loadOverlay(iframeRef);
            loadFrontendTokenValues();
          },
          ref: iframeRef,
          src: urlWithPreviewParameter(previewLayout === null || previewLayout === void 0 ? void 0 : previewLayout.layoutURL)
        })) : /*#__PURE__*/_react.default.createElement("div", {
          className: "style-book-editor__page-preview-no-page-message"
        }, Liferay.Language.get('you-cannot-preview-the-style-book-because-there-are-no-pages-in-this-site'))))
      );
    }

    function urlWithPreviewParameter(url) {
      var nextURL = new URL(url);
      nextURL.searchParams.set('styleBookEntryPreview', true);
      return nextURL.href;
    }

    function loadOverlay(iframeRef) {
      var style = {
        cursor: 'not-allowed',
        height: '100%',
        left: 0,
        position: 'fixed',
        top: 0,
        width: '100%'
      };

      if (iframeRef.current) {
        var overlay = document.createElement('div');
        Object.keys(style).forEach(function (key) {
          overlay.style[key] = style[key];
        });
        iframeRef.current.removeAttribute('style');
        iframeRef.current.contentDocument.body.append(overlay);
      }
    }
    //# sourceMappingURL=LayoutPreview.js.map
  }
});
//# sourceMappingURL=LayoutPreview.js.map