Liferay.Loader.define("portal-workflow-metrics-web@2.0.42/js/components/sla/form-page/hooks/useSLANodes.es", ['module', 'exports', 'require', 'frontend-js-react-web$react', '../../../../shared/hooks/useFetch.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.useSLANodes = void 0;

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

    var _useFetch2 = require("../../../../shared/hooks/useFetch.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 _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 useSLANodes = function useSLANodes(processId) {
      var _useState = (0, _react.useState)([]),
          _useState2 = _slicedToArray(_useState, 2),
          nodes = _useState2[0],
          setNodes = _useState2[1];

      var _useFetch = (0, _useFetch2.useFetch)({
        callback: function parseNodes(_ref) {
          var items = _ref.items;
          var nodeBegins = [];
          var nodeEnds = [];
          var nodeEnters = [];
          var nodeLeaves = [];
          items.forEach(function (node) {
            if (node.type === 'STATE') {
              var newNode = _objectSpread({}, node, {
                desc: node.initial ? Liferay.Language.get('process-begins') : "".concat(Liferay.Language.get('process-ends'), " ").concat(node.label),
                executionType: node.initial ? 'begin' : 'end'
              });

              if (node.initial) {
                nodeBegins.push(newNode);
              } else {
                nodeEnds.push(newNode);
              }
            } else if (node.type === 'TASK') {
              nodeEnters.push(_objectSpread({}, node, {
                desc: "".concat(Liferay.Language.get('enters-task'), " ").concat(node.label),
                executionType: 'enter'
              }));
              nodeLeaves.push(_objectSpread({}, node, {
                desc: "".concat(Liferay.Language.get('leaves-task'), " ").concat(node.label),
                executionType: 'leave'
              }));
            }
          });

          var compareToLabel = function compareToLabel(curNode, nextNode) {
            return curNode.label.localeCompare(nextNode.label);
          };

          nodeEnters.sort(compareToLabel);
          nodeLeaves.sort(compareToLabel);
          var nodes = [].concat(nodeBegins, nodeEnters, nodeLeaves, nodeEnds).map(function (node) {
            return _objectSpread({}, node, {
              compositeId: "".concat(node.id, ":").concat(node.executionType)
            });
          });
          setNodes(nodes);
        },
        url: "/processes/".concat(processId, "/nodes")
      }),
          fetchNodes = _useFetch.fetchData;

      return {
        fetchNodes: fetchNodes,
        getPauseNodes: function getPauseNodes(startNodeKeys, stopNodeKeys) {
          var selectedNodes = [].concat(_toConsumableArray(startNodeKeys), _toConsumableArray(stopNodeKeys)).map(function (_ref2) {
            var id = _ref2.id;
            return "".concat(id);
          }).filter(function (id, index, self) {
            return self.indexOf(id) === index;
          });
          var onTaskString = Liferay.Language.get('on-task');
          return nodes.filter(function (_ref3) {
            var type = _ref3.type;
            return type === 'TASK';
          }).filter(function (_ref4) {
            var id = _ref4.id;
            return !selectedNodes.includes("".concat(id));
          }).filter(function (node, index, self) {
            return self.findIndex(function (_ref5) {
              var id = _ref5.id;
              return id == node.id;
            }) === index;
          }).map(function (node) {
            return _objectSpread({}, node, {
              compositeId: "".concat(node.id, ":on"),
              desc: "".concat(onTaskString, " ").concat(node.label),
              executionType: 'on'
            });
          });
        },
        getStartNodes: function getStartNodes(pauseNodeKeys, stopNodeKeys) {
          var pauseNodeKeyIds = pauseNodeKeys.map(function (_ref6) {
            var id = _ref6.id;
            return "".concat(id);
          });
          var nodeKeyCompositeIds = stopNodeKeys.map(function (_ref7) {
            var executionType = _ref7.executionType,
                id = _ref7.id;
            return "".concat(id, ":").concat(executionType);
          });
          return nodes.filter(function (_ref8) {
            var compositeId = _ref8.compositeId,
                id = _ref8.id;
            return !nodeKeyCompositeIds.includes("".concat(compositeId)) && !pauseNodeKeyIds.includes("".concat(id));
          }).filter(function (_ref9) {
            var initial = _ref9.initial,
                type = _ref9.type;
            return type === 'STATE' && initial === true || type === 'TASK';
          });
        },
        getStopNodes: function getStopNodes(pauseNodeKeys, startNodeKeys) {
          var pauseNodeKeyIds = pauseNodeKeys.map(function (_ref10) {
            var id = _ref10.id;
            return "".concat(id);
          });
          var nodeKeyCompositeIds = startNodeKeys.map(function (_ref11) {
            var executionType = _ref11.executionType,
                id = _ref11.id;
            return "".concat(id, ":").concat(executionType);
          });
          return nodes.filter(function (_ref12) {
            var compositeId = _ref12.compositeId,
                id = _ref12.id;
            return !nodeKeyCompositeIds.includes("".concat(compositeId)) && !pauseNodeKeyIds.includes("".concat(id));
          }).filter(function (_ref13) {
            var terminal = _ref13.terminal,
                type = _ref13.type;
            return type === 'STATE' && terminal === true || type === 'TASK';
          });
        },
        nodes: nodes
      };
    };

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