var _excluded = ["className", "footnote", "icon", "id", "isDisabled", "isInvalid", "label", "max", "min", "name", "onBlur", "onChange", "placeholder", "prefix", "size", "step", "suffix", "value"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _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; }
import React from 'react';
import { isNil } from 'lodash';
import { Buttons } from './Buttons';
import { COMPONENT_SIZE } from '../Forms';
import { hasClass } from '../../helpers';
import { Input } from './Input';
import { useChangeHandlers } from './useChangeHandlers';
import { useFocus } from '../../hooks/useFocus';
import { useEarlyValidation } from './useEarlyValidation';
import { useValue } from './useValue';
import { StyledIcon } from './partials/StyledIcon';
import { StyledDivider } from './partials/StyledDivider';
import { StyledFootnote } from './partials/StyledFootnote';
import { StyledNumberInput } from './partials/StyledNumberInput';
import { StyledOuterWrapper } from './partials/StyledOuterWrapper';
import { StyledPrefix } from './partials/StyledPrefix';
import { StyledSuffix } from './partials/StyledSuffix';
import { useExternalId } from '../../hooks/useExternalId';
function formatValue(displayValue, prefix, suffix) {
  if (isNil(displayValue)) {
    return 'Not set';
  }
  if (prefix) {
    return "".concat(prefix, " ").concat(displayValue);
  }
  if (suffix) {
    return "".concat(displayValue, " ").concat(suffix);
  }
  return displayValue;
}
export var NumberInput = function NumberInput(_ref) {
  var className = _ref.className,
    footnote = _ref.footnote,
    icon = _ref.icon,
    externalId = _ref.id,
    _ref$isDisabled = _ref.isDisabled,
    isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled,
    isInvalid = _ref.isInvalid,
    label = _ref.label,
    max = _ref.max,
    min = _ref.min,
    name = _ref.name,
    onBlur = _ref.onBlur,
    onChange = _ref.onChange,
    _ref$placeholder = _ref.placeholder,
    placeholder = _ref$placeholder === void 0 ? '' : _ref$placeholder,
    prefix = _ref.prefix,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? COMPONENT_SIZE.FORMS : _ref$size,
    _ref$step = _ref.step,
    step = _ref$step === void 0 ? 1 : _ref$step,
    suffix = _ref.suffix,
    _ref$value = _ref.value,
    value = _ref$value === void 0 ? null : _ref$value,
    rest = _objectWithoutProperties(_ref, _excluded);
  var wrapperId = useExternalId('number-input');
  var inputId = useExternalId('number-input-input', externalId);
  var isNegativeAllowed = isNil(min) || min < 0;
  var _useValue = useValue(isNil(value) ? null : String(value)),
    committedValue = _useValue.committedValue,
    setCommittedValue = _useValue.setCommittedValue;
  var _useFocus = useFocus(onBlur),
    hasFocus = _useFocus.hasFocus,
    onLocalFocus = _useFocus.onLocalFocus,
    onLocalBlur = _useFocus.onLocalBlur;
  var _useEarlyValidation = useEarlyValidation(isNegativeAllowed),
    handleBeforeInput = _useEarlyValidation.handleBeforeInput,
    handlePaste = _useEarlyValidation.handlePaste;
  var _useChangeHandlers = useChangeHandlers(isNegativeAllowed, min, max, onChange, setCommittedValue),
    handleButtonClick = _useChangeHandlers.handleButtonClick,
    handleInputChange = _useChangeHandlers.handleInputChange;
  var isCommittedValueInvalid = committedValue && !Number.isFinite(parseFloat(committedValue));
  return /*#__PURE__*/React.createElement(StyledNumberInput, {
    "aria-label": label || 'Number input',
    className: className,
    id: wrapperId,
    role: "spinbutton",
    "aria-valuemin": min,
    "aria-valuemax": max,
    "aria-valuenow": Number(committedValue) || 0,
    "aria-valuetext": String(formatValue(committedValue, prefix, suffix))
  }, /*#__PURE__*/React.createElement(StyledOuterWrapper, {
    $hasFocus: hasFocus,
    $isDisabled: isDisabled,
    $isInvalid: isInvalid || isCommittedValueInvalid || hasClass(className, 'is-invalid'),
    $size: size
  }, icon && /*#__PURE__*/React.createElement(StyledIcon, null, icon), prefix && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(StyledPrefix, null, prefix), /*#__PURE__*/React.createElement(StyledDivider, {
    $size: size
  })), /*#__PURE__*/React.createElement(Input, _extends({
    "aria-labelledby": wrapperId,
    hasFocus: hasFocus,
    id: inputId,
    isDisabled: isDisabled,
    label: label,
    name: name,
    onBeforeInput: handleBeforeInput,
    onChange: handleInputChange,
    onPaste: handlePaste,
    onBlur: onLocalBlur,
    onFocus: onLocalFocus,
    placeholder: placeholder,
    size: size,
    step: step,
    value: committedValue
  }, rest)), suffix && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(StyledDivider, {
    $size: size
  }), /*#__PURE__*/React.createElement(StyledSuffix, null, suffix)), /*#__PURE__*/React.createElement(Buttons, {
    isDisabled: isDisabled,
    onClick: handleButtonClick,
    size: size,
    step: step,
    value: committedValue
  })), footnote && /*#__PURE__*/React.createElement(StyledFootnote, null, footnote));
};
NumberInput.displayName = 'NumberInput';