"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.FormattedNumber = void 0;
var _react = _interopRequireWildcard(require("react"));
var _ = require("../../..");
var _utils = require("../../../common/utils");
var _adjustValue = _interopRequireDefault(require("./adjustValue"));
var _FormattedNumber = require("./FormattedNumber.styles");
var _utils2 = require("./utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 FormattedNumberInput = function FormattedNumberInput(props) {
  var autoFocus = props.autoFocus,
    autoComplete = props.autoComplete,
    _props$defaultValue = props.defaultValue,
    defaultValue = _props$defaultValue === void 0 ? null : _props$defaultValue,
    disabled = props.disabled,
    error = props.error,
    id = props.id,
    leftAddon = props.leftAddon,
    max = props.max,
    _props$min = props.min,
    min = _props$min === void 0 ? 0 : _props$min,
    name = props.name,
    noSteppers = props.noSteppers,
    _onBlur = props.onBlur,
    onChange = props.onChange,
    onClick = props.onClick,
    onFocus = props.onFocus,
    onKeyDown = props.onKeyDown,
    onKeyPress = props.onKeyPress,
    onKeyUp = props.onKeyUp,
    onStepDown = props.onStepDown,
    onStepUp = props.onStepUp,
    placeholderRaw = props.placeholder,
    rightAddon = props.rightAddon,
    required = props.required,
    size = props.size,
    _props$step = props.step,
    step = _props$step === void 0 ? 1 : _props$step,
    _props$inputMode = props.inputMode,
    inputMode = _props$inputMode === void 0 ? 'decimal' : _props$inputMode,
    success = props.success,
    controlledValue = props.value,
    _props$variant = props.variant,
    variant = _props$variant === void 0 ? 'normal' : _props$variant;
  var _useState = (0, _react.useState)(defaultValue),
    _useState2 = _slicedToArray(_useState, 2),
    internalValue = _useState2[0],
    setInternalValue = _useState2[1];
  var inputRef = (0, _react.useRef)(null);
  var value = controlledValue !== undefined ? controlledValue : internalValue;
  var showSteppers = noSteppers !== true && (0, _utils.isUndefined)(leftAddon) && (0, _utils.isUndefined)(rightAddon) && variant !== 'quiet';
  var placeholder = showSteppers ? undefined : placeholderRaw;
  var handleValueChange = function handleValueChange(newValue) {
    setInternalValue(newValue);
    onChange === null || onChange === void 0 || onChange(newValue);
  };
  var getUpdateValue = function getUpdateValue(increment) {
    return (0, _adjustValue.default)({
      originalValue: value,
      step: step,
      min: min,
      max: max,
      shouldIncrement: increment
    });
  };
  var onStepHandler = function onStepHandler(stepUp) {
    var _inputRef$current;
    var updatedValue = getUpdateValue(stepUp);
    handleValueChange(updatedValue);
    if (stepUp) {
      onStepUp === null || onStepUp === void 0 || onStepUp();
    } else {
      onStepDown === null || onStepDown === void 0 || onStepDown();
    }
    inputRef === null || inputRef === void 0 || (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.focus();
  };
  var onKeyDownHandler = function onKeyDownHandler(e) {
    var key = e.key;
    var upKey = 'ArrowUp';
    var downKey = 'ArrowDown';
    if (key === upKey || key === downKey) {
      e.preventDefault();
      onStepHandler(key === upKey);
    }
    onKeyDown === null || onKeyDown === void 0 || onKeyDown(e);
  };
  var className = props.className,
    extraInfo = props.extraInfo,
    hideLabel = props.hideLabel,
    label = props.label,
    labelTooltip = props.labelTooltip,
    labelTooltipInModal = props.labelTooltipInModal,
    labelTooltipPosition = props.labelTooltipPosition,
    width = props.width,
    visuallyEmphasiseRequired = props.visuallyEmphasiseRequired;
  return _react.default.createElement(_.FormField, {
    className: className,
    error: error,
    extraInfo: extraInfo,
    fieldId: id,
    hideLabel: hideLabel,
    label: label,
    labelTooltip: labelTooltip,
    labelTooltipInModal: labelTooltipInModal,
    labelTooltipPosition: labelTooltipPosition,
    required: visuallyEmphasiseRequired,
    width: width
  }, _react.default.createElement(_.Typography, {
    type: "secondary",
    color: function color(t) {
      return t.color.text;
    }
  }, _react.default.createElement(_FormattedNumber.FormattedNumberComponents.Wrapper, {
    container: true,
    item: true,
    grow: 1,
    alignItems: "center"
  }, _react.default.createElement("input", {
    type: "hidden",
    name: name,
    value: value === null ? '' : value
  }), _react.default.createElement(_FormattedNumber.FormattedNumberComponents.Input, {
    id: id,
    autoFocus: autoFocus,
    autoComplete: autoComplete,
    ariaInvalid: (0, _utils2.hasError)(error),
    disabled: disabled,
    onChange: handleValueChange,
    onClick: onClick,
    onBlur: function onBlur() {
      return _onBlur === null || _onBlur === void 0 ? void 0 : _onBlur(value);
    },
    onKeyPress: onKeyPress,
    onFocus: onFocus,
    onKeyDown: onKeyDownHandler,
    onKeyUp: onKeyUp,
    placeholder: placeholder,
    value: value,
    inputMode: inputMode,
    ref: inputRef,
    required: required,
    variant: variant,
    error: error,
    success: success,
    size: size,
    showSteppers: showSteppers,
    leftAddon: leftAddon,
    rightAddon: rightAddon
  }), showSteppers && _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_FormattedNumber.FormattedNumberComponents.Stepper, {
    onClick: function onClick() {
      return onStepHandler(false);
    },
    size: size,
    disabled: disabled
  }, _react.default.createElement(_.VisuallyHidden, null, "\u2212", step), _react.default.createElement(_.OldIcon.Minus, {
    size: 3
  })), _react.default.createElement(_FormattedNumber.FormattedNumberComponents.Stepper, {
    onClick: function onClick() {
      return onStepHandler(true);
    },
    size: size,
    disabled: disabled
  }, _react.default.createElement(_.VisuallyHidden, null, "+", step), _react.default.createElement(_.OldIcon.Plus, {
    size: 3
  }))), leftAddon && _react.default.createElement(_FormattedNumber.FormattedNumberComponents.AddonBox, {
    container: true,
    justifyContent: "center",
    alignItems: "center",
    position: "left"
  }, leftAddon), rightAddon && _react.default.createElement(_FormattedNumber.FormattedNumberComponents.AddonBox, {
    container: true,
    justifyContent: "center",
    alignItems: "center",
    position: "right",
    variant: variant
  }, rightAddon))));
};
FormattedNumberInput.components = _FormattedNumber.FormattedNumberComponents;
var FormattedNumber = exports.FormattedNumber = FormattedNumberInput;