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; }
import React, { useImperativeHandle, useLayoutEffect, useRef, useState } from 'react';
import { useIntl } from 'react-intl';
import { Input } from '../../../../common/NormalizedElements/NormalizedInput';
import { calculateCaretPosition, formatNumber, handleThousandSeparatorRemoval, parseInputValue } from './utils';
var FormattedInput = React.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 = 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 = useRef();
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    caret = _useState4[0],
    setCaret = _useState4[1];
  var ref = useRef(null);
  var intl = useIntl();
  useImperativeHandle(forwardedRef, function () {
    return ref.current;
  }, []);
  useLayoutEffect(function () {
    var valueChangedExternally = value !== lastOnChangeValue.current;
    if (valueChangedExternally) {
      lastOnChangeValue.current = undefined;
      var invalidNumber = Number.isNaN(value);
      if (value === null || invalidNumber) {
        setFormattedValue('');
      } else {
        setFormattedValue(formatNumber(value, intl));
      }
    }
  }, [value, lastOnChangeValue, formattedValue, intl]);
  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 = parseInputValue(value, formattedValue, e.target.value, intl),
      newValue = _parseInputValue.value,
      newFormattedValue = _parseInputValue.formattedValue;
    var newCaretPosition = 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) {
    handleThousandSeparatorRemoval(e, intl);
    onKeyDown === null || onKeyDown === void 0 || onKeyDown(e);
  };
  return React.createElement(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
  });
});
export default FormattedInput;