"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.PostfixInput = void 0;
const react_1 = __importStar(require("react"));
const Styled = __importStar(require("../BaseInput/index.styled"));
const ExpendStyled = __importStar(require("./index.styled"));
const Typography_1 = require("../../../core/Typography");
const Colors_1 = require("../../../core/Colors");
const InputFrame_1 = require("../common/InputFrame");
/**
 *  fyi.  decoration 다시 살림 (https://pfcoworkspace.slack.com/archives/C0145TGCZ41/p1701937714740749)
 */
const PostfixInput = react_1.default.forwardRef((props, forwardedRef) => {
    const { realValue, postfix, onBlur, onFocus, onClick, disabled, label, placeholder, errorValidation, logRocketMasking, focus = false, max, decoration, updateRealValue, formatRealValue, formatDisplayValue, type, pattern } = props, otherProps = __rest(props, ["realValue", "postfix", "onBlur", "onFocus", "onClick", "disabled", "label", "placeholder", "errorValidation", "logRocketMasking", "focus", "max", "decoration", "updateRealValue", "formatRealValue", "formatDisplayValue", "type", "pattern"]);
    const [displayValue, setDisplayValue] = (0, react_1.useState)('');
    const [errorText, setErrorText] = (0, react_1.useState)('');
    const [focused, setFocused] = (0, react_1.useState)(focus);
    const inputRef = (0, react_1.useRef)(null);
    const active = focused || Boolean(displayValue);
    const showPlaceholderOnlyNotActive = !label && !active && Boolean(placeholder);
    const handleValidateMessage = (0, react_1.useCallback)((val) => {
        if (val.length === 0 || val === '0') {
            // 빈값일때 에러 안나게
            setErrorText('');
            return;
        }
        if (errorValidation) {
            for (let i = 0; i < errorValidation.length; i++) {
                if (!errorValidation[i].condition(val) && val.length !== 0) {
                    setErrorText(errorValidation[i].errorText);
                    break;
                }
                else {
                    setErrorText('');
                }
            }
        }
    }, [errorValidation]);
    const handleInputFocusState = (0, react_1.useCallback)((e) => {
        const { type } = e;
        if (type === 'focus') {
            setFocused(true);
        }
        else {
            setFocused(false);
        }
    }, []);
    const handleClickInput = (0, react_1.useCallback)((e) => {
        var _a;
        onClick === null || onClick === void 0 ? void 0 : onClick(e);
        (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
    }, [onClick, inputRef]);
    const handleChangeInput = (0, react_1.useCallback)((val) => {
        const updateValue = (formatRealValue === null || formatRealValue === void 0 ? void 0 : formatRealValue(val)) || val;
        const maxNumber = Number(max);
        const numeric = Number(updateValue);
        if (maxNumber && !isNaN(numeric)) {
            if (numeric > maxNumber) {
                return;
            }
        }
        updateRealValue(updateValue);
    }, [max, updateRealValue, formatRealValue]);
    (0, react_1.useEffect)(() => {
        let newDisplayValue = realValue;
        if (formatDisplayValue) {
            newDisplayValue = (formatDisplayValue === null || formatDisplayValue === void 0 ? void 0 : formatDisplayValue(newDisplayValue)) || ''; // 0만원 안되도록
        }
        setDisplayValue(newDisplayValue);
        handleValidateMessage(realValue);
    }, [formatDisplayValue, handleValidateMessage, realValue]);
    (0, react_1.useEffect)(() => {
        var _a;
        if (focus) {
            (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
        }
    }, [inputRef, focus]);
    return (react_1.default.createElement(InputFrame_1.InputFrame, { label: label, active: active, focused: focused, disabled: Boolean(disabled), errorText: errorText, handleClickInput: handleClickInput },
        react_1.default.createElement(ExpendStyled.InputAndUnitWrapper, null,
            react_1.default.createElement(ExpendStyled.InputSizer, { focused: focused, displayValue: displayValue },
                react_1.default.createElement(ExpendStyled.InputTag, Object.assign({}, otherProps, { className: ExpendStyled.className.inputTag, hasLabel: Boolean(label), placeholder: showPlaceholderOnlyNotActive ? placeholder : '', "data-private": logRocketMasking, ref: forwardedRef || inputRef, name: label, disabled: disabled, value: formatDisplayValue ? displayValue : realValue, onChange: (e) => {
                        const val = e.currentTarget.value;
                        handleChangeInput(val);
                    }, 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: false, pattern: type === 'number' ? '\\d*' : pattern, type: type, onClick: handleClickInput, size: showPlaceholderOnlyNotActive ? undefined : 1 }))),
            react_1.default.createElement(ExpendStyled.UnitWrapper, null, active && (react_1.default.createElement(ExpendStyled.Unit, { hasLabel: Boolean(label), hasValue: Boolean(displayValue) },
                react_1.default.createElement(Typography_1.BaseTypography, { md: "Title1", color: disabled ? Colors_1.basicColors.gray200 : undefined }, postfix))))),
        decoration && react_1.default.createElement(Styled.DecorationItemWrapper, { disabled: disabled }, decoration)));
});
exports.PostfixInput = PostfixInput;
