Liferay.Loader.define("portal-workflow-metrics-web@2.0.46/js/components/settings/indexes-page/hooks/useReindexActions.es", ['module', 'exports', 'require', 'frontend-js-react-web$react', '../../../../shared/components/toaster/hooks/useToaster.es', '../../../../shared/hooks/useFetch.es', '../../../../shared/hooks/useInterval.es', '../../../../shared/hooks/usePatch.es', '../../../../shared/util/lang.es', '../../../AppContext.es', '../IndexesConstants.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.useReindexActions = void 0;

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

    var _useToaster = require("../../../../shared/components/toaster/hooks/useToaster.es");

    var _useFetch2 = require("../../../../shared/hooks/useFetch.es");

    var _useInterval = require("../../../../shared/hooks/useInterval.es");

    var _usePatch2 = require("../../../../shared/hooks/usePatch.es");

    var _lang = require("../../../../shared/util/lang.es");

    var _AppContext = require("../../../AppContext.es");

    var _IndexesConstants = require("../IndexesConstants.es");

    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 ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {
          return Object.getOwnPropertyDescriptor(object, sym).enumerable;
        });keys.push.apply(keys, symbols);
      }return keys;
    }

    function _objectSpread(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          ownKeys(Object(source)).forEach(function (key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
          });
        }
      }return target;
    }

    function _defineProperty(obj, key, value) {
      if (key in obj) {
        Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
      } else {
        obj[key] = value;
      }return obj;
    }

    function _objectWithoutProperties(source, excluded) {
      if (source == null) return {};var target = _objectWithoutPropertiesLoose(source, excluded);var key, i;if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);for (i = 0; i < sourceSymbolKeys.length; i++) {
          key = sourceSymbolKeys[i];if (excluded.indexOf(key) >= 0) continue;if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;target[key] = source[key];
        }
      }return target;
    }

    function _objectWithoutPropertiesLoose(source, excluded) {
      if (source == null) return {};var target = {};var sourceKeys = Object.keys(source);var key, i;for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];if (excluded.indexOf(key) >= 0) continue;target[key] = source[key];
      }return target;
    }

    var useReindexActions = function useReindexActions() {
      var _useContext = (0, _react.useContext)(_AppContext.AppContext),
          reindexStatuses = _useContext.reindexStatuses,
          setReindexStatuses = _useContext.setReindexStatuses;

      var schedule = (0, _useInterval.useInterval)();
      var toaster = (0, _useToaster.useToaster)();

      var _useFetch = (0, _useFetch2.useFetch)({
        url: '/indexes/reindex/status'
      }),
          fetchData = _useFetch.fetchData;

      var _usePatch = (0, _usePatch2.usePatch)({
        url: '/indexes/reindex'
      }),
          patchData = _usePatch.patchData;

      var sendSuccess = function sendSuccess(key) {
        var label = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Liferay.Language.get('workflow-indexes');
        var message = _IndexesConstants.INDEXES_GROUPS_KEYS.includes(key) ? Liferay.Language.get('all-x-have-reindexed-successfully') : Liferay.Language.get('x-has-reindexed-successfully');
        toaster.success((0, _lang.sub)(message, [label]));
      };

      var sendError = function sendError() {
        toaster.danger(Liferay.Language.get('your-request-has-failed'));
      };

      var isReindexing = function isReindexing(key) {
        return reindexStatuses.findIndex(function (item) {
          return key === item.key;
        }) > -1;
      };

      var getStatuses = (0, _react.useCallback)(function () {
        var cancelInterval = schedule(function () {
          fetchData().then(function (_ref) {
            var items = _ref.items;

            if (!items.length) {
              cancelInterval();
            }

            setReindexStatuses(function (prevStatuses) {
              return prevStatuses.filter(function (_ref2) {
                var key = _ref2.key,
                    label = _ref2.label;
                var finished = items.findIndex(function (item) {
                  return item.key === key;
                }) === -1;

                if (finished && /\/settings\/indexes/gi.test(window.location.hash)) {
                  sendSuccess(key, label);
                }

                return !finished;
              }).map(function (_ref3) {
                var key = _ref3.key,
                    status = _objectWithoutProperties(_ref3, ["key"]);

                return _objectSpread(_objectSpread({}, status), items.find(function (item) {
                  return item.key === key;
                }));
              });
            });
          }).catch(function () {
            cancelInterval();
            sendError();
            setReindexStatuses([]);
          });
        }, 1000);
      }, // eslint-disable-next-line react-hooks/exhaustive-deps
      [fetchData, isReindexing]);

      return {
        getReindexStatus: function getReindexStatus(key) {
          return reindexStatuses.find(function (item) {
            return key === item.key;
          }) || {};
        },
        getStatuses: getStatuses,
        handleReindex: function handleReindex(key, label) {
          setReindexStatuses(function (prevStatuses) {
            return [].concat(_toConsumableArray(prevStatuses), [{
              completionPercentage: 0,
              key: key,
              label: label
            }]);
          });
          patchData({
            key: key
          }).then(getStatuses).catch(function () {
            sendError();
            setReindexStatuses(function (prevStatuses) {
              return _toConsumableArray(prevStatuses.filter(function (item) {
                return item.key !== key;
              }));
            });
          });
        },
        isReindexing: isReindexing,
        reindexStatuses: reindexStatuses
      };
    };

    exports.useReindexActions = useReindexActions;
    //# sourceMappingURL=useReindexActions.es.js.map
  }
});
//# sourceMappingURL=useReindexActions.es.js.map