'use strict';

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

var _tslib = require('../../_virtual/_tslib.js');
var React = require('react');
var AlertTriangle = require('../../icons/AlertTriangle.js');
var Minus = require('../../icons/Minus.js');
var Plus = require('../../icons/Plus.js');
var index$1 = require('../IconButton/index.js');
var index = require('../../utils/index.js');
var styles = require('./styles.js');
var styles$1 = require('../Field/styles.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

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

function StepperComponent(_a, ref) {
    var { step = 1, min, max, value, id, label, optionalText, name, errorText, assistText, onChange = index.noop, touched, disabled = false, required = true, hasError = false, fullWidth = false, stepperVariant = 'default', componentHeight = 'medium', iconButtonVariant = 'ghost' } = _a, props = _tslib.__rest(_a, ["step", "min", "max", "value", "id", "label", "optionalText", "name", "errorText", "assistText", "onChange", "touched", "disabled", "required", "hasError", "fullWidth", "stepperVariant", "componentHeight", "iconButtonVariant"]);
    const [showError, setShowError] = React.useState(false);
    const appendTestId = index.getId(props['data-testid']);
    const appendTrackId = index.getId(props['data-testid']);
    const handleChange = (value) => {
        if (onChange)
            onChange(value);
    };
    const handleSub = () => {
        let newValue;
        if (index.isDifferentFromUndefinedOrNull(min)) {
            if (value > min) {
                if (value - step < min) {
                    newValue = min;
                }
                else {
                    newValue = value - step;
                }
            }
            else {
                newValue = value;
            }
        }
        else {
            newValue = value - step;
        }
        newValue !== value && handleChange(newValue);
    };
    const handleAdd = () => {
        let newValue;
        if (index.isDifferentFromUndefinedOrNull(max)) {
            if (value < max) {
                if (value + step > max) {
                    newValue = max;
                }
                else {
                    newValue = value + step;
                }
            }
            else {
                newValue = value;
            }
        }
        else {
            newValue = value + step;
        }
        newValue !== value && handleChange(newValue);
    };
    React.useEffect(() => {
        const composeError = typeof touched === 'undefined' ? hasError : hasError && touched;
        setShowError(composeError);
    }, [hasError, touched]);
    return (React__default['default'].createElement(styles.Wrapper, { fullWidth: fullWidth, "data-testid": `${props['data-testid']}-stepper` },
        React__default['default'].createElement(styles$1.LabelWrapper, { disabled: disabled, hasLabel: Boolean(label === null || label === void 0 ? void 0 : label.length) },
            label && (React__default['default'].createElement(styles$1.Label, { htmlFor: id || `input-${name}`, disabled: disabled }, label)),
            !required && React__default['default'].createElement(styles$1.Text, null, optionalText)),
        React__default['default'].createElement(styles.FocusBorder, null,
            React__default['default'].createElement(styles.Border, { hasError: showError, disabled: disabled, stepperVariant: stepperVariant },
                React__default['default'].createElement(index$1.IconButton, { onClick: handleSub, variant: iconButtonVariant, size: componentHeight, icon: React__default['default'].createElement(Minus['default'], null), disabled: disabled || value - step < min, "aria-label": `Remove ${step}`, "aria-labelledby": "step-decrement label-text", "data-testid": appendTestId('stepper-minus-button'), "data-trackid": appendTrackId('stepper-minus-button') }),
                React__default['default'].createElement(styles.NumberField, { buttonsDisabled: disabled, ref: ref, id: id || (name && `input-${name}`), type: "number", value: value, min: min, max: max, name: name, disabled: true, componentHeight: componentHeight, stepperVariant: stepperVariant, hasError: showError, "aria-label": props['aria-label'] || label, "aria-live": "polite", "aria-labelledby": "label-number", "data-testid": appendTestId('stepper-number-field'), "data-trackid": appendTrackId('stepper-number-field') }),
                React__default['default'].createElement(index$1.IconButton, { onClick: handleAdd, variant: iconButtonVariant, size: componentHeight, icon: React__default['default'].createElement(Plus['default'], null), disabled: disabled || value + step > max, "aria-label": `Add ${step}`, "aria-labelledby": "step-increment label-text", "data-testid": appendTestId('stepper-plus-button'), "data-trackid": appendTrackId('stepper-plus-button') }))),
        React__default['default'].createElement(styles$1.SupportingText, null,
            Boolean(errorText) &&
                showError &&
                !disabled &&
                (stepperVariant === null || stepperVariant === void 0 ? void 0 : stepperVariant.includes('input')) && (React__default['default'].createElement(styles$1.ErrorMessage, null,
                React__default['default'].createElement(AlertTriangle['default'], { width: 20, height: 20 }),
                errorText)),
            !showError &&
                (typeof assistText === 'string' ? (React__default['default'].createElement(styles$1.AssistMessage, null, assistText)) : (assistText)))));
}
const Stepper = React.forwardRef(StepperComponent);

exports.Stepper = Stepper;
