Liferay.Loader.define("portal-workflow-metrics-web@2.0.46/js/components/instance-list-page/modal/shared/select-tasks-step/hooks/useFetchTasks.es", ['module', 'exports', 'require', 'frontend-js-react-web$react', '../../../../../../shared/hooks/useFilter.es', '../../../../../../shared/hooks/usePost.es', '../../../../../AppContext.es', '../../../../InstanceListPageProvider.es', '../../../ModalProvider.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.useFetchTasks = void 0;

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

    var _useFilter3 = require("../../../../../../shared/hooks/useFilter.es");

    var _usePost2 = require("../../../../../../shared/hooks/usePost.es");

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

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

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

    /**
     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
     *
     * The contents of this file are subject to the terms of the Liferay Enterprise
     * Subscription License ("License"). You may not use this file except in
     * compliance with the License. You can obtain a copy of the License by
     * contacting Liferay, Inc. See the License for the specific language governing
     * permissions and limitations under the License, including but not limited to
     * distribution rights of the Software.
     */
    var useFetchTasks = function useFetchTasks() {
      var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          _ref$page = _ref.page,
          page = _ref$page === void 0 ? 1 : _ref$page,
          _ref$pageSize = _ref.pageSize,
          pageSize = _ref$pageSize === void 0 ? 10000 : _ref$pageSize,
          withoutUnassigned = _ref.withoutUnassigned;

      var _useFilter = (0, _useFilter3.useFilter)({}),
          _useFilter$filterValu = _useFilter.filterValues,
          _useFilter$filterValu2 = _useFilter$filterValu.assigneeIds,
          userIds = _useFilter$filterValu2 === void 0 ? [] : _useFilter$filterValu2,
          slaStatuses = _useFilter$filterValu.slaStatuses,
          instanceTasks = _useFilter$filterValu.taskNames;

      var _useFilter2 = (0, _useFilter3.useFilter)({
        withoutRouteParams: true
      }),
          dispatch = _useFilter2.dispatch,
          _useFilter2$filterVal = _useFilter2.filterValues,
          _useFilter2$filterVal2 = _useFilter2$filterVal.bulkAssigneeIds,
          bulkAssigneeIds = _useFilter2$filterVal2 === void 0 ? [] : _useFilter2$filterVal2,
          _useFilter2$filterVal3 = _useFilter2$filterVal.bulkTaskNames,
          bulkTaskNames = _useFilter2$filterVal3 === void 0 ? [] : _useFilter2$filterVal3;

      var _useContext = (0, _react.useContext)(_ModalProvider.ModalContext),
          processId = _useContext.processId;

      var _useContext2 = (0, _react.useContext)(_AppContext.AppContext),
          userId = _useContext2.userId;

      var _useContext3 = (0, _react.useContext)(_InstanceListPageProvider.InstanceListContext),
          selectAll = _useContext3.selectAll,
          _useContext3$selected = _useContext3.selectedItems,
          selectedItems = _useContext3$selected === void 0 ? [] : _useContext3$selected;

      var assignees = bulkAssigneeIds.length ? bulkAssigneeIds : userIds;
      var availableUsers = withoutUnassigned ? [userId] : [userId, '-1'];
      var assigneeIds = assignees.filter(function (id) {
        return availableUsers.includes(id);
      });
      var instanceIds = !selectAll ? selectedItems.map(function (_ref2) {
        var id = _ref2.id;
        return id;
      }) : undefined;
      var taskNames = bulkTaskNames.length ? bulkTaskNames : instanceTasks;

      var _usePost = (0, _usePost2.usePost)({
        body: {
          assigneeIds: withoutUnassigned ? availableUsers : assigneeIds,
          instanceIds: instanceIds,
          processId: processId,
          slaStatuses: slaStatuses,
          taskNames: taskNames
        },
        params: {
          page: page,
          pageSize: pageSize,
          sort: 'instanceId:asc'
        },
        url: '/tasks'
      }),
          data = _usePost.data,
          fetchTasks = _usePost.postData;

      var noResultsFetch = function noResultsFetch() {
        data = {
          items: [],
          page: page,
          pageSize: pageSize,
          totalCount: 0
        };
        return Promise.resolve(data);
      };

      if (withoutUnassigned && assignees.length && !assignees.includes(userId)) {
        fetchTasks = noResultsFetch;
      }

      return {
        clearFilters: function clearFilters() {
          dispatch({
            bulkAssigneeIds: [],
            bulkTaskNames: []
          });
        },
        data: data,
        fetchTasks: fetchTasks,
        instanceIds: instanceIds
      };
    };

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