Liferay.Loader.define("journal-web@4.0.125/js/article/AssetDisplayPagePreview", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/drop-down', '@frontend-taglib-clay$clayui/form', '@frontend-taglib-clay$clayui/icon', '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 = AssetDisplayPagePreview;

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

    var _dropDown = _interopRequireDefault(require("@frontend-taglib-clay$clayui/drop-down"));

    var _form = _interopRequireWildcard(require("@frontend-taglib-clay$clayui/form"));

    var _icon = _interopRequireDefault(require("@frontend-taglib-clay$clayui/icon"));

    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 _toConsumableArray(arr) {
      return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
    }

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

    function _iterableToArray(iter) {
      if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
    }

    function _arrayWithoutHoles(arr) {
      if (Array.isArray(arr)) {
        for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
          arr2[i] = arr[i];
        }return arr2;
      }
    }

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

    function AssetDisplayPagePreview(_ref) {
      var newArticle = _ref.newArticle,
          namespace = _ref.portletNamespace,
          previewURL = _ref.previewURL,
          saveAsDraftURL = _ref.saveAsDraftURL,
          selectAssetDisplayPageEventName = _ref.selectAssetDisplayPageEventName,
          selectAssetDisplayPageURL = _ref.selectAssetDisplayPageURL,
          selectSiteEventName = _ref.selectSiteEventName,
          siteItemSelectorURL = _ref.siteItemSelectorURL,
          sites = _ref.sites,
          sitesCount = _ref.sitesCount;

      var _useState = (0, _react.useState)(),
          _useState2 = _slicedToArray(_useState, 2),
          selectedSite = _useState2[0],
          setSelectedSite = _useState2[1];

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

      var _useState5 = (0, _react.useState)(),
          _useState6 = _slicedToArray(_useState5, 2),
          assetDisplayPageSelected = _useState6[0],
          setAssetDisplayPageSelected = _useState6[1];

      var siteInputId = "".concat(namespace, "siteInput");
      var items = (0, _react.useMemo)(function () {
        return [{
          groupId: 0,
          name: "- ".concat(Liferay.Language.get('not-selected'), " -")
        }].concat(_toConsumableArray(sites)).map(function (site) {
          return {
            label: site.name,
            onClick: function onClick() {
              setActive(false);
              setSelectedSite({
                groupId: site.groupId,
                name: site.name
              });
            }
          };
        });
      }, [sites]);
      return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("label", {
          className: "control-label",
          htmlFor: siteInputId
        }, Liferay.Language.get('site')), /*#__PURE__*/_react.default.createElement(_dropDown.default, {
          active: active,
          "aria-labelledby": siteInputId,
          onActiveChange: setActive,
          trigger: /*#__PURE__*/_react.default.createElement(_button.default, {
            className: "bg-light form-control-select text-left w-100",
            displayType: "secondary",
            type: "button"
          }, /*#__PURE__*/_react.default.createElement("span", null, (selectedSite === null || selectedSite === void 0 ? void 0 : selectedSite.name) || "- ".concat(Liferay.Language.get('not-selected'), " -")))
        }, items.map(function (item) {
          return (/*#__PURE__*/_react.default.createElement(_dropDown.default.Item, {
              key: item.label,
              onClick: item.onClick
            }, item.label)
          );
        }), sitesCount > sites.length && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_dropDown.default.Caption, null, Liferay.Util.sub(Liferay.Language.get('showing-x-of-x-items'), sites.length, sitesCount)), /*#__PURE__*/_react.default.createElement("div", {
          className: "d-flex w-100"
        }, /*#__PURE__*/_react.default.createElement(_button.default, {
          className: "flex-grow-1 mx-3",
          displayType: "secondary",
          onClick: function onClick() {
            (0, _frontendJsWeb.openSelectionModal)({
              containerProps: {
                className: 'cadmin'
              },
              onSelect: function onSelect(selectedItem) {
                setSelectedSite({
                  groupId: selectedItem.groupid,
                  name: selectedItem.groupdescriptivename
                });
              },
              selectEventName: selectSiteEventName,
              title: Liferay.Language.get('select-site'),
              url: siteItemSelectorURL
            });
          },
          small: true,
          type: "button"
        }, Liferay.Language.get('more'))))), /*#__PURE__*/_react.default.createElement(AssetDisplayPageSelector, {
          assetDisplayPageSelected: assetDisplayPageSelected,
          namespace: namespace,
          newArticle: newArticle,
          previewURL: previewURL,
          saveAsDraftURL: saveAsDraftURL,
          selectAssetDisplayPageEventName: selectAssetDisplayPageEventName,
          selectAssetDisplayPageURL: selectAssetDisplayPageURL,
          selectedSite: selectedSite,
          setAssetDisplayPageSelected: setAssetDisplayPageSelected
        }))
      );
    }

    function AssetDisplayPageSelector(_ref2) {
      var _assetDisplayPageSele;

      var assetDisplayPageSelected = _ref2.assetDisplayPageSelected,
          namespace = _ref2.namespace,
          newArticle = _ref2.newArticle,
          previewURL = _ref2.previewURL,
          saveAsDraftURL = _ref2.saveAsDraftURL,
          selectAssetDisplayPageEventName = _ref2.selectAssetDisplayPageEventName,
          selectAssetDisplayPageURL = _ref2.selectAssetDisplayPageURL,
          selectedSite = _ref2.selectedSite,
          setAssetDisplayPageSelected = _ref2.setAssetDisplayPageSelected;
      var assetDisplayPageId = "".concat(namespace, "assetDisplayPageId");

      var openAssetDisplayPageSelector = function openAssetDisplayPageSelector() {
        var url = new URL(selectAssetDisplayPageURL);
        url.searchParams.set("".concat((0, _frontendJsWeb.getPortletNamespace)(Liferay.PortletKeys.ITEM_SELECTOR), "groupId"), selectedSite.groupId);
        (0, _frontendJsWeb.openSelectionModal)({
          containerProps: {
            className: 'cadmin'
          },
          onSelect: function onSelect(selectedItem) {
            setAssetDisplayPageSelected({
              name: selectedItem.name,
              plid: selectedItem.plid
            });
          },
          selectEventName: selectAssetDisplayPageEventName,
          title: Liferay.Util.sub(Liferay.Language.get('select-x'), Liferay.Language.get('display-page')),
          url: url
        });
      };

      return (/*#__PURE__*/_react.default.createElement("div", {
          className: "mb-3 mt-2"
        }, /*#__PURE__*/_react.default.createElement(_form.default.Group, {
          className: "mb-2"
        }, /*#__PURE__*/_react.default.createElement("label", {
          className: "sr-only",
          htmlFor: assetDisplayPageId
        }, Liferay.Language.get('display-page')), /*#__PURE__*/_react.default.createElement(_form.ClayInput.Group, {
          small: true
        }, /*#__PURE__*/_react.default.createElement(_form.ClayInput.GroupItem, null, /*#__PURE__*/_react.default.createElement(_form.ClayInput, {
          disabled: !(selectedSite === null || selectedSite === void 0 ? void 0 : selectedSite.groupId),
          onClick: function onClick() {
            return openAssetDisplayPageSelector();
          },
          placeholder: Liferay.Util.sub(Liferay.Language.get('select-x'), Liferay.Language.get('display-page')),
          readOnly: true,
          sizing: "sm",
          value: (_assetDisplayPageSele = assetDisplayPageSelected === null || assetDisplayPageSelected === void 0 ? void 0 : assetDisplayPageSelected.name) !== null && _assetDisplayPageSele !== void 0 ? _assetDisplayPageSele : ''
        })), /*#__PURE__*/_react.default.createElement(_form.ClayInput.GroupItem, {
          shrink: true
        }, /*#__PURE__*/_react.default.createElement(_button.default, {
          disabled: !(selectedSite === null || selectedSite === void 0 ? void 0 : selectedSite.groupId),
          displayType: "secondary",
          monospaced: true,
          onClick: function onClick() {
            return openAssetDisplayPageSelector();
          },
          small: true,
          title: Liferay.Util.sub(assetDisplayPageSelected ? Liferay.Language.get('change-x') : Liferay.Language.get('select-x'), Liferay.Language.get('display-page'))
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          className: "mt-0",
          symbol: assetDisplayPageSelected ? 'change' : 'plus'
        }))))), /*#__PURE__*/_react.default.createElement(_button.default, {
          disabled: !assetDisplayPageSelected,
          displayType: "secondary",
          onClick: function onClick() {
            updateJournalInput({
              name: 'formDate',
              namespace: namespace,
              value: Date.now().toString()
            });
            Liferay.fire('updateDDMFormInputValue');
            var form = document.getElementById("".concat(namespace, "fm1"));
            var formData = new FormData(form);
            var articleId = document.getElementById("".concat(namespace, "articleId"));
            formData.append("".concat(namespace, "cmd"), newArticle && !articleId.value ? 'add' : 'update');
            return Liferay.Util.fetch(saveAsDraftURL, {
              body: formData,
              method: form.method
            }).then(function (response) {
              return response.json();
            }).then(function (_ref3) {
              var articleId = _ref3.articleId,
                  classPK = _ref3.classPK,
                  error = _ref3.error,
                  friendlyUrlMap = _ref3.friendlyUrlMap,
                  version = _ref3.version;

              if (error) {
                (0, _frontendJsWeb.openToast)({
                  message: Liferay.Language.get('web-content-could-not-be-previewed-due-to-an-unexpected-error-while-generating-the-draft'),
                  title: Liferay.Language.get('error'),
                  type: 'danger'
                });
              } else {
                updateJournalInput({
                  name: 'formDate',
                  namespace: namespace,
                  value: Date.now().toString()
                });
                updateJournalInput({
                  name: 'articleId',
                  namespace: namespace,
                  value: articleId
                });
                updateJournalInput({
                  name: 'version',
                  namespace: namespace,
                  value: version
                });
                Object.entries(friendlyUrlMap).forEach(function (_ref4) {
                  var _ref5 = _slicedToArray(_ref4, 2),
                      languageId = _ref5[0],
                      value = _ref5[1];

                  updateJournalInput({
                    name: "friendlyURL_".concat(languageId),
                    namespace: namespace,
                    value: value
                  });
                });
                (0, _frontendJsWeb.openModal)({
                  title: Liferay.Language.get('preview'),
                  url: (0, _frontendJsWeb.createPortletURL)(previewURL, {
                    classPK: classPK,
                    selPlid: assetDisplayPageSelected === null || assetDisplayPageSelected === void 0 ? void 0 : assetDisplayPageSelected.plid,
                    version: version
                  }).toString()
                });
              }
            }).catch(function () {
              (0, _frontendJsWeb.openToast)({
                message: Liferay.Language.get('web-content-could-not-be-previewed-due-to-an-unexpected-error-while-generating-the-draft'),
                title: Liferay.Language.get('error'),
                type: 'danger'
              });
            });
          },
          title: Liferay.Language.get('a-draft-will-be-saved-before-displaying-the-preview')
        }, Liferay.Language.get('preview')))
      );
    }

    function updateJournalInput(_ref6) {
      var name = _ref6.name,
          namespace = _ref6.namespace,
          value = _ref6.value;
      var input = document.getElementById("".concat(namespace).concat(name));

      if (input) {
        input.value = value;
      }
    }
    //# sourceMappingURL=AssetDisplayPagePreview.js.map
  }
});
//# sourceMappingURL=AssetDisplayPagePreview.js.map