'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var index = require('../../utils/index.js');
var IconMinus = require('../_Icon/Icons/IconMinus.js');
var IconPlus = require('../_Icon/Icons/IconPlus.js');
var NumberInput_tokens = require('./NumberInput.tokens.js');
var NumberInput_styles = require('./NumberInput.styles.js');
var base = require('./variations/_view/base.js');
var base$1 = require('./variations/_size/base.js');
var base$2 = require('./variations/_shape/base.js');
var base$3 = require('./variations/_background-type/base.js');
var base$4 = require('./variations/_segmentation/base.js');
var base$5 = require('./variations/_disabled/base.js');
var index$1 = require('./utils/index.js');
var isNumber = require('../../utils/isNumber.js');
var ActionButton = require('./ui/ActionButton/ActionButton.js');
var Input = require('./ui/Input/Input.js');
var getSizeValueFromProp = require('../../utils/getSizeValueFromProp.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

var _excluded = ["className", "style", "width", "value", "min", "max", "step", "precision", "isLoading", "loader", "size", "view", "clear", "shape", "segmentation", "inputBackgroundType", "disabled", "textBefore", "textAfter", "customIncrementButton", "incrementIcon", "customDecrementButton", "decrementIcon", "isManualInput", "displayWithoutValue", "limitBehavior", "onChange", "onIncrement", "onDecrement"];
var numberInputRoot = function numberInputRoot(Root) {
  return /*#__PURE__*/React.forwardRef(function (_ref, ref) {
    var className = _ref.className,
      style = _ref.style,
      width = _ref.width,
      outerValue = _ref.value,
      min = _ref.min,
      max = _ref.max,
      _ref$step = _ref.step,
      step = _ref$step === void 0 ? 1 : _ref$step,
      _ref$precision = _ref.precision,
      precision = _ref$precision === void 0 ? 2 : _ref$precision,
      isLoading = _ref.isLoading,
      loader = _ref.loader,
      size = _ref.size,
      view = _ref.view,
      clear = _ref.clear,
      shape = _ref.shape,
      segmentation = _ref.segmentation,
      _ref$inputBackgroundT = _ref.inputBackgroundType,
      inputBackgroundType = _ref$inputBackgroundT === void 0 ? 'fill' : _ref$inputBackgroundT,
      disabled = _ref.disabled,
      textBefore = _ref.textBefore,
      textAfter = _ref.textAfter,
      customIncrementButton = _ref.customIncrementButton,
      incrementIcon = _ref.incrementIcon,
      customDecrementButton = _ref.customDecrementButton,
      decrementIcon = _ref.decrementIcon,
      isManualInput = _ref.isManualInput,
      _ref$displayWithoutVa = _ref.displayWithoutValue,
      displayWithoutValue = _ref$displayWithoutVa === void 0 ? 'input' : _ref$displayWithoutVa,
      _ref$limitBehavior = _ref.limitBehavior,
      limitBehavior = _ref$limitBehavior === void 0 ? 'disabled' : _ref$limitBehavior,
      onChange = _ref.onChange,
      onIncrement = _ref.onIncrement,
      onDecrement = _ref.onDecrement,
      rest = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
    if (outerValue && !isNumber.isNumber(outerValue) && outerValue !== undefined && outerValue !== '-' && outerValue !== '.') {
      console.error('`value` должен быть undefined, числом или строкой с числом');
    }
    var _useState = React.useState(false),
      _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
      isInputFocused = _useState2[0],
      setIsInputFocused = _useState2[1];
    var _useState3 = React.useState(false),
      _useState4 = _rollupPluginBabelHelpers.slicedToArray(_useState3, 2),
      isAnimationRun = _useState4[0],
      setIsAnimationRun = _useState4[1];
    var _useState5 = React.useState(''),
      _useState6 = _rollupPluginBabelHelpers.slicedToArray(_useState5, 2),
      innerValue = _useState6[0],
      setInnerValue = _useState6[1];
    var _useState7 = React.useState(false),
      _useState8 = _rollupPluginBabelHelpers.slicedToArray(_useState7, 2),
      shouldFocusInput = _useState8[0],
      setShouldFocusInput = _useState8[1];
    var value = outerValue !== null && outerValue !== void 0 ? outerValue : innerValue;
    var innerWidth = width ? getSizeValueFromProp.getSizeValueFromProp(width) : '100%';
    var actionIconSize = size === 'xs' ? 'xs' : 's';
    var solidViewClass = segmentation === 'solid' ? NumberInput_tokens.classes.solidView : undefined;
    var segmentedViewClass = segmentation === 'segmented' ? NumberInput_tokens.classes.segmentedView : undefined;
    var clearViewClass = clear ? NumberInput_tokens.classes.clearView : undefined;
    var disabledClass = disabled ? NumberInput_tokens.classes.disabled : undefined;
    var loadingClass = isLoading ? NumberInput_tokens.classes.loading : undefined;
    var focusedClass = isInputFocused ? NumberInput_tokens.classes.focused : undefined;
    var isMinValue = function isMinValue(currentValue) {
      return min !== undefined && currentValue <= min;
    };
    var isMaxValue = function isMaxValue(currentValue) {
      return max !== undefined && currentValue >= max;
    };
    var isValueEmpty = (value === '' || value === undefined) && !isManualInput;
    var onlyShowIncrement = isValueEmpty && displayWithoutValue === 'increment';
    var onlyShowDecrement = isValueEmpty && displayWithoutValue === 'decrement';
    var decrementAtLimit = isNumber.isNumber(value) && value !== '' && isMinValue(Number(value)) && !isAnimationRun;
    var incrementAtLimit = isNumber.isNumber(value) && value !== '' && isMaxValue(Number(value)) && !isAnimationRun;
    var decrementButtonDisabled = (decrementAtLimit && limitBehavior !== 'hidden' || isLoading) && !disabled ? NumberInput_tokens.classes.actionButtonDecrementDisabled : undefined;
    var incrementButtonDisabled = (incrementAtLimit && limitBehavior !== 'hidden' || isLoading) && !disabled ? NumberInput_tokens.classes.actionButtonIncrementDisabled : undefined;
    var decrementHiddenClass = decrementAtLimit && limitBehavior === 'hidden' ? NumberInput_tokens.classes.decrementHidden : undefined;
    var incrementHiddenClass = incrementAtLimit && limitBehavior === 'hidden' ? NumberInput_tokens.classes.incrementHidden : undefined;
    var onlyIncrementShownClass = onlyShowIncrement ? NumberInput_tokens.classes.onlyIncrementShown : undefined;
    var onlyDecrementShownClass = onlyShowDecrement ? NumberInput_tokens.classes.onlyDecrementShown : undefined;
    var handleDecrement = function handleDecrement() {
      if (isLoading || disabled || isAnimationRun) {
        return;
      }
      if (onlyShowDecrement) {
        setShouldFocusInput(true);
      }
      var processedValue = isNumber.isNumber(value) && value !== '' ? Number(value) : max !== null && max !== void 0 ? max : 0;
      var preciseDiff = index$1.getPreciseValue(processedValue - step, precision);
      var diffValue = Number(preciseDiff);
      var resValue = min !== undefined && diffValue <= min ? min : diffValue;
      setInnerValue(resValue);
      if (onDecrement) {
        onDecrement(resValue);
      }
      if (onChange) {
        onChange(null, resValue);
      }
    };
    var handleIncrement = function handleIncrement() {
      if (isLoading || disabled || isAnimationRun) {
        return;
      }
      if (onlyShowIncrement) {
        setShouldFocusInput(true);
      }
      var processedValue = isNumber.isNumber(value) && value !== '' ? Number(value) : min !== null && min !== void 0 ? min : 0;
      var preciseDiff = index$1.getPreciseValue(processedValue + step, precision);
      var diffValue = Number(preciseDiff);
      var resValue = max !== undefined && diffValue >= max ? max : diffValue;
      setInnerValue(resValue);
      if (onIncrement) {
        onIncrement(resValue);
      }
      if (onChange) {
        onChange(null, resValue);
      }
    };
    var decrementButton = customDecrementButton || /*#__PURE__*/React__default.default.createElement(ActionButton.ActionButton, {
      className: index.cx(NumberInput_tokens.classes.actionButtonDecrement, decrementButtonDisabled, solidViewClass, segmentedViewClass),
      icon: decrementIcon || /*#__PURE__*/React__default.default.createElement(IconMinus.IconMinus, {
        color: "inherit",
        size: actionIconSize
      }),
      onClick: handleDecrement,
      tabIndex: onlyShowDecrement ? 0 : -1
    });
    var incrementButton = customIncrementButton || /*#__PURE__*/React__default.default.createElement(ActionButton.ActionButton, {
      className: index.cx(NumberInput_tokens.classes.actionButtonIncrement, incrementButtonDisabled, solidViewClass, segmentedViewClass),
      icon: incrementIcon || /*#__PURE__*/React__default.default.createElement(IconPlus.IconPlus, {
        color: "inherit",
        size: actionIconSize
      }),
      onClick: handleIncrement,
      tabIndex: onlyShowIncrement ? 0 : -1
    });
    return /*#__PURE__*/React__default.default.createElement(Root, _rollupPluginBabelHelpers.extends({
      view: view,
      size: size,
      disabled: disabled,
      clear: clear,
      shape: shape,
      segmentation: segmentation,
      isLoading: isLoading
    }, segmentation === 'solid' ? {
      inputBackgroundType: 'clear'
    } : {
      inputBackgroundType: inputBackgroundType
    }, {
      className: index.cx(className, solidViewClass, segmentedViewClass, clearViewClass, disabledClass, loadingClass, focusedClass, decrementHiddenClass, incrementHiddenClass, onlyIncrementShownClass, onlyDecrementShownClass),
      style: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, style), {}, _rollupPluginBabelHelpers.defineProperty({}, NumberInput_tokens.privateTokens.inputWidth, innerWidth))
    }), !onlyShowIncrement && decrementButton, !onlyShowIncrement && !onlyShowDecrement && /*#__PURE__*/React__default.default.createElement(Input.NumberInput, _rollupPluginBabelHelpers.extends({
      ref: ref,
      segmentation: segmentation,
      value: value,
      precision: precision,
      min: min,
      max: max,
      isManualInput: isManualInput,
      textBefore: textBefore,
      textAfter: textAfter,
      isLoading: isLoading,
      loader: loader,
      disabled: disabled,
      isInputFocused: isInputFocused,
      setIsInputFocused: setIsInputFocused,
      setIsAnimationRun: setIsAnimationRun,
      setInnerValue: setInnerValue,
      onChange: onChange,
      onDecrementKey: handleDecrement,
      onIncrementKey: handleIncrement,
      shouldFocusInput: shouldFocusInput,
      onFocusHandled: function onFocusHandled() {
        return setShouldFocusInput(false);
      }
    }, rest)), !onlyShowDecrement && incrementButton);
  });
};
var numberInputConfig = {
  name: 'NumberInput',
  tag: 'div',
  layout: numberInputRoot,
  base: NumberInput_styles.base,
  variations: {
    view: {
      css: base.base
    },
    size: {
      css: base$1.base
    },
    shape: {
      css: base$2.base
    },
    inputBackgroundType: {
      css: base$3.base
    },
    segmentation: {
      css: base$4.base
    },
    disabled: {
      css: base$5.base,
      attrs: true
    }
  },
  defaults: {
    view: 'default',
    size: 'l',
    shape: 'cornered',
    segmentation: 'default'
  }
};

exports.numberInputConfig = numberInputConfig;
exports.numberInputRoot = numberInputRoot;
