Liferay.Loader.define("dynamic-data-mapping-form-field-type@5.0.45/Validation/Validation.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/form', 'frontend-js-react-web', 'frontend-js-react-web$react', '../Checkbox/Checkbox.es', '../Numeric/Numeric.es', '../Select/Select.es', '../Text/Text.es', '../util/strings.es', './transform.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 _form = _interopRequireDefault(require("@frontend-taglib-clay$clayui/form"));

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

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

    var _Checkbox = _interopRequireDefault(require("../Checkbox/Checkbox.es"));

    var _Numeric = _interopRequireDefault(require("../Numeric/Numeric.es"));

    var _Select = _interopRequireDefault(require("../Select/Select.es"));

    var _Text = _interopRequireDefault(require("../Text/Text.es"));

    var _strings = require("../util/strings.es");

    var _transform = require("./transform.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 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 Validation = function Validation(_ref) {
      var dataType = _ref.dataType,
          defaultLanguageId = _ref.defaultLanguageId,
          editingLanguageId = _ref.editingLanguageId,
          initialEnableValidation = _ref.enableValidation,
          initialErrorMessage = _ref.errorMessage,
          label = _ref.label,
          localizationMode = _ref.localizationMode,
          name = _ref.name,
          onChange = _ref.onChange,
          initialParameter = _ref.parameter,
          parameterMessage = _ref.parameterMessage,
          readOnly = _ref.readOnly,
          initialSelectedValidation = _ref.selectedValidation,
          spritemap = _ref.spritemap,
          validation = _ref.validation,
          validations = _ref.validations,
          value = _ref.value,
          visible = _ref.visible;

      var _useState = (0, _react.useState)({
        enableValidation: initialEnableValidation,
        errorMessage: initialErrorMessage,
        parameter: initialParameter,
        selectedValidation: initialSelectedValidation
      }),
          _useState2 = _slicedToArray(_useState, 2),
          _useState2$ = _useState2[0],
          enableValidation = _useState2$.enableValidation,
          errorMessage = _useState2$.errorMessage,
          parameter = _useState2$.parameter,
          selectedValidation = _useState2$.selectedValidation,
          setState = _useState2[1];

      var DynamicComponent = selectedValidation && selectedValidation.parameterMessage && dataType === 'string' ? _Text["default"] : _Numeric["default"];

      var handleChange = function handleChange(key, newValue) {
        setState(function (prevState) {
          var newState = _objectSpread({}, prevState, _defineProperty({}, key, newValue));

          var expression = {};

          if (newState.enableValidation) {
            expression = {
              name: newState.selectedValidation.name,
              value: (0, _strings.subWords)(newState.selectedValidation.template, {
                name: validation.fieldName
              })
            };
          }

          onChange({
            enableValidation: newState.enableValidation,
            errorMessage: _objectSpread({}, value.errorMessage, _defineProperty({}, editingLanguageId, newState.errorMessage)),
            expression: expression,
            parameter: _objectSpread({}, value.parameter, _defineProperty({}, editingLanguageId, !value.expression ? parameterMessage : newState.parameter))
          });
          return newState;
        });
      };

      var transformSelectedValidation = (0, _transform.getSelectedValidation)(validations);
      var prevEditingLanguageId = (0, _frontendJsReactWeb.usePrevious)(editingLanguageId);
      (0, _react.useEffect)(function () {
        if (prevEditingLanguageId !== editingLanguageId) {
          setState(function (prevState) {
            var _value$errorMessage = value.errorMessage,
                errorMessage = _value$errorMessage === void 0 ? {} : _value$errorMessage,
                _value$parameter = value.parameter,
                parameter = _value$parameter === void 0 ? {} : _value$parameter;
            return _objectSpread({}, prevState, {
              errorMessage: errorMessage[editingLanguageId] !== undefined ? errorMessage[editingLanguageId] : errorMessage[defaultLanguageId],
              parameter: parameter[editingLanguageId] !== undefined ? parameter[editingLanguageId] : parameter[defaultLanguageId]
            });
          });
        }
      }, [defaultLanguageId, editingLanguageId, prevEditingLanguageId, value]);
      return _react["default"].createElement(_form["default"].Group, {
        className: "lfr-ddm-form-field-validation"
      }, _react["default"].createElement(_Checkbox["default"], {
        disabled: readOnly,
        label: label,
        name: "enableValidation",
        onChange: function onChange(event, value) {
          return handleChange('enableValidation', value);
        },
        showAsSwitcher: true,
        spritemap: spritemap,
        value: enableValidation,
        visible: visible
      }), enableValidation && _react["default"].createElement(_react["default"].Fragment, null, _react["default"].createElement(_Select["default"], {
        disableEmptyOption: true,
        label: Liferay.Language.get('if-input'),
        name: "selectedValidation",
        onChange: function onChange(event, value) {
          return handleChange('selectedValidation', transformSelectedValidation(value));
        },
        options: validations,
        placeholder: Liferay.Language.get('choose-an-option'),
        readOnly: readOnly || localizationMode,
        spritemap: spritemap,
        value: [selectedValidation.name],
        visible: visible
      }), selectedValidation.parameterMessage && _react["default"].createElement(DynamicComponent, {
        dataType: dataType,
        label: Liferay.Language.get('the-value'),
        name: "".concat(name, "_parameter"),
        onChange: function onChange(event) {
          return handleChange('parameter', event.target.value);
        },
        placeholder: selectedValidation.parameterMessage,
        readOnly: readOnly,
        required: false,
        spritemap: spritemap,
        value: parameter,
        visible: visible
      }), _react["default"].createElement(_Text["default"], {
        label: Liferay.Language.get('show-error-message'),
        name: "".concat(name, "_errorMessage"),
        onChange: function onChange(event) {
          return handleChange('errorMessage', event.target.value);
        },
        placeholder: Liferay.Language.get('show-error-message'),
        readOnly: readOnly,
        required: false,
        spritemap: spritemap,
        value: errorMessage,
        visible: visible
      })));
    };

    var Main = function Main(_ref2) {
      var initialDataType = _ref2.dataType,
          defaultLanguageId = _ref2.defaultLanguageId,
          editingLanguageId = _ref2.editingLanguageId,
          label = _ref2.label,
          name = _ref2.name,
          _onChange = _ref2.onChange,
          readOnly = _ref2.readOnly,
          spritemap = _ref2.spritemap,
          validation = _ref2.validation,
          initialValidations = _ref2.validations,
          _ref2$value = _ref2.value,
          value = _ref2$value === void 0 ? {} : _ref2$value,
          visible = _ref2.visible;
      var data = (0, _transform.transformData)({
        defaultLanguageId: defaultLanguageId,
        editingLanguageId: editingLanguageId,
        initialDataType: initialDataType,
        initialValidations: initialValidations,
        validation: validation,
        value: value
      });
      return _react["default"].createElement(Validation, _extends({}, data, {
        defaultLanguageId: defaultLanguageId,
        editingLanguageId: editingLanguageId,
        label: label,
        name: name,
        onChange: function onChange(value) {
          return _onChange({}, value);
        },
        readOnly: readOnly,
        spritemap: spritemap,
        validation: validation,
        value: value,
        visible: visible
      }));
    };

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