Liferay.Loader.define("portal-workflow-metrics-web@2.0.46/js/components/instance-list-page/modal/shared/select-tasks-step/SelectTasksStepHeader.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/management-toolbar', 'frontend-js-react-web', 'frontend-js-react-web$react', '../../../../../shared/components/results-bar/ResultsBar.es', '../../../../../shared/components/toolbar-with-selection/ToolbarWithSelection.es', '../../../../../shared/hooks/useFilter.es', '../../../../AppContext.es', '../../../../filter/AssigneeFilter.es', '../../../../filter/ProcessStepFilter.es', '../../ModalProvider.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.Header = void 0;

    var _managementToolbar = _interopRequireDefault(require("@frontend-taglib-clay$clayui/management-toolbar"));

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

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

    var _ResultsBar = _interopRequireDefault(require("../../../../../shared/components/results-bar/ResultsBar.es"));

    var _ToolbarWithSelection = _interopRequireDefault(require("../../../../../shared/components/toolbar-with-selection/ToolbarWithSelection.es"));

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

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

    var _AssigneeFilter = _interopRequireDefault(require("../../../../filter/AssigneeFilter.es"));

    var _ProcessStepFilter = _interopRequireDefault(require("../../../../filter/ProcessStepFilter.es"));

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

    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 _extends() {
      _extends = Object.assign || function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }return target;
      };return _extends.apply(this, arguments);
    }

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

    var Header = function Header(_ref) {
      var _ref$items = _ref.items,
          items = _ref$items === void 0 ? [] : _ref$items,
          instanceIds = _ref.instanceIds,
          totalCount = _ref.totalCount,
          withoutUnassigned = _ref.withoutUnassigned;

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

      var prefixKey = 'bulk';

      var previousCount = (0, _frontendJsReactWeb.usePrevious)(totalCount);

      var _useContext2 = (0, _react.useContext)(_ModalProvider.ModalContext),
          processId = _useContext2.processId,
          _useContext2$selectTa = _useContext2.selectTasks,
          selectAll = _useContext2$selectTa.selectAll,
          tasks = _useContext2$selectTa.tasks,
          setSelectTasks = _useContext2.setSelectTasks;

      var _useFilter = (0, _useFilter3.useFilter)({
        filterKeys: ['processStep', 'assignee'],
        prefixKeys: [prefixKey],
        withoutRouteParams: true
      }),
          prefixedKeys = _useFilter.prefixedKeys,
          selectedFilters = _useFilter.selectedFilters;

      var _useFilter2 = (0, _useFilter3.useFilter)({}),
          _useFilter2$filterVal = _useFilter2.filterValues,
          _useFilter2$filterVal2 = _useFilter2$filterVal.assigneeIds,
          userIds = _useFilter2$filterVal2 === void 0 ? [] : _useFilter2$filterVal2,
          slaStatuses = _useFilter2$filterVal.slaStatuses,
          taskNames = _useFilter2$filterVal.taskNames;

      var availableUsers = withoutUnassigned ? [userId] : [userId, '-1'];
      var assigneeIds = userIds.filter(function (id) {
        return availableUsers.includes(id);
      });
      var currentAndUnassigned = [userId, '-1'].every(function (user) {
        return userIds.includes(user);
      });
      var hideAssigneeFilter = !currentAndUnassigned && userIds.length;
      var stepFilterOptions = {
        requestBody: {
          assigneeIds: withoutUnassigned ? availableUsers : assigneeIds,
          instanceIds: instanceIds,
          processId: processId,
          slaStatuses: slaStatuses,
          taskNames: taskNames
        },
        requestMethod: 'post',
        requestUrl: '/tasks?page=0&pageSize=0',
        withoutRouteParams: true
      };
      var selectedOnPage = tasks.filter(function (item) {
        return items.find(function (_ref2) {
          var id = _ref2.id;
          return id === item.id;
        });
      });
      var allPageSelected = items.length > 0 && items.length === selectedOnPage.length;
      var checkbox = {
        checked: allPageSelected || selectAll,
        indeterminate: selectedOnPage.length > 0 && !allPageSelected && !selectAll
      };
      var remainingItems = items.filter(function (item) {
        return !tasks.find(function (_ref3) {
          var id = _ref3.id;
          return item.id === id;
        });
      });
      var toolbarActive = tasks.length > 0;
      (0, _react.useEffect)(function () {
        if (selectAll && remainingItems.length > 0 && previousCount === totalCount) {
          setSelectTasks({
            selectAll: selectAll,
            tasks: items
          });
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [items]);
      (0, _react.useEffect)(function () {
        setSelectTasks(function (selectTasks) {
          return _objectSpread(_objectSpread({}, selectTasks), {}, {
            selectAll: totalCount > 0 && totalCount === tasks.length
          });
        }); // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [totalCount]);

      return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ToolbarWithSelection.default, _extends({}, checkbox, {
          active: toolbarActive,
          handleCheck: function handleCheck(checked) {
            return function () {
              var updatedItems = checked ? [].concat(_toConsumableArray(tasks), _toConsumableArray(remainingItems)) : tasks.filter(function (item) {
                return !items.find(function (_ref4) {
                  var id = _ref4.id;
                  return id === item.id;
                });
              });
              setSelectTasks({
                selectAll: totalCount > 0 && totalCount === updatedItems.length,
                tasks: updatedItems
              });
            };
          }(!checkbox.indeterminate && !selectAll && !allPageSelected),
          handleClear: function handleClear() {
            setSelectTasks({
              selectAll: false,
              tasks: []
            });
          },
          handleSelectAll: function handleSelectAll() {
            setSelectTasks({
              selectAll: true,
              tasks: items
            });
          },
          selectAll: selectAll,
          selectedCount: tasks.length,
          totalCount: totalCount
        }), !toolbarActive && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_managementToolbar.default.Item, null, /*#__PURE__*/_react.default.createElement("strong", {
          className: "ml-0 mr-0 navbar-text"
        }, Liferay.Language.get('filter-by'))), /*#__PURE__*/_react.default.createElement(_ProcessStepFilter.default, {
          options: stepFilterOptions,
          prefixKey: prefixKey,
          processId: processId
        }), !withoutUnassigned && !hideAssigneeFilter && /*#__PURE__*/_react.default.createElement(_AssigneeFilter.default, {
          options: {
            withoutRouteParams: true
          },
          prefixKey: prefixKey,
          processId: processId,
          staticData: [{
            id: userId,
            name: userName
          }]
        }))), selectedFilters.length > 0 && /*#__PURE__*/_react.default.createElement(_ResultsBar.default, null, /*#__PURE__*/_react.default.createElement(_ResultsBar.default.TotalCount, {
          totalCount: totalCount
        }), /*#__PURE__*/_react.default.createElement(_ResultsBar.default.FilterItems, {
          filters: selectedFilters,
          withoutRouteParams: true
        }), /*#__PURE__*/_react.default.createElement(_ResultsBar.default.Clear, {
          filterKeys: prefixedKeys,
          filters: selectedFilters,
          withoutRouteParams: true
        })))
      );
    };

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