Liferay.Loader.define("portal-workflow-metrics-web@2.0.42/js/components/instance-list-page/InstanceListPageHeader.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/layout', '@frontend-taglib-clay$clayui/management-toolbar', 'frontend-js-react-web', 'frontend-js-react-web$react', '../../shared/components/filter/util/filterConstants.es', '../../shared/components/quick-action-kebab/QuickActionKebab.es', '../../shared/components/results-bar/ResultsBar.es', '../../shared/components/toolbar-with-selection/ToolbarWithSelection.es', '../../shared/util/util.es', '../AppContext.es', '../filter/AssigneeFilter.es', '../filter/ProcessStatusFilter.es', '../filter/ProcessStepFilter.es', '../filter/SLAStatusFilter.es', '../filter/TimeRangeFilter.es', './InstanceListPageProvider.es', './modal/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 _layout = _interopRequireDefault(require("@frontend-taglib-clay$clayui/layout"));

    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 _filterConstants = _interopRequireDefault(require("../../shared/components/filter/util/filterConstants.es"));

    var _QuickActionKebab = _interopRequireDefault(require("../../shared/components/quick-action-kebab/QuickActionKebab.es"));

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

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

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

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

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

    var _ProcessStatusFilter = _interopRequireWildcard(require("../filter/ProcessStatusFilter.es"));

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

    var _SLAStatusFilter = _interopRequireDefault(require("../filter/SLAStatusFilter.es"));

    var _TimeRangeFilter = _interopRequireDefault(require("../filter/TimeRangeFilter.es"));

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

    var _ModalProvider = require("./modal/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;
      }
    }

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

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

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

      var _useContext3 = (0, _react.useContext)(_ModalProvider.ModalContext),
          openModal = _useContext3.openModal;

      var previousCount = (0, _frontendJsReactWeb.usePrevious)(totalCount);
      var handleClick = (0, _react.useCallback)(function (bulkModal, singleModal) {
        var bulkOperation = selectedItems.length > 1 || selectedItems[0].taskNames.length > 1;
        openModal(bulkOperation ? bulkModal : singleModal);
      }, [openModal, selectedItems]);

      var compareId = function compareId(itemId) {
        return function (_ref2) {
          var id = _ref2.id;
          return id === itemId;
        };
      };

      var kebabItems = [{
        icon: 'arrow-start',
        label: (0, _util.capitalize)(Liferay.Language.get('transition')),
        onClick: function onClick() {
          openModal('bulkTransition');
        }
      }, {
        icon: 'date',
        label: Liferay.Language.get('update-tasks-due-dates'),
        onClick: function onClick() {
          return handleClick('bulkUpdateDueDate', 'updateDueDate');
        }
      }, {
        icon: 'change',
        label: Liferay.Language.get('reassign-task'),
        onClick: function onClick() {
          return handleClick('bulkReassign', 'singleReassign');
        }
      }];
      var selectedOnPage = (0, _react.useMemo)(function () {
        return selectedItems.filter(function (_ref3) {
          var id = _ref3.id;
          return items.find(compareId(id));
        });
      }, [items, selectedItems]);
      var allPageSelected = items.length > 0 && items.length === selectedOnPage.length;
      var checkbox = {
        checked: allPageSelected || selectAll,
        indeterminate: selectedOnPage.length > 0 && !allPageSelected && !selectAll
      };
      var isRemainingItem = (0, _react.useCallback)(function (clear) {
        return function (_ref4) {
          var _ref4$assignees = _ref4.assignees,
              assignees = _ref4$assignees === void 0 ? [] : _ref4$assignees,
              id = _ref4.id,
              status = _ref4.status;
          var assignedToUser = !!assignees.find(function (_ref5) {
            var id = _ref5.id;
            return id === Number(userId);
          });
          var completed = status === _ProcessStatusFilter.processStatusConstants.completed;
          var selected = clear && selectedItems.find(compareId(id));

          var _ref6 = assignees.find(function (_ref7) {
            var id = _ref7.id;
            return id === -1;
          }) || {},
              reviewer = _ref6.reviewer;

          return (reviewer || assignedToUser) && !completed && !selected;
        };
      }, [selectedItems, userId]);
      var remainingItems = (0, _react.useMemo)(function () {
        return items.filter(isRemainingItem(true));
      }, [items, isRemainingItem]);
      var toolbarActive = (0, _react.useMemo)(function () {
        return selectedItems.length > 0;
      }, [selectedItems]);
      (0, _react.useEffect)(function () {
        if (selectAll && remainingItems.length > 0 && previousCount === totalCount) {
          setSelectedItems([].concat(_toConsumableArray(selectedItems), _toConsumableArray(items.filter(isRemainingItem()))));
          setSelectAll(items.length === remainingItems.length);
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [items]);
      (0, _react.useEffect)(function () {
        setSelectAll(totalCount > 0 && totalCount === selectedItems.length); // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [totalCount]);

      var handleCheck = (0, _react.useCallback)(function (checked) {
        return function () {
          var updatedItems = checked ? [].concat(_toConsumableArray(selectedItems), _toConsumableArray(remainingItems)) : selectedItems.filter(function (_ref8) {
            var id = _ref8.id;
            return !items.find(compareId(id));
          });
          setSelectAll(totalCount > 0 && totalCount === updatedItems.length);
          setSelectedItems(updatedItems);
        };
      }, // eslint-disable-next-line react-hooks/exhaustive-deps
      [items, remainingItems, selectedItems]);
      var statusesFilterItem = (0, _react.useMemo)(function () {
        return selectedFilters.find(function (filter) {
          return filter.key === 'statuses';
        });
      }, [selectedFilters]);

      var _ref9 = statusesFilterItem ? statusesFilterItem.items[0] : {},
          key = _ref9.key;

      var completedStatusSelected = (0, _react.useMemo)(function () {
        return selectedFilters.length > 0 && statusesFilterItem ? key === _ProcessStatusFilter.processStatusConstants.completed : false;
      }, // eslint-disable-next-line react-hooks/exhaustive-deps
      [key]);
      var selectedFilterItems = (0, _react.useMemo)(function () {
        return selectedFilters.filter(function (filter) {
          return completedStatusSelected || filter.key !== _filterConstants["default"].timeRange.key;
        });
      }, [completedStatusSelected, selectedFilters]);
      return _react["default"].createElement(_react["default"].Fragment, null, _react["default"].createElement(_ToolbarWithSelection["default"], _extends({}, checkbox, {
        active: toolbarActive,
        handleCheck: handleCheck(!checkbox.indeterminate && !selectAll && !allPageSelected),
        handleClear: function handleClear() {
          setSelectedItems([]);
          setSelectAll(false);
        },
        handleSelectAll: function handleSelectAll() {
          setSelectedItems(items);
          setSelectAll(true);
        },
        selectAll: selectAll,
        selectedCount: selectedItems.length,
        totalCount: totalCount
      }), toolbarActive ? _react["default"].createElement(_managementToolbar["default"].Item, {
        className: "navbar-nav-last"
      }, _react["default"].createElement(_layout["default"].ContentCol, {
        "data-testid": "headerQuickAction"
      }, _react["default"].createElement(_QuickActionKebab["default"], {
        items: kebabItems
      }))) : _react["default"].createElement(_react["default"].Fragment, null, _react["default"].createElement(_managementToolbar["default"].Item, null, _react["default"].createElement("strong", {
        className: "ml-0 mr-0 navbar-text"
      }, Liferay.Language.get('filter-by'))), _react["default"].createElement(_SLAStatusFilter["default"], null), _react["default"].createElement(_ProcessStatusFilter["default"], null), completedStatusSelected && _react["default"].createElement(_TimeRangeFilter["default"], {
        options: {
          withSelectionTitle: false
        }
      }), _react["default"].createElement(_ProcessStepFilter["default"], {
        processId: routeParams.processId
      }), _react["default"].createElement(_AssigneeFilter["default"], {
        processId: routeParams.processId
      }))), selectedFilterItems.length > 0 && _react["default"].createElement(_ResultsBar["default"], null, _react["default"].createElement(_ResultsBar["default"].TotalCount, {
        search: routeParams.search,
        totalCount: totalCount
      }), _react["default"].createElement(_ResultsBar["default"].FilterItems, _extends({
        filters: selectedFilterItems
      }, routeParams)), _react["default"].createElement(_ResultsBar["default"].Clear, _extends({
        filterKeys: filterKeys,
        filters: selectedFilters
      }, routeParams))));
    };

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