Liferay.Loader.define("portal-workflow-metrics-web@2.0.46/js/components/instance-list-page/modal/reassign/bulk/BulkReassignModal.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/modal', 'frontend-js-react-web$react', '../../../../../shared/components/modal-with-steps/ModalWithSteps.es', '../../../../../shared/components/toaster/hooks/useToaster.es', '../../../../../shared/hooks/usePatch.es', '../../../../../shared/util/lang.es', '../../../InstanceListPageProvider.es', '../../ModalProvider.es', '../../shared/select-tasks-step/SelectTasksStep.es', '../../shared/select-tasks-step/hooks/useFetchTasks.es', './select-assignees-step/SelectAssigneesStep.es'], 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 _modal = require("@frontend-taglib-clay$clayui/modal");

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

    var _ModalWithSteps = _interopRequireDefault(require("../../../../../shared/components/modal-with-steps/ModalWithSteps.es"));

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

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

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

    var _InstanceListPageProvider = require("../../../InstanceListPageProvider.es");

    var _ModalProvider = require("../../ModalProvider.es");

    var _SelectTasksStep = _interopRequireDefault(require("../../shared/select-tasks-step/SelectTasksStep.es"));

    var _useFetchTasks2 = require("../../shared/select-tasks-step/hooks/useFetchTasks.es");

    var _SelectAssigneesStep = _interopRequireDefault(require("./select-assignees-step/SelectAssigneesStep.es"));

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

    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 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 _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 BulkReassignModal = function BulkReassignModal() {
      var _useContext = (0, _react.useContext)(_ModalProvider.ModalContext),
          _useContext$bulkReass = _useContext.bulkReassign,
          reassignedTasks = _useContext$bulkReass.reassignedTasks,
          reassigning = _useContext$bulkReass.reassigning,
          closeModal = _useContext.closeModal,
          selectTasks = _useContext.selectTasks,
          setBulkReassign = _useContext.setBulkReassign,
          setSelectTasks = _useContext.setSelectTasks,
          visibleModal = _useContext.visibleModal;

      var _useFetchTasks = (0, _useFetchTasks2.useFetchTasks)(),
          clearFilters = _useFetchTasks.clearFilters,
          fetchTasks = _useFetchTasks.fetchTasks;

      var _useContext2 = (0, _react.useContext)(_InstanceListPageProvider.InstanceListContext),
          setSelectAll = _useContext2.setSelectAll,
          setSelectedItems = _useContext2.setSelectedItems;

      var _useState = (0, _react.useState)('selectTasks'),
          _useState2 = _slicedToArray(_useState, 2),
          currentStep = _useState2[0],
          setCurrentStep = _useState2[1];

      var _useState3 = (0, _react.useState)(null),
          _useState4 = _slicedToArray(_useState3, 2),
          errorToast = _useState4[0],
          setErrorToast = _useState4[1];

      var _useState5 = (0, _react.useState)(false),
          _useState6 = _slicedToArray(_useState5, 2),
          fetching = _useState6[0],
          setFetching = _useState6[1];

      var selectAll = selectTasks.selectAll,
          tasks = selectTasks.tasks;
      var toaster = (0, _useToaster.useToaster)();
      var clearContext = (0, _react.useCallback)(function () {
        setBulkReassign({
          reassignedTasks: [],
          reassigning: false,
          selectedAssignee: null,
          useSameAssignee: false
        });
      }, [setBulkReassign]);

      var onCloseModal = function onCloseModal(refetch) {
        clearContext();
        clearFilters();
        closeModal(refetch);
        setSelectTasks({
          selectAll: false,
          tasks: []
        });
        setCurrentStep('selectTasks');
        setErrorToast(false);
      };

      var _useModal = (0, _modal.useModal)({
        onClose: onCloseModal
      }),
          observer = _useModal.observer,
          onClose = _useModal.onClose;

      var _usePatch = (0, _usePatch2.usePatch)({
        admin: true,
        body: reassignedTasks,
        url: 'workflow-tasks/assign-to-user'
      }),
          patchData = _usePatch.patchData;

      var handleNext = (0, _react.useCallback)(function () {
        if (selectAll) {
          setFetching(true);
          fetchTasks().then(function (_ref) {
            var items = _ref.items;
            setFetching(false);
            setSelectTasks({
              selectAll: selectAll,
              tasks: items
            });
            setCurrentStep('selectAssignees');
          }).catch(function () {
            setErrorToast(Liferay.Language.get('your-request-has-failed'));
            setFetching(false);
          });
        } else {
          setCurrentStep('selectAssignees');
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [setBulkReassign, fetchTasks, selectAll]);
      var handlePrevious = (0, _react.useCallback)(function () {
        clearContext();
        setCurrentStep('selectTasks');
        setErrorToast(false);
      }, [clearContext]);
      var handleReassign = (0, _react.useCallback)(function () {
        setBulkReassign(function (bulkReassign) {
          return _objectSpread(_objectSpread({}, bulkReassign), {}, {
            reassigning: true
          });
        });
        setErrorToast(false);
        patchData().then(function () {
          toaster.success(reassignedTasks.length > 1 ? (0, _lang.sub)(Liferay.Language.get('x-tasks-have-been-reassigned'), [reassignedTasks.length]) : Liferay.Language.get('this-task-has-been-reassigned'));
          onCloseModal(true);
          setSelectedItems([]);
          setSelectAll(false);
        }).catch(function () {
          var error = "".concat(Liferay.Language.get('your-request-has-failed'), " ").concat(Liferay.Language.get('select-reassign-to-retry'));
          setBulkReassign(function (bulkReassign) {
            return _objectSpread(_objectSpread({}, bulkReassign), {}, {
              reassigning: false
            });
          });
          setErrorToast(error);
        }); // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [patchData, setBulkReassign]);
      var getStep = (0, _react.useCallback)(function (step) {
        var steps = {
          selectAssignees: {
            cancelBtn: {
              disabled: reassigning,
              handle: onClose
            },
            component: _SelectAssigneesStep.default,
            nextBtn: {
              disabled: reassigning || reassignedTasks.length !== tasks.length,
              handle: handleReassign,
              text: Liferay.Language.get('reassign')
            },
            order: 2,
            previousBtn: {
              disabled: reassigning,
              handle: handlePrevious
            },
            props: {
              setErrorToast: setErrorToast
            },
            subtitle: Liferay.Language.get('select-assignees'),
            title: Liferay.Language.get('select-new-assignees')
          },
          selectTasks: {
            cancelBtn: {
              disabled: fetching,
              handle: onClose
            },
            component: _SelectTasksStep.default,
            nextBtn: {
              disabled: tasks.length === 0 || fetching,
              handle: handleNext,
              text: Liferay.Language.get('next')
            },
            order: 1,
            previousBtn: false,
            props: {
              setErrorToast: setErrorToast
            },
            subtitle: Liferay.Language.get('select-tasks'),
            title: Liferay.Language.get('select-tasks-to-reassign')
          }
        };
        return steps[step];
      }, [reassigning, onClose, reassignedTasks, tasks, handleReassign, handlePrevious, fetching, handleNext]);
      var step = (0, _react.useMemo)(function () {
        return getStep(currentStep);
      }, [currentStep, getStep]);
      return (/*#__PURE__*/_react.default.createElement(_ModalWithSteps.default, {
          error: errorToast,
          observer: observer,
          step: step,
          visible: visibleModal === 'bulkReassign'
        })
      );
    };

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