var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useState } from 'react';
import InputMasked from '../InputMasked';
import css from './input.module.scss';
import { _cx } from 'src/utility/class';
var cx = _cx(css);
export default function Input(_a) {
    var _b = _a.value, value = _b === void 0 ? '' : _b, onChange = _a.onChange, onSelect = _a.onSelect, onKeyDown = _a.onKeyDown, id = _a.id, mask = _a.mask, _c = _a.maskChar, maskChar = _c === void 0 ? '_' : _c, type = _a.type, isInValid = _a.isInValid, _d = _a.error, error = _d === void 0 ? false : _d, _e = _a.showError, showError = _e === void 0 ? false : _e, _f = _a.placeholder, placeholder = _f === void 0 ? '' : _f, _g = _a.mode, mode = _g === void 0 ? "normal" : _g, buttonProps = _a.buttonProps, _h = _a.useSpaceAfter, useSpaceAfter = _h === void 0 ? false : _h, _j = _a.useSpaceBelow, useSpaceBelow = _j === void 0 ? false : _j, maxLength = _a.maxLength, autoFocus = _a.autoFocus, wrapperStyle = _a.wrapperStyle, inputStyle = _a.inputStyle, borderColor = _a.borderColor, bgColor = _a.bgColor, handleOnBlur = _a.handleOnBlur;
    if (mask) {
        return (_jsx(InputMasked, { id: id, mode: mode, value: value, onChange: onChange, onSelect: onSelect, showError: showError, error: error, isInValid: isInValid, mask: mask, maskChar: maskChar, placeholder: placeholder, useSpaceAfter: useSpaceAfter, buttonProps: buttonProps, wrapperStyle: wrapperStyle, inputStyle: inputStyle, borderColor: borderColor, bgColor: bgColor }));
    }
    var _k = __read(useState(false), 2), inFocusNotEmpty = _k[0], setInFocusNotEmptyvalue = _k[1];
    var _l = __read(useState(false), 2), errorBorder = _l[0], setErrorBorder = _l[1];
    useEffect(function () {
        if (!error)
            setErrorBorder(false);
    }, [error]);
    useEffect(function () {
        if (showError && error)
            setErrorBorder(true);
    }, [showError]);
    return (_jsxs("div", { className: cx('input-wrapper', {
            useSpaceAfter: useSpaceAfter,
            useSpaceBelow: useSpaceBelow,
        }), style: wrapperStyle, children: [_jsx("input", { className: cx('styled-input', {
                    normal: mode === 'normal',
                    presentation: mode === 'presentation',
                    small: mode === 'small',
                    'small-fix': mode === 'small-fix',
                    isInValid: isInValid,
                    isValid: typeof isInValid === 'boolean' && !isInValid,
                    errorBorder: errorBorder,
                    inFocusNotEmpty: inFocusNotEmpty,
                }), id: id, value: value, type: "text", pattern: type == 'number' ? '[0-9]*' : undefined, onChange: onChange, placeholder: placeholder, maxLength: maxLength, autoFocus: autoFocus, onKeyDown: onKeyDown, onSelect: onSelect, onFocus: function () { return value && value.length > 0 && setInFocusNotEmptyvalue(true); }, onBlur: function () {
                    var isError = !!error;
                    setErrorBorder(isError);
                    setInFocusNotEmptyvalue(false);
                    handleOnBlur && handleOnBlur();
                }, style: inputStyle }), buttonProps && buttonProps.button] }));
}
