Liferay.Loader.define("document-library-opener-onedrive-web@2.0.44/js/DocumentLibraryOpener.es", ['module', 'exports', 'require', 'frontend-js-web'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;

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

    function _classCallCheck(instance, Constructor) {
      if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
      }
    }

    function _defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    function _createClass(Constructor, protoProps, staticProps) {
      if (protoProps) _defineProperties(Constructor.prototype, protoProps);if (staticProps) _defineProperties(Constructor, staticProps);return Constructor;
    }

    var TIME_POLLING = 500;
    var TIME_SHOW_MSG = 2000;
    var DEFAULT_ERROR = Liferay.Language.get('an-unexpected-error-occurred');

    var DocumentLibraryOpener = /*#__PURE__*/function () {
      function DocumentLibraryOpener(_ref) {
        var namespace = _ref.namespace;

        _classCallCheck(this, DocumentLibraryOpener);

        this._namespace = namespace;
        this._dialogLoadingId = "".concat(namespace, "OneDriveLoadingDialog");
        this._refreshAfterNavigate = false;
      }

      _createClass(DocumentLibraryOpener, [{
        key: "_hideLoading",
        value: function _hideLoading() {
          Liferay.Util.getWindow(this._dialogLoadingId).hide();
        }
      }, {
        key: "_openExternal",
        value: function _openExternal(_ref2) {
          var externalURL = _ref2.externalURL;
          window.open(externalURL);

          this._hideLoading();

          if (this._refreshAfterNavigate) {
            this._refreshAfterNavigate = false;
            Liferay.Portlet.refresh("#p_p_id".concat(this._namespace));
          }
        }
      }, {
        key: "_polling",
        value: function _polling(_ref3) {
          var _this = this;

          var statusURL = _ref3.statusURL;
          return (0, _frontendJsWeb.fetch)(statusURL).then(function (response) {
            if (!response.ok) {
              throw DEFAULT_ERROR;
            }

            return response.json();
          }).then(function (response) {
            if (response.complete) {
              _this._openExternal({
                externalURL: response.office365EditURL
              });
            } else if (response.error) {
              throw DEFAULT_ERROR;
            } else {
              return new Promise(function (resolve) {
                setTimeout(function () {
                  _this._polling({
                    statusURL: statusURL
                  }).then(resolve);
                }, TIME_POLLING);
              });
            }
          }).catch(function (error) {
            _this._showError(error);
          });
        }
      }, {
        key: "_showError",
        value: function _showError(message) {
          (0, _frontendJsWeb.openToast)({
            message: message,
            type: 'danger'
          });
        }
      }, {
        key: "_showLoading",
        value: function _showLoading(_ref4) {
          var _this2 = this;

          var dialogMessage = _ref4.dialogMessage;
          return new Promise(function (resolve) {
            Liferay.Util.openWindow({
              dialog: {
                bodyContent: "<p>".concat(dialogMessage, "</p><div aria-hidden=\"true\" class=\"loading-animation\"></div>"),
                cssClass: 'office-365-redirect-modal',
                height: 172,
                modal: true,
                resizable: false,
                title: '',
                width: 320
              },
              id: _this2._dialogLoadingId
            }, function () {
              setTimeout(resolve, TIME_SHOW_MSG);
            });
          });
        }
      }, {
        key: "createWithName",
        value: function createWithName(_ref5) {
          var _this3 = this;

          var dialogTitle = _ref5.dialogTitle,
              formSubmitURL = _ref5.formSubmitURL;
          (0, _frontendJsWeb.openSimpleInputModal)({
            alert: {
              message: Liferay.Language.get('the-document-has-been-checked-out-please-on-finish-editing-check-in-the-document-to-save-changes-into-the-document-library'),
              style: 'info',
              title: Liferay.Language.get('info')
            },
            dialogTitle: dialogTitle,
            formSubmitURL: formSubmitURL,
            mainFieldLabel: Liferay.Language.get('title'),
            mainFieldName: 'title',
            namespace: this._namespace,
            onFormSuccess: function onFormSuccess(serverResponseContent) {
              if (serverResponseContent.oneDriveBackgroundTaskStatusURL) {
                _this3.open({
                  dialogMessage: serverResponseContent.dialogMessage,
                  refresh: true,
                  statusURL: serverResponseContent.oneDriveBackgroundTaskStatusURL
                });
              }
            },
            spritemap: Liferay.ThemeDisplay.getPathThemeImages() + '/clay/icons.svg'
          });
        }
      }, {
        key: "edit",
        value: function edit(_ref6) {
          var _this4 = this;

          var formSubmitURL = _ref6.formSubmitURL;
          this._refreshAfterNavigate = true;

          var loadingPromise = this._showLoading({
            dialogMessage: Liferay.Language.get('you-are-being-redirected-to-an-external-editor-to-edit-this-document')
          });

          var fetchPromise = (0, _frontendJsWeb.fetch)(formSubmitURL).then(function (response) {
            if (!response.ok) {
              throw DEFAULT_ERROR;
            }

            return response.json();
          }).then(function (response) {
            if (response.redirectURL) {
              (0, _frontendJsWeb.navigate)(response.redirectURL);
            } else if (response.oneDriveBackgroundTaskStatusURL) {
              return _this4._polling({
                statusURL: response.oneDriveBackgroundTaskStatusURL
              });
            } else if (response.error) {
              throw response.error.errorMessage || DEFAULT_ERROR;
            }
          }).catch(function (error) {
            _this4._showError(error);
          });
          return Promise.all([loadingPromise, fetchPromise]);
        }
      }, {
        key: "open",
        value: function open(_ref7) {
          var _ref7$dialogMessage = _ref7.dialogMessage,
              dialogMessage = _ref7$dialogMessage === void 0 ? Liferay.Language.get('you-are-being-redirected-to-an-external-editor-to-edit-this-document') : _ref7$dialogMessage,
              statusURL = _ref7.statusURL,
              _ref7$refresh = _ref7.refresh,
              refresh = _ref7$refresh === void 0 ? false : _ref7$refresh;
          this._refreshAfterNavigate = refresh;

          var loadingPromise = this._showLoading({
            dialogMessage: dialogMessage
          });

          var pollingPromise = this._polling({
            statusURL: statusURL
          });

          return Promise.all([loadingPromise, pollingPromise]);
        }
      }]);

      return DocumentLibraryOpener;
    }();

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