Liferay.Loader.define("fragment-web@3.0.73/js/fragment-editor/FragmentPreview", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', 'frontend-js-react-web$classnames', 'frontend-js-react-web', 'frontend-js-web', 'frontend-js-react-web$react'], 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 = void 0;

    var _button = require("@frontend-taglib-clay$clayui/button");

    var _classnames = _interopRequireDefault(require("frontend-js-react-web$classnames"));

    var _frontendJsReactWeb = require("frontend-js-react-web");

    var _frontendJsWeb = require("frontend-js-web");

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

    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;
    }

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

    function _slicedToArray(arr, i) {
      return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
    }

    function _nonIterableRest() {
      throw new TypeError("Invalid attempt to destructure non-iterable instance");
    }

    function _iterableToArrayLimit(arr, i) {
      if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
        return;
      }var _arr = [];var _n = true;var _d = false;var _e = undefined;try {
        for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
          _arr.push(_s.value);if (i && _arr.length === i) break;
        }
      } catch (err) {
        _d = true;_e = err;
      } finally {
        try {
          if (!_n && _i["return"] != null) _i["return"]();
        } finally {
          if (_d) throw _e;
        }
      }return _arr;
    }

    function _arrayWithHoles(arr) {
      if (Array.isArray(arr)) return arr;
    }

    /**
     * Defined ratios for preview sizing.
     */
    var SIZE_RATIOS = {
      desktop: {
        height: 9,
        width: 16
      },
      'full-size': {
        height: '',
        width: ''
      },
      'mobile-portrait': {
        height: 16,
        width: 10
      },
      'tablet-portrait': {
        height: 3,
        width: 4
      }
    };
    /**
     * Available preview sizes in order.
     */

    var PREVIEW_SIZES = ['desktop', 'tablet-portrait', 'mobile-portrait', 'full-size'];

    var stopEventPropagation = function stopEventPropagation(event) {
      event.preventDefault();
      event.stopPropagation();
    };

    var FragmentPreview = function FragmentPreview(_ref) {
      var configuration = _ref.configuration,
          css = _ref.css,
          html = _ref.html,
          js = _ref.js,
          namespace = _ref.namespace,
          _ref$urls = _ref.urls,
          urls = _ref$urls === void 0 ? {} : _ref$urls;
      var iframeRef = (0, _react.useRef)();
      var ref = (0, _react.useRef)();

      var _useState = (0, _react.useState)('full-size'),
          _useState2 = _slicedToArray(_useState, 2),
          currentPreviewSize = _useState2[0],
          setCurrentPreviewSize = _useState2[1];

      var _useState3 = (0, _react.useState)(false),
          _useState4 = _slicedToArray(_useState3, 2),
          loading = _useState4[0],
          setLoading = _useState4[1];

      var _useState5 = (0, _react.useState)({}),
          _useState6 = _slicedToArray(_useState5, 2),
          previewStyles = _useState6[0],
          setPreviewStyles = _useState6[1];

      var isMounted = (0, _frontendJsReactWeb.useIsMounted)();
      /* eslint-disable-next-line react-hooks/exhaustive-deps */

      var updatePreview = (0, _react.useCallback)((0, _frontendJsWeb.debounce)(function () {
        if (!loading && isMounted()) {
          setLoading(true);
          var formData = new FormData();
          formData.append("".concat(namespace, "configuration"), configuration);
          formData.append("".concat(namespace, "css"), css);
          formData.append("".concat(namespace, "html"), html);
          formData.append("".concat(namespace, "js"), js);
          (0, _frontendJsWeb.fetch)(urls.render, {
            body: formData,
            method: 'POST'
          }).then(function (response) {
            return response.text();
          }).then(function (response) {
            if (isMounted()) {
              setLoading(false);
            }

            iframeRef.current.contentWindow.postMessage(JSON.stringify({
              data: response
            }), '*');
          });
        }
      }, 500), [configuration, css, html, js, iframeRef]);
      /* eslint-disable-next-line react-hooks/exhaustive-deps */

      var updatePreviewStyles = (0, _react.useCallback)((0, _frontendJsWeb.debounce)(function () {
        var ratio = SIZE_RATIOS[currentPreviewSize];

        if (ratio && ref.current) {
          var wrapperRect = ref.current.getBoundingClientRect();
          var scale = Math.min(wrapperRect.width * 0.9 / ratio.width, wrapperRect.height * 0.8 / ratio.height);
          setPreviewStyles({
            height: ratio.height ? "".concat(ratio.height * scale, "px") : '',
            width: ratio.width ? "".concat(ratio.width * scale, "px") : ''
          });
        }
      }, 100), [currentPreviewSize]);
      var previousUpdatePreview = (0, _frontendJsReactWeb.usePrevious)(updatePreview);
      var previousUpdatePreviewStyles = (0, _frontendJsReactWeb.usePrevious)(updatePreviewStyles);
      (0, _react.useEffect)(function () {
        if (previousUpdatePreview && previousUpdatePreview !== updatePreview) {
          (0, _frontendJsWeb.cancelDebounce)(previousUpdatePreview);
          updatePreview();
        }
      }, [previousUpdatePreview, updatePreview]);
      (0, _react.useEffect)(function () {
        if (previousUpdatePreviewStyles && previousUpdatePreviewStyles !== updatePreviewStyles) {
          (0, _frontendJsWeb.cancelDebounce)(previousUpdatePreviewStyles);
          updatePreviewStyles();
        }
      }, [previousUpdatePreviewStyles, updatePreviewStyles]);
      (0, _frontendJsReactWeb.useEventListener)('click', stopEventPropagation, true, iframeRef.current && iframeRef.current.contentWindow);
      (0, _frontendJsReactWeb.useEventListener)('resize', updatePreviewStyles, true, window);
      return (/*#__PURE__*/_react.default.createElement("div", {
          className: "fragment-preview",
          ref: ref
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "btn-group fragment-preview__toolbar"
        }, PREVIEW_SIZES.map(function (previewSize) {
          return (/*#__PURE__*/_react.default.createElement(_button.ClayButtonWithIcon, {
              borderless: true,
              className: (0, _classnames.default)({
                active: currentPreviewSize === previewSize
              }),
              displayType: "secondary",
              key: previewSize,
              onClick: function onClick() {
                return setCurrentPreviewSize(previewSize);
              },
              small: true,
              symbol: previewSize
            })
          );
        })), /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)('fragment-preview__wrapper', {
            'fragment-preview__wrapper--loading': loading,
            'fragment-preview__wrapper--resized': currentPreviewSize
          }),
          style: previewStyles
        }, loading && /*#__PURE__*/_react.default.createElement("div", {
          className: "fragment-preview__loading-indicator"
        }, /*#__PURE__*/_react.default.createElement("span", {
          "aria-hidden": "true",
          className: "loading-animation"
        })), /*#__PURE__*/_react.default.createElement("iframe", {
          className: "fragment-preview__content",
          onLoad: updatePreview,
          ref: iframeRef,
          src: urls.preview
        })))
      );
    };

    var _default = FragmentPreview;
    exports.default = _default;
    //# sourceMappingURL=FragmentPreview.js.map
  }
});
//# sourceMappingURL=FragmentPreview.js.map