Liferay.Loader.define("portal-workflow-metrics-web@2.0.59/js/components/sla/form-page/SLAFormPageBody.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/form', '@frontend-taglib-clay$clayui/layout', 'frontend-js-react-web$react', '../../../shared/components/content-view/ContentView.es', '../../../shared/components/form/FormGroupWithStatus.es', '../../../shared/components/list/ReloadButton.es', '../../../shared/components/router/queryString.es', '../../../shared/components/toaster/hooks/useToaster.es', '../../../shared/hooks/usePageTitle.es', '../../AppContext.es', '../SLAContainer.es', './SLAFormConstants.es', './SLAFormPageAlerts.es', './SLAFormPageProvider.es', './sections/DurationSection.es', './sections/TimeFrameSection.es', './util/slaFormUtil.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.Body = void 0;

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

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

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

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

    var _ContentView = _interopRequireDefault(require("../../../shared/components/content-view/ContentView.es"));

    var _FormGroupWithStatus = _interopRequireDefault(require("../../../shared/components/form/FormGroupWithStatus.es"));

    var _ReloadButton = _interopRequireDefault(require("../../../shared/components/list/ReloadButton.es"));

    var _queryString = require("../../../shared/components/router/queryString.es");

    var _useToaster = require("../../../shared/components/toaster/hooks/useToaster.es");

    var _usePageTitle = require("../../../shared/hooks/usePageTitle.es");

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

    var _SLAContainer = require("../SLAContainer.es");

    var _SLAFormConstants = require("./SLAFormConstants.es");

    var _SLAFormPageAlerts = require("./SLAFormPageAlerts.es");

    var _SLAFormPageProvider = require("./SLAFormPageProvider.es");

    var _DurationSection = require("./sections/DurationSection.es");

    var _TimeFrameSection = require("./sections/TimeFrameSection.es");

    var _slaFormUtil = require("./util/slaFormUtil.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;
    }

    var Body = function Body(_ref) {
      var history = _ref.history,
          id = _ref.id,
          processId = _ref.processId,
          query = _ref.query;

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

      var _useContext2 = (0, _react.useContext)(_SLAContainer.SLAContext),
          setSLAUpdated = _useContext2.setSLAUpdated;

      var _useContext3 = (0, _react.useContext)(_SLAFormPageProvider.SLAFormContext),
          changeValue = _useContext3.changeValue,
          errors = _useContext3.errors,
          fetchNodes = _useContext3.fetchNodes,
          resetNodes = _useContext3.resetNodes,
          saveSLA = _useContext3.saveSLA,
          setErrors = _useContext3.setErrors,
          sla = _useContext3.sla;

      var toaster = (0, _useToaster.useToaster)();

      var _parse = (0, _queryString.parse)(query),
          slaInfoLink = _parse.slaInfoLink;

      (0, _usePageTitle.usePageTitle)(id ? sla.name : Liferay.Language.get('new-sla'));

      var handleErrors = function handleErrors(error) {
        var _ref2 = error.response || {},
            data = _ref2.data;

        if (Array.isArray(data)) {
          data.forEach(function (_ref3) {
            var fieldName = _ref3.fieldName,
                message = _ref3.message;
            errors[fieldName || _SLAFormConstants.ALERT_MESSAGE] = message;
          });
          var nodeKeys = [_SLAFormConstants.PAUSE_NODE_KEYS, _SLAFormConstants.START_NODE_KEYS, _SLAFormConstants.STOP_NODE_KEYS];
          var nodeErrors = data.filter(function (_ref4) {
            var fieldName = _ref4.fieldName;
            return nodeKeys.includes(fieldName);
          });

          if (nodeErrors.length) {
            resetNodes();
            fetchNodes(processId);
          }

          setErrors(_objectSpread({}, errors));
        } else {
          toaster.danger(Liferay.Language.get('your-request-has-failed'));
        }
      };

      var handleSubmit = (0, _react.useCallback)(function () {
        var _objectSpread2;

        var newErrors = _objectSpread(_objectSpread({}, errors), {}, (_objectSpread2 = {}, _defineProperty(_objectSpread2, _SLAFormConstants.ALERT_MESSAGE, ''), _defineProperty(_objectSpread2, _SLAFormConstants.DURATION, (0, _slaFormUtil.validateDuration)(sla.days, sla.hours)), _defineProperty(_objectSpread2, _SLAFormConstants.HOURS, (0, _slaFormUtil.validateHours)(sla.hours)), _defineProperty(_objectSpread2, _SLAFormConstants.NAME, (0, _slaFormUtil.validateName)(sla.name)), _defineProperty(_objectSpread2, _SLAFormConstants.PAUSE_NODE_KEYS, ''), _defineProperty(_objectSpread2, _SLAFormConstants.START_NODE_KEYS, (0, _slaFormUtil.validateNodeKeys)(sla.startNodeKeys.nodeKeys)), _defineProperty(_objectSpread2, _SLAFormConstants.STOP_NODE_KEYS, (0, _slaFormUtil.validateNodeKeys)(sla.stopNodeKeys.nodeKeys)), _objectSpread2));

        if ((!sla.hours || sla.hours === '00:00') && sla.days && Number(sla.days) > 0) {
          newErrors[_SLAFormConstants.HOURS] = '';
        }

        if ((0, _slaFormUtil.hasErrors)(newErrors)) {
          setErrors(_objectSpread(_objectSpread({}, newErrors), {}, _defineProperty({}, _SLAFormConstants.ALERT_MESSAGE, Liferay.Language.get('please-fill-in-the-required-fields'))));
        } else {
          saveSLA().then(function () {
            if (id) {
              setSLAUpdated(true);
              history.goBack();
              toaster.success(Liferay.Language.get('sla-was-updated'));
            } else {
              if (slaInfoLink) {
                history.push({
                  pathname: "/sla/".concat(processId, "/list/").concat(defaultDelta, "/1"),
                  search: query
                });
              } else {
                history.goBack();
              }

              toaster.success(Liferay.Language.get('sla-was-saved'));
            }
          }).catch(handleErrors);
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [id, processId, saveSLA, sla]);

      var onChangeHandler = function onChangeHandler(validateFunction) {
        return function (_ref5) {
          var _ref5$target = _ref5.target,
              name = _ref5$target.name,
              _ref5$target$value = _ref5$target.value,
              value = _ref5$target$value === void 0 ? '' : _ref5$target$value;
          changeValue(name, value);

          if (typeof validateFunction === 'function') {
            validateFunction(value);
          }
        };
      };

      var statesProps = {
        errorProps: {
          actionButton: /*#__PURE__*/_react.default.createElement(_ReloadButton.default, null),
          className: 'pb-5 pt-6 sheet sheet-lg',
          hideAnimation: true,
          message: Liferay.Language.get('there-was-a-problem-retrieving-data-please-try-reloading-the-page')
        },
        loadingProps: {
          className: 'py-5 sheet sheet-lg'
        }
      };
      return (/*#__PURE__*/_react.default.createElement(_ContentView.default, statesProps, /*#__PURE__*/_react.default.createElement("div", {
          className: "alert-container"
        }, errors[_SLAFormConstants.ALERT_MESSAGE] && /*#__PURE__*/_react.default.createElement(Body.AlertMessage, {
          message: errors[_SLAFormConstants.ALERT_MESSAGE]
        }), sla.status === 2 && /*#__PURE__*/_react.default.createElement(Body.AlertChange, null)), /*#__PURE__*/_react.default.createElement(_form.default, null, /*#__PURE__*/_react.default.createElement(_layout.default.Sheet, {
          size: "lg"
        }, /*#__PURE__*/_react.default.createElement(_layout.default.SheetHeader, {
          className: "mb-0"
        }, /*#__PURE__*/_react.default.createElement("h2", {
          className: "sheet-title"
        }, Liferay.Language.get('sla-definition'))), /*#__PURE__*/_react.default.createElement(_layout.default.SheetSection, {
          className: "mb-0"
        }, /*#__PURE__*/_react.default.createElement(_layout.default.Row, null, /*#__PURE__*/_react.default.createElement(_layout.default.Col, {
          sm: "5"
        }, /*#__PURE__*/_react.default.createElement(_FormGroupWithStatus.default, {
          className: "form-group",
          error: errors[_SLAFormConstants.NAME],
          htmlFor: "slaName",
          label: Liferay.Language.get('name'),
          requiredLabel: true
        }, /*#__PURE__*/_react.default.createElement(_form.ClayInput, {
          autoFocus: true,
          className: "form-control",
          id: "slaName",
          maxLength: 75,
          name: "name",
          onChange: onChangeHandler(function onNameChanged(newName) {
            var _objectSpread4;

            setErrors(_objectSpread(_objectSpread({}, errors), {}, (_objectSpread4 = {}, _defineProperty(_objectSpread4, _SLAFormConstants.ALERT_MESSAGE, ''), _defineProperty(_objectSpread4, _SLAFormConstants.NAME, (0, _slaFormUtil.validateName)(newName)), _objectSpread4)));
          }),
          type: "text",
          value: sla.name
        }))), /*#__PURE__*/_react.default.createElement(_layout.default.Col, {
          sm: "7"
        }, /*#__PURE__*/_react.default.createElement(_FormGroupWithStatus.default, {
          className: "form-group",
          htmlFor: "slaDescription",
          label: Liferay.Language.get('description')
        }, /*#__PURE__*/_react.default.createElement(_form.ClayInput, {
          id: "slaDescription",
          name: "description",
          onChange: onChangeHandler(),
          type: "text",
          value: sla.description
        })))), /*#__PURE__*/_react.default.createElement(Body.TimeFrameSection, null), /*#__PURE__*/_react.default.createElement(Body.DurationSection, {
          onChangeHandler: onChangeHandler
        })), /*#__PURE__*/_react.default.createElement(_layout.default.SheetFooter, {
          className: "sheet-footer-btn-block-sm-down"
        }, /*#__PURE__*/_react.default.createElement(_button.default.Group, {
          spaced: true
        }, /*#__PURE__*/_react.default.createElement(_button.default, {
          onClick: handleSubmit
        }, id ? Liferay.Language.get('update') : Liferay.Language.get('save')), /*#__PURE__*/_react.default.createElement(_button.default, {
          displayType: "secondary",
          onClick: function onClick() {
            return history.goBack();
          }
        }, Liferay.Language.get('cancel')))))))
      );
    };

    exports.Body = Body;
    Body.AlertChange = _SLAFormPageAlerts.AlertChange;
    Body.AlertMessage = _SLAFormPageAlerts.AlertMessage;
    Body.DurationSection = _DurationSection.DurationSection;
    Body.TimeFrameSection = _TimeFrameSection.TimeFrameSection;
    //# sourceMappingURL=SLAFormPageBody.es.js.map
  }
});
//# sourceMappingURL=SLAFormPageBody.es.js.map