Liferay.Loader.define("fragment-web@3.0.46/js/fragment-editor/FragmentEditor", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/tabs', 'frontend-js-react-web', 'frontend-js-web', 'frontend-js-react-web$react', './CodeMirrorEditor', './FragmentPreview'], 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 _icon = _interopRequireDefault(require("@frontend-taglib-clay$clayui/icon"));

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

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

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

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

    var _CodeMirrorEditor = _interopRequireDefault(require("./CodeMirrorEditor"));

    var _FragmentPreview = _interopRequireDefault(require("./FragmentPreview"));

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

    var CHANGES_STATUS = {
      saved: Liferay.Language.get('changes-saved'),
      saving: Liferay.Language.get('saving-changes'),
      unsaved: Liferay.Language.get('unsaved-changes')
    };

    var FragmentEditor = function FragmentEditor(_ref) {
      var namespace = _ref.context.namespace,
          _ref$props = _ref.props,
          _ref$props$allowedSta = _ref$props.allowedStatus,
          allowedStatus = _ref$props$allowedSta === void 0 ? {
        approved: false,
        draft: false
      } : _ref$props$allowedSta,
          autocompleteTags = _ref$props.autocompleteTags,
          cacheable = _ref$props.cacheable,
          dataAttributes = _ref$props.dataAttributes,
          fragmentCollectionId = _ref$props.fragmentCollectionId,
          fragmentEntryId = _ref$props.fragmentEntryId,
          htmlEditorCustomEntities = _ref$props.htmlEditorCustomEntities,
          initialCSS = _ref$props.initialCSS,
          initialConfiguration = _ref$props.initialConfiguration,
          initialHTML = _ref$props.initialHTML,
          initialJS = _ref$props.initialJS,
          name = _ref$props.name,
          propagationEnabled = _ref$props.propagationEnabled,
          readOnly = _ref$props.readOnly,
          urls = _ref$props.urls;

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

      var _useState3 = (0, _react.useState)(cacheable),
          _useState4 = _slicedToArray(_useState3, 2),
          isCacheable = _useState4[0],
          setIsCacheable = _useState4[1];

      var _useState5 = (0, _react.useState)(null),
          _useState6 = _slicedToArray(_useState5, 2),
          changesStatus = _useState6[0],
          setChangesStatus = _useState6[1];

      var _useState7 = (0, _react.useState)(initialConfiguration),
          _useState8 = _slicedToArray(_useState7, 2),
          configuration = _useState8[0],
          setConfiguration = _useState8[1];

      var _useState9 = (0, _react.useState)(initialCSS),
          _useState10 = _slicedToArray(_useState9, 2),
          css = _useState10[0],
          setCss = _useState10[1];

      var _useState11 = (0, _react.useState)(initialHTML),
          _useState12 = _slicedToArray(_useState11, 2),
          html = _useState12[0],
          setHtml = _useState12[1];

      var _useState13 = (0, _react.useState)(initialJS),
          _useState14 = _slicedToArray(_useState13, 2),
          js = _useState14[0],
          setJs = _useState14[1];

      var isMounted = (0, _frontendJsReactWeb.useIsMounted)();
      var contentHasChanged = (0, _react.useCallback)(function () {
        return initialConfiguration !== configuration || initialCSS !== css || initialHTML !== html || initialJS !== js || cacheable !== isCacheable;
      }, [cacheable, configuration, css, html, initialCSS, initialConfiguration, initialHTML, initialJS, isCacheable, js]);

      /* eslint-disable-next-line react-hooks/exhaustive-deps */

      var saveDraft = (0, _react.useCallback)((0, _frontendJsWeb.debounce)(function () {
        setChangesStatus(CHANGES_STATUS.saving);
        var formData = new FormData();
        formData.append("".concat(namespace, "cacheable"), isCacheable);
        formData.append("".concat(namespace, "configurationContent"), configuration);
        formData.append("".concat(namespace, "cssContent"), css);
        formData.append("".concat(namespace, "htmlContent"), html);
        formData.append("".concat(namespace, "fragmentCollectionId"), fragmentCollectionId);
        formData.append("".concat(namespace, "fragmentEntryId"), fragmentEntryId);
        formData.append("".concat(namespace, "jsContent"), js);
        formData.append("".concat(namespace, "name"), name);
        formData.append("".concat(namespace, "status"), allowedStatus.draft);
        (0, _frontendJsWeb.fetch)(urls.edit, {
          body: formData,
          method: 'POST'
        }).then(function (response) {
          return response.json();
        }).then(function (response) {
          if (response.error) {
            throw response.error;
          }

          return response;
        }).then(function () {
          setChangesStatus(CHANGES_STATUS.saved);
        }).catch(function (error) {
          if (isMounted()) {
            setChangesStatus(CHANGES_STATUS.unsaved);
          }

          var message = typeof error === 'string' ? error : Liferay.Language.get('error');
          (0, _frontendJsWeb.openToast)({
            message: message,
            type: 'danger'
          });
        });
      }, 500), [configuration, css, html, isCacheable, js]);
      var previousSaveDraft = (0, _frontendJsReactWeb.usePrevious)(saveDraft);
      (0, _react.useEffect)(function () {
        if (previousSaveDraft && previousSaveDraft !== saveDraft) {
          (0, _frontendJsWeb.cancelDebounce)(previousSaveDraft);
        }
      }, [previousSaveDraft, saveDraft]);
      (0, _react.useEffect)(function () {
        if (contentHasChanged()) {
          setChangesStatus(CHANGES_STATUS.unsaved);
          saveDraft();
        }
      }, [contentHasChanged, saveDraft]);
      return (/*#__PURE__*/_react.default.createElement("div", {
          className: "fragment-editor-container"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "fragment-editor__toolbar nav-bar-container"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "navbar navbar-default pb-2 pt-2"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "container"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "navbar navbar-collapse-absolute navbar-expand-md navbar-underline navigation-bar navigation-bar-light"
        }, /*#__PURE__*/_react.default.createElement(_tabs.default, {
          modern: true
        }, /*#__PURE__*/_react.default.createElement(_tabs.default.Item, {
          active: activeTabKeyValue === 0,
          innerProps: {
            'aria-controls': 'code',
            'aria-expanded': 'true'
          },
          onClick: function onClick() {
            return setActiveTabKeyValue(0);
          }
        }, Liferay.Language.get('code')), /*#__PURE__*/_react.default.createElement(_tabs.default.Item, {
          active: activeTabKeyValue === 1,
          innerProps: {
            'aria-controls': 'configuration',
            'aria-expanded': 'false'
          },
          onClick: function onClick() {
            return setActiveTabKeyValue(1);
          }
        }, Liferay.Language.get('configuration')))), /*#__PURE__*/_react.default.createElement("div", {
          className: "btn-group btn-group-nowrap float-right mt-1"
        }, readOnly ? /*#__PURE__*/_react.default.createElement("span", {
          className: "pr-3 pt-1 text-info"
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: "exclamation-circle"
        }), /*#__PURE__*/_react.default.createElement("span", null, Liferay.Language.get('read-only-view'))) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, propagationEnabled && /*#__PURE__*/_react.default.createElement("span", {
          className: "lfr-portal-tooltip pr-3 pt-1 text-info",
          "data-title": Liferay.Language.get('automatic-propagation-enabled-help')
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: "exclamation-circle"
        }), /*#__PURE__*/_react.default.createElement("span", null, Liferay.Language.get('automatic-propagation-enabled'))), /*#__PURE__*/_react.default.createElement("div", {
          className: "btn-group-item ml-2 mr-4"
        }, /*#__PURE__*/_react.default.createElement("span", {
          className: "my-0 navbar-text p-0"
        }, changesStatus)), /*#__PURE__*/_react.default.createElement("div", {
          className: "btn-group-item custom-checkbox custom-control mb-1 mr-4 mt-1"
        }, /*#__PURE__*/_react.default.createElement("label", {
          className: "lfr-portal-tooltip",
          "data-title": Liferay.Language.get('cacheable-fragment-help')
        }, /*#__PURE__*/_react.default.createElement("input", {
          checked: isCacheable,
          className: "custom-control-input toggle-switch-check",
          name: "cacheable",
          onChange: function onChange(event) {
            return setIsCacheable(event.currentTarget.checked);
          },
          type: "checkbox",
          value: "true"
        }), /*#__PURE__*/_react.default.createElement("span", {
          className: "custom-control-label"
        }, /*#__PURE__*/_react.default.createElement("span", {
          className: "custom-control-label-text"
        }, Liferay.Language.get('cacheable'))))), /*#__PURE__*/_react.default.createElement("div", {
          className: "btn-group-item"
        }, /*#__PURE__*/_react.default.createElement("button", {
          className: "btn btn-primary btn-sm",
          disabled: changesStatus === CHANGES_STATUS.saving,
          onClick: function publish() {
            var formData = new FormData();
            formData.append("".concat(namespace, "fragmentEntryId"), fragmentEntryId);
            (0, _frontendJsWeb.fetch)(urls.publish, {
              body: formData,
              method: 'POST'
            }).then(function (response) {
              return response.json();
            }).then(function (response) {
              if (response.error) {
                throw response.error;
              }

              return response;
            }).then(function (response) {
              var redirectURL = response.redirect || urls.redirect;
              Liferay.Util.navigate(redirectURL);
            }).catch(function (error) {
              if (isMounted()) {
                setChangesStatus(CHANGES_STATUS.unsaved);
              }

              var message = typeof error === 'string' ? error : Liferay.Language.get('error');
              (0, _frontendJsWeb.openToast)({
                message: message,
                type: 'danger'
              });
            });
          },
          type: "button"
        }, /*#__PURE__*/_react.default.createElement("span", {
          className: "lfr-btn-label"
        }, Liferay.Language.get('publish'))))))))), /*#__PURE__*/_react.default.createElement(_tabs.default.Content, {
          activeIndex: activeTabKeyValue,
          fade: true
        }, /*#__PURE__*/_react.default.createElement(_tabs.default.TabPane, {
          "aria-labelledby": "code"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "fragment-editor"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "html source-editor"
        }, /*#__PURE__*/_react.default.createElement(_CodeMirrorEditor.default, {
          content: initialHTML,
          customDataAttributes: dataAttributes,
          customEntities: htmlEditorCustomEntities,
          customTags: autocompleteTags,
          mode: "html",
          onChange: setHtml,
          readOnly: readOnly
        })), /*#__PURE__*/_react.default.createElement("div", {
          className: "css source-editor"
        }, /*#__PURE__*/_react.default.createElement(_CodeMirrorEditor.default, {
          content: initialCSS,
          mode: "css",
          onChange: setCss,
          readOnly: readOnly
        })), /*#__PURE__*/_react.default.createElement("div", {
          className: "javascript source-editor"
        }, /*#__PURE__*/_react.default.createElement(_CodeMirrorEditor.default, {
          codeFooterText: "}",
          codeHeaderHelpText: Liferay.Util.sub(Liferay.Language.get('parameter-x-provides-access-to-the-current-fragment-node-use-it-to-manipulate-fragment-components'), ['fragmentElement']),
          codeHeaderText: "function(fragmentElement, configuration) {",
          content: initialJS,
          mode: "javascript",
          onChange: setJs,
          readOnly: readOnly
        })), /*#__PURE__*/_react.default.createElement(_FragmentPreview.default, {
          configuration: configuration,
          css: css,
          html: html,
          js: js,
          namespace: namespace,
          urls: urls
        }))), /*#__PURE__*/_react.default.createElement(_tabs.default.TabPane, {
          "aria-labelledby": "configuration"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "fragment-editor"
        }, /*#__PURE__*/_react.default.createElement(_CodeMirrorEditor.default, {
          content: initialConfiguration,
          mode: "json",
          onChange: setConfiguration,
          readOnly: readOnly
        })))))
      );
    };

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