"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get numberInputConfig () {
        return numberInputConfig;
    },
    get numberInputRoot () {
        return numberInputRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _utils = require("../../utils");
const _Icon = require("../_Icon");
const _ui = require("./ui");
const _NumberInputtokens = require("./NumberInput.tokens");
const _NumberInputstyles = require("./NumberInput.styles");
const _base = require("./variations/_view/base");
const _base1 = require("./variations/_size/base");
const _base2 = require("./variations/_shape/base");
const _base3 = require("./variations/_background-type/base");
const _base4 = require("./variations/_segmentation/base");
const _base5 = require("./variations/_disabled/base");
const _utils1 = require("./utils");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(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 = {
        __proto__: null
    };
    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;
}
const numberInputRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ className, style, width, value: outerValue, min, max, step = 1, precision = 2, isLoading, loader, size, view, clear, shape, segmentation, inputBackgroundType = 'fill', disabled, textBefore, textAfter, customIncrementButton, incrementIcon, customDecrementButton, decrementIcon, isManualInput, displayWithoutValue = 'input', limitBehavior = 'disabled', onChange, onIncrement, onDecrement, ...rest }, ref)=>{
        if (outerValue && !(0, _utils.isNumber)(outerValue) && outerValue !== undefined && outerValue !== '-' && outerValue !== '.') {
            console.error('`value` должен быть undefined, числом или строкой с числом');
        }
        const [isInputFocused, setIsInputFocused] = (0, _react.useState)(false);
        const [isAnimationRun, setIsAnimationRun] = (0, _react.useState)(false);
        const [innerValue, setInnerValue] = (0, _react.useState)('');
        const [shouldFocusInput, setShouldFocusInput] = (0, _react.useState)(false);
        const value = outerValue ?? innerValue;
        const innerWidth = width ? (0, _utils.getSizeValueFromProp)(width) : '100%';
        const actionIconSize = size === 'xs' ? 'xs' : 's';
        const solidViewClass = segmentation === 'solid' ? _NumberInputtokens.classes.solidView : undefined;
        const segmentedViewClass = segmentation === 'segmented' ? _NumberInputtokens.classes.segmentedView : undefined;
        const clearViewClass = clear ? _NumberInputtokens.classes.clearView : undefined;
        const disabledClass = disabled ? _NumberInputtokens.classes.disabled : undefined;
        const loadingClass = isLoading ? _NumberInputtokens.classes.loading : undefined;
        const focusedClass = isInputFocused ? _NumberInputtokens.classes.focused : undefined;
        const isMinValue = (currentValue)=>min !== undefined && currentValue <= min;
        const isMaxValue = (currentValue)=>max !== undefined && currentValue >= max;
        const isValueEmpty = (value === '' || value === undefined) && !isManualInput;
        const onlyShowIncrement = isValueEmpty && displayWithoutValue === 'increment';
        const onlyShowDecrement = isValueEmpty && displayWithoutValue === 'decrement';
        const decrementAtLimit = (0, _utils.isNumber)(value) && value !== '' && isMinValue(Number(value)) && !isAnimationRun;
        const incrementAtLimit = (0, _utils.isNumber)(value) && value !== '' && isMaxValue(Number(value)) && !isAnimationRun;
        const decrementButtonDisabled = (decrementAtLimit && limitBehavior !== 'hidden' || isLoading) && !disabled ? _NumberInputtokens.classes.actionButtonDecrementDisabled : undefined;
        const incrementButtonDisabled = (incrementAtLimit && limitBehavior !== 'hidden' || isLoading) && !disabled ? _NumberInputtokens.classes.actionButtonIncrementDisabled : undefined;
        const decrementHiddenClass = decrementAtLimit && limitBehavior === 'hidden' ? _NumberInputtokens.classes.decrementHidden : undefined;
        const incrementHiddenClass = incrementAtLimit && limitBehavior === 'hidden' ? _NumberInputtokens.classes.incrementHidden : undefined;
        const onlyIncrementShownClass = onlyShowIncrement ? _NumberInputtokens.classes.onlyIncrementShown : undefined;
        const onlyDecrementShownClass = onlyShowDecrement ? _NumberInputtokens.classes.onlyDecrementShown : undefined;
        const handleDecrement = ()=>{
            if (isLoading || disabled || isAnimationRun) {
                return;
            }
            if (onlyShowDecrement) {
                setShouldFocusInput(true);
            }
            const processedValue = (0, _utils.isNumber)(value) && value !== '' ? Number(value) : max ?? 0;
            const preciseDiff = (0, _utils1.getPreciseValue)(processedValue - step, precision);
            const diffValue = Number(preciseDiff);
            const resValue = min !== undefined && diffValue <= min ? min : diffValue;
            setInnerValue(resValue);
            if (onDecrement) {
                onDecrement(resValue);
            }
            if (onChange) {
                onChange(null, resValue);
            }
        };
        const handleIncrement = ()=>{
            if (isLoading || disabled || isAnimationRun) {
                return;
            }
            if (onlyShowIncrement) {
                setShouldFocusInput(true);
            }
            const processedValue = (0, _utils.isNumber)(value) && value !== '' ? Number(value) : min ?? 0;
            const preciseDiff = (0, _utils1.getPreciseValue)(processedValue + step, precision);
            const diffValue = Number(preciseDiff);
            const resValue = max !== undefined && diffValue >= max ? max : diffValue;
            setInnerValue(resValue);
            if (onIncrement) {
                onIncrement(resValue);
            }
            if (onChange) {
                onChange(null, resValue);
            }
        };
        const decrementButton = customDecrementButton || /*#__PURE__*/ _react.default.createElement(_ui.ActionButton, {
            className: (0, _utils.cx)(_NumberInputtokens.classes.actionButtonDecrement, decrementButtonDisabled, solidViewClass, segmentedViewClass),
            icon: decrementIcon || /*#__PURE__*/ _react.default.createElement(_Icon.IconMinus, {
                color: "inherit",
                size: actionIconSize
            }),
            onClick: handleDecrement,
            tabIndex: onlyShowDecrement ? 0 : -1
        });
        const incrementButton = customIncrementButton || /*#__PURE__*/ _react.default.createElement(_ui.ActionButton, {
            className: (0, _utils.cx)(_NumberInputtokens.classes.actionButtonIncrement, incrementButtonDisabled, solidViewClass, segmentedViewClass),
            icon: incrementIcon || /*#__PURE__*/ _react.default.createElement(_Icon.IconPlus, {
                color: "inherit",
                size: actionIconSize
            }),
            onClick: handleIncrement,
            tabIndex: onlyShowIncrement ? 0 : -1
        });
        return /*#__PURE__*/ _react.default.createElement(Root, {
            view: view,
            size: size,
            disabled: disabled,
            clear: clear,
            shape: shape,
            segmentation: segmentation,
            isLoading: isLoading,
            ...segmentation === 'solid' ? {
                inputBackgroundType: 'clear'
            } : {
                inputBackgroundType
            },
            className: (0, _utils.cx)(className, solidViewClass, segmentedViewClass, clearViewClass, disabledClass, loadingClass, focusedClass, decrementHiddenClass, incrementHiddenClass, onlyIncrementShownClass, onlyDecrementShownClass),
            style: {
                ...style,
                [_NumberInputtokens.privateTokens.inputWidth]: innerWidth
            }
        }, !onlyShowIncrement && decrementButton, !onlyShowIncrement && !onlyShowDecrement && /*#__PURE__*/ _react.default.createElement(_ui.NumberInput, {
            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: ()=>setShouldFocusInput(false),
            ...rest
        }), !onlyShowDecrement && incrementButton);
    });
const numberInputConfig = {
    name: 'NumberInput',
    tag: 'div',
    layout: numberInputRoot,
    base: _NumberInputstyles.base,
    variations: {
        view: {
            css: _base.base
        },
        size: {
            css: _base1.base
        },
        shape: {
            css: _base2.base
        },
        inputBackgroundType: {
            css: _base3.base
        },
        segmentation: {
            css: _base4.base
        },
        disabled: {
            css: _base5.base,
            attrs: true
        }
    },
    defaults: {
        view: 'default',
        size: 'l',
        shape: 'cornered',
        segmentation: 'default'
    }
};
