Liferay.Loader.define("segments-experiment-web@2.0.19/js/components/SegmentsExperimentsModal.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', 'frontend-js-react-web', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', '../types.es', './BusyButton/BusyButton.es', './ValidatedInput/ValidatedInput.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["default"] = void 0;

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

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

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

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

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

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

    var _propTypes = _interopRequireDefault(require("frontend-js-react-web$prop-types"));

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

    var _types = require("../types.es");

    var _BusyButton = _interopRequireDefault(require("./BusyButton/BusyButton.es"));

    var _ValidatedInput = _interopRequireDefault(require("./ValidatedInput/ValidatedInput.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 _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 SegmentsExperimentsModal(_ref) {
      var _ref$description = _ref.description,
          description = _ref$description === void 0 ? '' : _ref$description,
          error = _ref.error,
          goal = _ref.goal,
          _ref$goals = _ref.goals,
          goals = _ref$goals === void 0 ? [] : _ref$goals,
          _ref$name = _ref.name,
          name = _ref$name === void 0 ? '' : _ref$name,
          onClose = _ref.onClose,
          onSave = _ref.onSave,
          segmentsExperienceId = _ref.segmentsExperienceId,
          segmentsExperimentId = _ref.segmentsExperimentId,
          title = _ref.title;

      var _useState = (0, _react.useState)(false),
          _useState2 = _slicedToArray(_useState, 2),
          busy = _useState2[0],
          setBusy = _useState2[1];

      var _useState3 = (0, _react.useState)(description),
          _useState4 = _slicedToArray(_useState3, 2),
          inputDescription = _useState4[0],
          setInputDescription = _useState4[1];

      var _useState5 = (0, _react.useState)(goal && goal.value || goals[0] && goals[0].value),
          _useState6 = _slicedToArray(_useState5, 2),
          inputGoal = _useState6[0],
          setInputGoal = _useState6[1];

      var _useState7 = (0, _react.useState)(name),
          _useState8 = _slicedToArray(_useState7, 2),
          inputName = _useState8[0],
          setInputName = _useState8[1];

      var _useState9 = (0, _react.useState)(false),
          _useState10 = _slicedToArray(_useState9, 2),
          invalidForm = _useState10[0],
          setInvalidForm = _useState10[1];

      var isMounted = (0, _frontendJsReactWeb.useIsMounted)();
      return _react["default"].createElement(_react["default"].Fragment, null, _react["default"].createElement(_modal["default"].Header, null, title), _react["default"].createElement(_modal["default"].Body, null, _react["default"].createElement("form", {
        onSubmit: function _handleFormSubmit(event) {
          event.preventDefault();

          _handleSave();
        }
      }, error && _react["default"].createElement(_alert["default"], {
        displayType: "danger",
        title: Liferay.Language.get('error')
      }, error), _react["default"].createElement(_ValidatedInput["default"], {
        autofocus: true,
        errorMessage: Liferay.Language.get('test-name-is-required'),
        label: Liferay.Language.get('test-name'),
        onChange: function _handleNameChange(event) {
          setInputName(event.target.value);
        },
        onValidationChange: function _handleInputNameValidation(error) {
          setInvalidForm(error);
        }
        /**
         * Triggers `onSave` prop
         *
         * Resets `goalTarget` if goal is not 'click'
         */

        ,
        value: inputName
      }), _react["default"].createElement("div", {
        className: "form-group"
      }, _react["default"].createElement("label", null, Liferay.Language.get('description')), _react["default"].createElement("textarea", {
        className: "form-control",
        maxLength: "4000",
        onChange: function _handleDescriptionChange(event) {
          setInputDescription(event.target.value);
        },
        placeholder: Liferay.Language.get('description-placeholder'),
        value: inputDescription
      })), goals.length > 0 && _react["default"].createElement("div", {
        className: "form-group"
      }, _react["default"].createElement("label", {
        className: "w100"
      }, Liferay.Language.get('select-goal'), _react["default"].createElement(_icon["default"], {
        className: "lexicon-icon-sm ml-1 reference-mark text-warning",
        style: {
          verticalAlign: 'super'
        },
        symbol: "asterisk"
      }), _react["default"].createElement(_form.ClaySelect, {
        className: "mt-1",
        defaultValue: inputGoal,
        onChange: function _handleGoalChange(event) {
          setInputGoal(event.target.value);
        }
      }, goals.map(function (goal) {
        return _react["default"].createElement(_form.ClaySelect.Option, {
          key: goal.value,
          label: goal.label,
          value: goal.value
        });
      })))))), _react["default"].createElement(_modal["default"].Footer, {
        last: _react["default"].createElement(_button["default"].Group, {
          spaced: true
        }, _react["default"].createElement(_button["default"], {
          displayType: "secondary",
          onClick: onClose,
          type: "button"
        }, Liferay.Language.get('cancel')), _react["default"].createElement(_BusyButton["default"], {
          busy: busy,
          disabled: invalidForm || busy,
          displayType: "primary",
          onClick: _handleSave
        }, Liferay.Language.get('save')))
      }));

      function _handleSave() {
        if (!invalidForm && !busy) {
          var goalTarget = inputGoal === 'click' ? goal && goal.target ? goal.target : '' : '';
          setBusy(true);
          onSave({
            description: inputDescription,
            goal: inputGoal,
            goalTarget: goalTarget,
            name: inputName,
            segmentsExperienceId: segmentsExperienceId,
            segmentsExperimentId: segmentsExperimentId
          })["finally"](function () {
            if (isMounted()) {
              setBusy(false);
            }
          });
        }
      }
    }

    var _default = SegmentsExperimentsModal;
    exports["default"] = _default;
    //# sourceMappingURL=SegmentsExperimentsModal.es.js.map
  }
});
//# sourceMappingURL=SegmentsExperimentsModal.es.js.map