"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.NumberDecrementStepper = exports.NumberIncrementStepper = exports.NumberInputStepper = exports.NumberInputField = exports.NumberInput = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _MenuDownIcon = _interopRequireDefault(require("mdi-react/MenuDownIcon"));

var _MenuUpIcon = _interopRequireDefault(require("mdi-react/MenuUpIcon"));

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

var _Flex = require("../Flex");

var _Form = require("../Form");

var _useForkRef = require("../hooks/useForkRef");

var _useNumberInput = require("../hooks/useNumberInput");

var _Input = require("../Input");

var _PseudoBox = require("../PseudoBox");

var _wrapEvent = require("../utils/wrapEvent");

var _styles = _interopRequireDefault(require("./styles"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __jsx = _react["default"].createElement;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var NumberInputContext = /*#__PURE__*/(0, _react.createContext)({});

var useNumberInputContext = function useNumberInputContext() {
  var context = (0, _react.useContext)(NumberInputContext);

  if (context == null) {
    throw new Error('This component must be used within the `NumberInput` ');
  }

  return context;
};

var NumberInput = /*#__PURE__*/(0, _react.forwardRef)(function (_ref2, ref) {
  var value = _ref2.value,
      name = _ref2.name,
      onChange = _ref2.onChange,
      defaultValue = _ref2.defaultValue,
      focusInputOnChange = _ref2.focusInputOnChange,
      clampValueOnBlur = _ref2.clampValueOnBlur,
      keepWithinRange = _ref2.keepWithinRange,
      min = _ref2.min,
      max = _ref2.max,
      step = _ref2.step,
      precision = _ref2.precision,
      getAriaValueText = _ref2.getAriaValueText,
      isReadOnly = _ref2.isReadOnly,
      isInvalid = _ref2.isInvalid,
      isDisabled = _ref2.isDisabled,
      _ref2$isFullWidth = _ref2.isFullWidth,
      isFullWidth = _ref2$isFullWidth === void 0 ? true : _ref2$isFullWidth,
      size = _ref2.size,
      children = _ref2.children,
      rest = (0, _objectWithoutProperties2["default"])(_ref2, ["value", "name", "onChange", "defaultValue", "focusInputOnChange", "clampValueOnBlur", "keepWithinRange", "min", "max", "step", "precision", "getAriaValueText", "isReadOnly", "isInvalid", "isDisabled", "isFullWidth", "size", "children"]);
  // Form Support
  var formField = (0, _Form.useFormField)({
    name: name,
    onChange: onChange
  });

  var handleInputChange = function handleInputChange(e) {
    if (onChange) {
      onChange(e);
    }

    if (formField.onChange) {
      formField.onChange(null, e);
    }
  };

  var context = (0, _useNumberInput.useNumberInput)({
    value: value,
    name: name,
    onChange: handleInputChange,
    defaultValue: defaultValue || formField.value,
    focusInputOnChange: focusInputOnChange,
    clampValueOnBlur: clampValueOnBlur,
    keepWithinRange: keepWithinRange,
    min: min,
    max: max,
    step: step,
    precision: precision,
    getAriaValueText: getAriaValueText,
    isReadOnly: isReadOnly,
    isInvalid: isInvalid,
    isDisabled: isDisabled
  });

  var _children = children || __jsx(_react["default"].Fragment, null, __jsx(NumberInputField, null), __jsx(NumberInputStepper, null, __jsx(NumberIncrementStepper, null), __jsx(NumberDecrementStepper, null)));

  return __jsx(NumberInputContext.Provider, {
    value: _objectSpread(_objectSpread({}, context), {}, {
      size: size
    })
  }, __jsx(_Flex.Flex, (0, _extends2["default"])({
    ref: ref,
    align: "stretch",
    w: isFullWidth ? 'full' : null,
    pos: "relative"
  }, rest, {
    id: null
  }), _children));
});
exports.NumberInput = NumberInput;
var NumberInputField = /*#__PURE__*/(0, _react.forwardRef)(function (_ref3, ref) {
  var onBlur = _ref3.onBlur,
      onFocus = _ref3.onFocus,
      onKeyDown = _ref3.onKeyDown,
      onChange = _ref3.onChange,
      props = (0, _objectWithoutProperties2["default"])(_ref3, ["onBlur", "onFocus", "onKeyDown", "onChange"]);

  var _useNumberInputContex = useNumberInputContext(),
      size = _useNumberInputContex.size,
      _useNumberInputContex2 = _useNumberInputContex.input,
      _ref = _useNumberInputContex2.ref,
      _onBlur = _useNumberInputContex2.onBlur,
      _onFocus = _useNumberInputContex2.onFocus,
      _onChange = _useNumberInputContex2.onChange,
      _onKeyDown = _useNumberInputContex2.onKeyDown,
      isDisabled = _useNumberInputContex2.disabled,
      isReadOnly = _useNumberInputContex2.readOnly,
      otherInputProps = (0, _objectWithoutProperties2["default"])(_useNumberInputContex2, ["ref", "onBlur", "onFocus", "onChange", "onKeyDown", "disabled", "readOnly"]),
      name = _useNumberInputContex.name;

  var inputRef = (0, _useForkRef.useForkRef)(_ref, ref);
  var handleBlur = (0, _wrapEvent.wrapEvent)(onBlur, _onBlur);
  var handleFocus = (0, _wrapEvent.wrapEvent)(onFocus, _onFocus);
  var handleKeyDown = (0, _wrapEvent.wrapEvent)(onKeyDown, _onKeyDown);
  var handleChange = (0, _wrapEvent.wrapEvent)(onChange, _onChange);
  return __jsx(_Input.Input, (0, _extends2["default"])({
    ref: inputRef,
    isReadOnly: isReadOnly,
    isDisabled: isDisabled,
    onBlur: handleBlur,
    onFocus: handleFocus,
    onKeyDown: handleKeyDown,
    onChange: handleChange,
    size: size,
    name: name
  }, otherInputProps, props, {
    id: name
  }));
});
exports.NumberInputField = NumberInputField;
var NumberInputStepper = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _useNumberInputStyle = (0, _styles["default"])({}),
      numberInputStepperStyleProps = _useNumberInputStyle.inputStepper;

  return __jsx(_Flex.Flex, (0, _extends2["default"])({
    ref: ref,
    "aria-hidden": true
  }, numberInputStepperStyleProps, props));
});
exports.NumberInputStepper = NumberInputStepper;
var StepperButton = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _useNumberInputContex3 = useNumberInputContext(),
      isDisabled = _useNumberInputContex3.isDisabled,
      size = _useNumberInputContex3.size;

  var _useNumberInputStyle2 = (0, _styles["default"])({
    size: size,
    isDisabled: isDisabled
  }),
      stepperButtonStyleProps = _useNumberInputStyle2.root;

  return __jsx(_PseudoBox.PseudoBox, (0, _extends2["default"])({
    ref: ref,
    role: "button",
    "aria-disabled": isDisabled
  }, stepperButtonStyleProps, props));
});
var NumberIncrementStepper = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _useNumberInputContex4 = useNumberInputContext(),
      incrementStepper = _useNumberInputContex4.incrementStepper;

  var children = props.children || __jsx(_MenuUpIcon["default"], {
    size: "0.75em"
  });

  return __jsx(StepperButton, (0, _extends2["default"])({
    ref: ref
  }, props, incrementStepper), children);
});
exports.NumberIncrementStepper = NumberIncrementStepper;
var NumberDecrementStepper = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _useNumberInputContex5 = useNumberInputContext(),
      decrementStepper = _useNumberInputContex5.decrementStepper;

  var children = props.children || __jsx(_MenuDownIcon["default"], {
    size: "0.75em"
  });

  return __jsx(StepperButton, (0, _extends2["default"])({
    ref: ref
  }, props, decrementStepper), children);
});
exports.NumberDecrementStepper = NumberDecrementStepper;
//# sourceMappingURL=NumberInput.js.map