Liferay.Loader.define("app-builder-workflow-web@1.0.40/js/pages/entry/ReassignEntryModal.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/alert', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/form', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/modal', 'app-builder-web/js/components/loading/Loading.es', 'app-builder-web/js/utils/client.es', 'app-builder-web/js/utils/toast.es', 'frontend-js-react-web$react', '../../components/select-dropdown/SelectDropdown.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.AssigneeInput = AssigneeInput;
    exports.default = ReassignEntryModal;

    var _alert = _interopRequireDefault(require("@frontend-taglib-clay$clayui/alert"));

    var _button = _interopRequireDefault(require("@frontend-taglib-clay$clayui/button"));

    var _form = _interopRequireWildcard(require("@frontend-taglib-clay$clayui/form"));

    var _icon = _interopRequireDefault(require("@frontend-taglib-clay$clayui/icon"));

    var _modal = _interopRequireWildcard(require("@frontend-taglib-clay$clayui/modal"));

    var _Loading = require("app-builder-web/js/components/loading/Loading.es");

    var _client = require("app-builder-web/js/utils/client.es");

    var _toast = require("app-builder-web/js/utils/toast.es");

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

    var _SelectDropdown = _interopRequireDefault(require("../../components/select-dropdown/SelectDropdown.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 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;
    }

    function AssigneeInput(_ref) {
      var _ref$label = _ref.label,
          label = _ref$label === void 0 ? Liferay.Language.get('new-assignee') : _ref$label,
          onSelectAssignee = _ref.onSelectAssignee,
          required = _ref.required,
          selectedAssignee = _ref.selectedAssignee,
          taskId = _ref.taskId;

      var _useState = (0, _react.useState)({
        hasError: false,
        isLoading: true
      }),
          _useState2 = _slicedToArray(_useState, 2),
          _useState2$ = _useState2[0],
          hasError = _useState2$.hasError,
          isLoading = _useState2$.isLoading,
          setState = _useState2[1];

      var _useState3 = (0, _react.useState)([]),
          _useState4 = _slicedToArray(_useState3, 2),
          assignees = _useState4[0],
          setAssignees = _useState4[1];

      var fetchAssignees = (0, _react.useCallback)(function () {
        if (taskId) {
          setState({
            hasError: false,
            isLoading: true
          });
          (0, _client.getItem)("/o/headless-admin-workflow/v1.0/workflow-tasks/".concat(taskId, "/assignable-users"), {
            page: -1,
            pageSize: -1
          }).then(function (_ref2) {
            var items = _ref2.items;
            setAssignees(items);
            setState({
              hasError: false,
              isLoading: false
            });
          }).catch(function () {
            setState({
              hasError: true,
              isLoading: false
            });
          });
        }
      }, [taskId]);
      (0, _react.useEffect)(function () {
        fetchAssignees();
      }, [fetchAssignees]);
      return (/*#__PURE__*/_react.default.createElement(_form.default.Group, null, /*#__PURE__*/_react.default.createElement("label", null, label, ' ', required && /*#__PURE__*/_react.default.createElement("span", {
          className: "reference-mark"
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: "asterisk"
        }))), /*#__PURE__*/_react.default.createElement(_SelectDropdown.default, {
          emptyResultMessage: Liferay.Language.get('no-assignees-were-found-with-this-name-try-searching-again-with-a-different-name'),
          error: hasError,
          isLoading: isLoading,
          items: assignees,
          label: Liferay.Language.get('select-a-new-assignee'),
          onSelect: onSelectAssignee,
          selectedValue: selectedAssignee === null || selectedAssignee === void 0 ? void 0 : selectedAssignee.name,
          stateProps: {
            emptyProps: {
              label: Liferay.Language.get('there-are-no-assignees-yet')
            },
            errorProps: {
              children: /*#__PURE__*/_react.default.createElement(_button.default, {
                displayType: "link",
                onClick: fetchAssignees,
                small: true
              }, Liferay.Language.get('retry')),
              label: Liferay.Language.get('unable-to-retrieve-the-assignees')
            },
            loadingProps: {
              label: Liferay.Language.get('retrieving-all-assignees')
            }
          }
        }))
      );
    }

    function ReassignEntryModal(_ref3) {
      var entry = _ref3.entry,
          onCloseModal = _ref3.onCloseModal,
          refetch = _ref3.refetch;

      var _useState5 = (0, _react.useState)({
        comment: '',
        error: false,
        isLoading: false,
        isReassigning: false,
        selectedAssignee: null,
        taskId: null
      }),
          _useState6 = _slicedToArray(_useState5, 2),
          _useState6$ = _useState6[0],
          comment = _useState6$.comment,
          error = _useState6$.error,
          isLoading = _useState6$.isLoading,
          isReassigning = _useState6$.isReassigning,
          selectedAssignee = _useState6$.selectedAssignee,
          taskId = _useState6$.taskId,
          setState = _useState6[1];

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

      var fetchWorkflowTask = function fetchWorkflowTask(instanceId) {
        setState(function (state) {
          return _objectSpread(_objectSpread({}, state), {}, {
            error: false,
            isLoading: true
          });
        });
        (0, _client.getItem)("/o/headless-admin-workflow/v1.0/workflow-instances/".concat(instanceId, "/workflow-tasks"), {
          completed: false,
          page: 1,
          pageSize: 1
        }).then(function (_ref4) {
          var items = _ref4.items;
          var newState = {
            error: false,
            isLoading: false
          };

          if (items.length) {
            newState.taskId = items.pop().id;
          }

          setState(function (state) {
            return _objectSpread(_objectSpread({}, state), newState);
          });
        }).catch(function () {
          setState(function (state) {
            return _objectSpread(_objectSpread({}, state), {}, {
              error: true,
              isLoading: false
            });
          });
        });
      };

      var onDone = function onDone(assigneeId) {
        setState(function (state) {
          return _objectSpread(_objectSpread({}, state), {}, {
            error: false,
            isReassigning: true
          });
        });
        (0, _client.addItem)("/o/headless-admin-workflow/v1.0/workflow-tasks/".concat(taskId, "/assign-to-user"), {
          assigneeId: assigneeId,
          comment: comment
        }).then(function () {
          onClose();
          (0, _toast.successToast)(Liferay.Language.get('this-entry-has-been-reassigned'));
          refetch({
            entryInstanceId: entry.instanceId,
            newAssignee: selectedAssignee
          });
        }).catch(function () {
          setState(function (state) {
            return _objectSpread(_objectSpread({}, state), {}, {
              error: true,
              isReassigning: false
            });
          });
        });
      };

      (0, _react.useEffect)(function () {
        if (entry === null || entry === void 0 ? void 0 : entry.instanceId) {
          fetchWorkflowTask(entry.instanceId);
        }
      }, [entry]);
      return (/*#__PURE__*/_react.default.createElement(_modal.default, {
          center: true,
          observer: observer,
          size: "md"
        }, /*#__PURE__*/_react.default.createElement(_modal.default.Header, null, Liferay.Language.get('assign-to')), error && /*#__PURE__*/_react.default.createElement(_alert.default, {
          className: "mb-0",
          "data-testid": "alertError",
          displayType: "danger",
          title: Liferay.Language.get('error')
        }, Liferay.Language.get('an-unexpected-error-occurred')), /*#__PURE__*/_react.default.createElement(_modal.default.Body, null, /*#__PURE__*/_react.default.createElement(_Loading.Loading, {
          isLoading: isLoading
        }, /*#__PURE__*/_react.default.createElement(AssigneeInput, {
          onSelectAssignee: function onSelectAssignee(selectedAssignee) {
            return setState(function (state) {
              return _objectSpread(_objectSpread({}, state), {}, {
                selectedAssignee: selectedAssignee
              });
            });
          },
          required: true,
          selectedAssignee: selectedAssignee,
          taskId: taskId
        }), /*#__PURE__*/_react.default.createElement(_form.default.Group, null, /*#__PURE__*/_react.default.createElement("label", null, Liferay.Language.get('comment')), /*#__PURE__*/_react.default.createElement(_form.ClayInput, {
          component: "textarea",
          onChange: function onChange(_ref5) {
            var target = _ref5.target;
            return setState(function (state) {
              return _objectSpread(_objectSpread({}, state), {}, {
                comment: target.value
              });
            });
          },
          placeholder: Liferay.Language.get('comment'),
          type: "text",
          value: comment
        })))), /*#__PURE__*/_react.default.createElement(_modal.default.Footer, {
          last: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_button.default, {
            className: "mr-3",
            displayType: "secondary",
            onClick: onClose
          }, Liferay.Language.get('cancel')), /*#__PURE__*/_react.default.createElement(_button.default, {
            disabled: !(selectedAssignee === null || selectedAssignee === void 0 ? void 0 : selectedAssignee.id) || isReassigning,
            onClick: function onClick() {
              return onDone(selectedAssignee === null || selectedAssignee === void 0 ? void 0 : selectedAssignee.id);
            }
          }, Liferay.Language.get('done')))
        }))
      );
    }
    //# sourceMappingURL=ReassignEntryModal.es.js.map
  }
});
//# sourceMappingURL=ReassignEntryModal.es.js.map