"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.NumberInputContext = void 0;

var _react = _interopRequireDefault(require("react"));

var _theme = require("../../../theme");

var _FormControl = require("../FormControl");

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

const NumberInputContext = /*#__PURE__*/_react.default.createContext({});

exports.NumberInputContext = NumberInputContext;

const NumberInput = ({
  children,
  ...props
}) => {
  const {
    defaultValue,
    keepWithinRange,
    value,
    min,
    max,
    ...newProps
  } = (0, _theme.usePropsConfig)('NumberInput', props);

  const formControlContext = _react.default.useContext(_FormControl.FormControlContext);

  const [numberInputValue, setNumberInputValue] = _react.default.useState(parseInt(value || defaultValue, 10));

  const handleChange = newValue => {
    const temp = newValue;
    setNumberInputValue(temp);

    if (keepWithinRange) {
      if (newValue < min) setNumberInputValue(min);else if (newValue > max) setNumberInputValue(max);
    }
  };

  const handleChangeWithoutCheck = newValue => {
    const temp = newValue;
    setNumberInputValue(temp);
  };

  let numberInputStepper = null;

  const separatingNumberInputStepper = allChildren => {
    return _react.default.Children.map(allChildren, child => {
      if (child.type.name === 'NumberInputStepper') {
        numberInputStepper = child;
        return null;
      } else {
        return child;
      }
    });
  };

  const updatedChildren = separatingNumberInputStepper(children);
  return /*#__PURE__*/_react.default.createElement(NumberInputContext.Provider, {
    value: { ...formControlContext,
      ...newProps,
      min,
      max,
      handleChange,
      handleChangeWithoutCheck,
      numberInputValue,
      numberInputStepper
    }
  }, updatedChildren);
};

var _default = NumberInput;
exports.default = _default;
//# sourceMappingURL=NumberInput.js.map