"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseInputStyled = exports.BaseInput = void 0;
const react_1 = __importStar(require("react"));
const Styled = __importStar(require("./index.styled"));
const InputFrame_1 = require("../common/InputFrame");
/**
 * @description input decoration은 항상 오른쪽에 위치해 있다.
 */
const BaseInput = react_1.default.forwardRef((props, forwardedRef) => {
    const { value = '', placeholder, onBlur, onFocus, onChange, disabled, label, decoration, errorValidation, logRocketMasking, focus = false, focusedAnimation = true, format = '', pattern, type } = props, otherProps = __rest(props, ["value", "placeholder", "onBlur", "onFocus", "onChange", "disabled", "label", "decoration", "errorValidation", "logRocketMasking", "focus", "focusedAnimation", "format", "pattern", "type"]);
    const [errorText, setErrorText] = (0, react_1.useState)('');
    const [focused, setFocused] = (0, react_1.useState)(false);
    const inputRef = (0, react_1.useRef)(null);
    const active = focused || Boolean(value);
    const showPlaceHolder = (label && active) || !label;
    const handleInputFocusState = (0, react_1.useCallback)((e) => {
        const { type } = e;
        if (otherProps.readOnly || disabled) {
            return;
        }
        if (focusedAnimation) {
            setFocused(type === 'focus');
        }
    }, [disabled, focusedAnimation, otherProps.readOnly]);
    (0, react_1.useEffect)(() => {
        var _a;
        if (focus) {
            (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
        }
    }, [focus]);
    (0, react_1.useEffect)(() => {
        const cleanValue = value.replace(format, '').trim();
        if (cleanValue && errorValidation) {
            for (let i = 0; i < errorValidation.length; i++) {
                if (!errorValidation[i].condition(value) && value.length !== 0) {
                    setErrorText(errorValidation[i].errorText);
                    break;
                }
                else {
                    setErrorText('');
                }
            }
        }
    }, [value, errorValidation, format]);
    (0, react_1.useEffect)(() => {
        const numberInput = inputRef.current;
        const handleWheel = (e) => {
            e.preventDefault();
        };
        if (numberInput) {
            numberInput.addEventListener('wheel', handleWheel);
            return () => {
                numberInput.removeEventListener('wheel', handleWheel);
            };
        }
    }, []);
    return (react_1.default.createElement(InputFrame_1.InputFrame, { label: label, focused: focused, active: active, disabled: Boolean(disabled), errorText: errorText },
        react_1.default.createElement(Styled.InputTag, Object.assign({ "data-private": logRocketMasking, ref: forwardedRef || inputRef, name: label, disabled: disabled, value: value, onChange: onChange, onFocus: (e) => {
                onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
                handleInputFocusState(e);
            }, onBlur: (e) => {
                onBlur === null || onBlur === void 0 ? void 0 : onBlur(e);
                handleInputFocusState(e);
            }, decoration: Boolean(decoration), pattern: type === 'number' ? '\\d*' : pattern, type: type, hasLabel: Boolean(label), placeholder: showPlaceHolder ? placeholder : '' }, otherProps)),
        decoration && react_1.default.createElement(Styled.DecorationItemWrapper, { disabled: disabled }, decoration)));
});
exports.BaseInput = BaseInput;
exports.BaseInputStyled = __importStar(require("./index.styled"));
