Liferay.Loader.define("dynamic-data-mapping-form-field-type@5.0.95/Numeric/Numeric.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/form', 'frontend-js-react-web', 'frontend-js-react-web$react', 'dynamic-data-mapping-form-field-type$text-mask-addons/dist/createNumberMask', 'dynamic-data-mapping-form-field-type$vanilla-text-mask', '../FieldBase/ReactFieldBase.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 = exports.Main = void 0;

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

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

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

    var _createNumberMask = _interopRequireDefault(require("dynamic-data-mapping-form-field-type$text-mask-addons/dist/createNumberMask"));

    var _vanillaTextMask = _interopRequireDefault(require("dynamic-data-mapping-form-field-type$vanilla-text-mask"));

    var _ReactFieldBase = require("../FieldBase/ReactFieldBase.es");

    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
    }

    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 _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 _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 _objectWithoutProperties(source, excluded) {
      if (source == null) return {};var target = _objectWithoutPropertiesLoose(source, excluded);var key, i;if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);for (i = 0; i < sourceSymbolKeys.length; i++) {
          key = sourceSymbolKeys[i];if (excluded.indexOf(key) >= 0) continue;if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;target[key] = source[key];
        }
      }return target;
    }

    function _objectWithoutPropertiesLoose(source, excluded) {
      if (source == null) return {};var target = {};var sourceKeys = Object.keys(source);var key, i;for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];if (excluded.indexOf(key) >= 0) continue;target[key] = source[key];
      }return target;
    }

    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 getMaskConfig = function getMaskConfig(dataType, symbols) {
      var config = {
        allowLeadingZeroes: true,
        includeThousandsSeparator: false,
        prefix: ''
      };

      if (dataType === 'double') {
        config = _objectSpread(_objectSpread({}, config), {}, {
          allowDecimal: true,
          decimalLimit: null,
          decimalSymbol: symbols.decimalSymbol
        });
      }

      return config;
    };

    var getValue = function getValue(dataType, symbols, value) {
      var newValue = value;
      var decimalSymbol = symbols.decimalSymbol;

      if (newValue && !newValue.includes('.') && symbols.decimalSymbol != ',') {
        decimalSymbol = ',';
      }

      if (dataType === 'integer' && newValue && newValue.includes(decimalSymbol)) {
        newValue = String(Math.round(newValue.replace(decimalSymbol, '.')));
      }

      return newValue;
    };

    var Numeric = function Numeric(_ref) {
      var _ref$dataType = _ref.dataType,
          dataType = _ref$dataType === void 0 ? 'integer' : _ref$dataType,
          defaultLanguageId = _ref.defaultLanguageId,
          disabled = _ref.disabled,
          editingLanguageId = _ref.editingLanguageId,
          localizable = _ref.localizable,
          localizedValue = _ref.localizedValue,
          _onChange = _ref.onChange,
          _ref$symbols = _ref.symbols,
          symbols = _ref$symbols === void 0 ? {
        decimalSymbol: '.',
        thousandsSeparator: ','
      } : _ref$symbols,
          value = _ref.value,
          otherProps = _objectWithoutProperties(_ref, ["dataType", "defaultLanguageId", "disabled", "editingLanguageId", "localizable", "localizedValue", "onChange", "symbols", "value"]);

      var _useState = (0, _react.useState)(value),
          _useState2 = _slicedToArray(_useState, 2),
          currentValue = _useState2[0],
          setCurrentValue = _useState2[1];

      var inputRef = (0, _react.useRef)(null);
      var prevEditingLanguageId = (0, _frontendJsReactWeb.usePrevious)(editingLanguageId);
      (0, _react.useEffect)(function () {
        if (prevEditingLanguageId !== editingLanguageId && localizable) {
          var newValue = localizedValue[editingLanguageId] !== undefined ? localizedValue[editingLanguageId] : localizedValue[defaultLanguageId];
          newValue = getValue(dataType, symbols, newValue);
          setCurrentValue(newValue);
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [defaultLanguageId, editingLanguageId, localizable, localizedValue, prevEditingLanguageId, setCurrentValue]);
      (0, _react.useEffect)(function () {
        var maskInstance = null;

        if (inputRef.current) {
          var newValue = getValue(dataType, symbols, value);
          var mask = (0, _createNumberMask.default)(getMaskConfig(dataType, symbols));
          maskInstance = (0, _vanillaTextMask.default)({
            inputElement: inputRef.current,
            mask: mask
          });

          if (newValue !== inputRef.current.value) {
            setCurrentValue(newValue);
          }
        }

        return function () {
          if (maskInstance) {
            maskInstance.destroy();
          }
        }; // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [dataType, inputRef, setCurrentValue, value]);
      return (/*#__PURE__*/_react.default.createElement(_form.ClayInput, _extends({}, otherProps, {
          disabled: disabled,
          onChange: function onChange(event) {
            var newValue = event.target.value;

            if (dataType === 'integer' && newValue.substr(-1) === symbols.decimalSymbol) {
              return;
            }

            setCurrentValue(newValue);

            _onChange(event);
          },
          ref: inputRef,
          type: "text",
          value: currentValue
        }))
      );
    };

    var Main = function Main(_ref2) {
      var dataType = _ref2.dataType,
          defaultLanguageId = _ref2.defaultLanguageId,
          editingLanguageId = _ref2.editingLanguageId,
          id = _ref2.id,
          localizable = _ref2.localizable,
          _ref2$localizedValue = _ref2.localizedValue,
          localizedValue = _ref2$localizedValue === void 0 ? {} : _ref2$localizedValue,
          name = _ref2.name,
          onBlur = _ref2.onBlur,
          onChange = _ref2.onChange,
          onFocus = _ref2.onFocus,
          placeholder = _ref2.placeholder,
          _ref2$predefinedValue = _ref2.predefinedValue,
          predefinedValue = _ref2$predefinedValue === void 0 ? '' : _ref2$predefinedValue,
          readOnly = _ref2.readOnly,
          symbols = _ref2.symbols,
          value = _ref2.value,
          otherProps = _objectWithoutProperties(_ref2, ["dataType", "defaultLanguageId", "editingLanguageId", "id", "localizable", "localizedValue", "name", "onBlur", "onChange", "onFocus", "placeholder", "predefinedValue", "readOnly", "symbols", "value"]);

      return (/*#__PURE__*/_react.default.createElement(_ReactFieldBase.FieldBase, _extends({}, otherProps, {
          id: id,
          localizedValue: localizedValue,
          name: name,
          readOnly: readOnly
        }), /*#__PURE__*/_react.default.createElement(Numeric, {
          dataType: dataType,
          defaultLanguageId: defaultLanguageId,
          disabled: readOnly,
          editingLanguageId: editingLanguageId,
          id: id,
          localizable: localizable,
          localizedValue: localizedValue,
          name: name,
          onBlur: onBlur,
          onChange: onChange,
          onFocus: onFocus,
          placeholder: placeholder,
          symbols: symbols,
          value: value ? value : predefinedValue
        }))
      );
    };

    exports.Main = Main;
    Main.displayName = 'Numeric';
    var _default = Main;
    exports.default = _default;
    //# sourceMappingURL=Numeric.es.js.map
  }
});
//# sourceMappingURL=Numeric.es.js.map