"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "NumberInputField", {
  enumerable: true,
  get: function () {
    return _NumberInputField.default;
  }
});
Object.defineProperty(exports, "NumberInputStepper", {
  enumerable: true,
  get: function () {
    return _NumberInputStepper.default;
  }
});
Object.defineProperty(exports, "NumberIncrementStepper", {
  enumerable: true,
  get: function () {
    return _NumberIncrementStepper.default;
  }
});
Object.defineProperty(exports, "NumberDecrementStepper", {
  enumerable: true,
  get: function () {
    return _NumberDecrementStepper.default;
  }
});
exports.default = exports.NumberInputContext = void 0;

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

var _v = require("@native-base/v3");

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

var _NumberInputField = _interopRequireDefault(require("./NumberInputField"));

var _NumberInputStepper = _interopRequireDefault(require("./NumberInputStepper"));

var _NumberIncrementStepper = _interopRequireDefault(require("./NumberIncrementStepper"));

var _NumberDecrementStepper = _interopRequireDefault(require("./NumberDecrementStepper"));

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

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

exports.NumberInputContext = NumberInputContext;

const NumberInput = ({
  children,
  ...props
}) => {
  // TODO: Needs refactoring after input is refactored.
  const {
    defaultValue,
    keepWithinRange,
    value,
    min,
    max,
    ...newProps
  } = (0, _v.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=index.js.map