"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _NormalizedInput = require("../../../../common/NormalizedElements/NormalizedInput");
var _utils = require("./utils");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var FormattedInput = _react.default.forwardRef(function (_ref, forwardedRef) {
  var _value$toString;
  var id = _ref.id,
    autoFocus = _ref.autoFocus,
    autoComplete = _ref.autoComplete,
    className = _ref.className,
    disabled = _ref.disabled,
    ariaInvalid = _ref.ariaInvalid,
    onChange = _ref.onChange,
    onClick = _ref.onClick,
    onBlur = _ref.onBlur,
    onKeyPress = _ref.onKeyPress,
    onFocus = _ref.onFocus,
    onKeyDown = _ref.onKeyDown,
    onKeyUp = _ref.onKeyUp,
    placeholder = _ref.placeholder,
    value = _ref.value,
    _ref$inputMode = _ref.inputMode,
    inputMode = _ref$inputMode === void 0 ? 'decimal' : _ref$inputMode,
    required = _ref.required;
  var _useState = (0, _react.useState)((_value$toString = value === null || value === void 0 ? void 0 : value.toString()) !== null && _value$toString !== void 0 ? _value$toString : ''),
    _useState2 = _slicedToArray(_useState, 2),
    formattedValue = _useState2[0],
    setFormattedValue = _useState2[1];
  var lastOnChangeValue = (0, _react.useRef)();
  var _useState3 = (0, _react.useState)(),
    _useState4 = _slicedToArray(_useState3, 2),
    caret = _useState4[0],
    setCaret = _useState4[1];
  var ref = (0, _react.useRef)(null);
  var intl = (0, _reactIntl.useIntl)();
  (0, _react.useImperativeHandle)(forwardedRef, function () {
    return ref.current;
  }, []);
  (0, _react.useLayoutEffect)(function () {
    var valueChangedExternally = value !== lastOnChangeValue.current;
    if (valueChangedExternally) {
      lastOnChangeValue.current = undefined;
      var invalidNumber = Number.isNaN(value);
      if (value === null || invalidNumber) {
        setFormattedValue('');
      } else {
        setFormattedValue((0, _utils.formatNumber)(value, intl));
      }
    }
  }, [value, lastOnChangeValue, formattedValue, intl]);
  (0, _react.useLayoutEffect)(function () {
    if (ref.current && caret !== undefined) {
      ref.current.selectionStart = caret.position;
      ref.current.selectionEnd = caret.position;
    }
  }, [caret, ref]);
  var onChangeHandler = function onChangeHandler(e) {
    var _parseInputValue = (0, _utils.parseInputValue)(value, formattedValue, e.target.value, intl),
      newValue = _parseInputValue.value,
      newFormattedValue = _parseInputValue.formattedValue;
    var newCaretPosition = (0, _utils.calculateCaretPosition)({
      value: e.target.value,
      formattedValue: newFormattedValue
    }, formattedValue, e.target.selectionStart, intl);
    onChange(newValue);
    setFormattedValue(newFormattedValue);
    setCaret({
      position: newCaretPosition
    });
    lastOnChangeValue.current = newValue;
  };
  var handleKeyDown = function handleKeyDown(e) {
    (0, _utils.handleThousandSeparatorRemoval)(e, intl);
    onKeyDown === null || onKeyDown === void 0 || onKeyDown(e);
  };
  return _react.default.createElement(_NormalizedInput.Input, {
    id: id,
    autoFocus: autoFocus,
    autoComplete: autoComplete,
    className: className,
    disabled: disabled,
    onBlur: onBlur,
    onChange: onChangeHandler,
    onClick: onClick,
    onFocus: onFocus,
    onKeyDown: handleKeyDown,
    onKeyPress: onKeyPress,
    onKeyUp: onKeyUp,
    placeholder: placeholder,
    ref: ref,
    value: formattedValue,
    inputMode: inputMode,
    "aria-invalid": ariaInvalid,
    type: "text",
    required: required
  });
});
var _default = exports.default = FormattedInput;