"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.inputVariants = exports.Input = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const class_variance_authority_1 = require("class-variance-authority");
const react_1 = tslib_1.__importStar(require("react"));
const react_native_1 = require("react-native");
const theme_1 = require("../../lib/theme/theme");
const zero = tslib_1.__importStar(require("../../ui"));
const input_1 = require("./primitives/input");
const inputVariants = (0, class_variance_authority_1.cva)("", {
    variants: {
        variant: {
            default: "default",
            filled: "filled",
            underlined: "underlined",
        },
        size: {
            sm: "sm",
            md: "md",
            lg: "lg",
        },
    },
    defaultVariants: {
        variant: "default",
        size: "md",
    },
});
exports.inputVariants = inputVariants;
exports.Input = (0, react_1.forwardRef)(({ variant = "default", size = "md", label, description, error, required = false, leftAddon, rightAddon, disabled = false, containerStyle, inputStyle, ...props }, ref) => {
    const { zero: zt, theme } = (0, theme_1.useTheme)();
    const [isFocused, setIsFocused] = react_1.default.useState(false);
    const inputRef = react_1.default.useRef(null);
    // Get variant and size styles using theme.zero
    const containerStyles = react_1.default.useMemo(() => {
        const variantStyle = (() => {
            switch (variant) {
                case "filled":
                    return [zt.bg.muted];
                case "underlined":
                    return [
                        zt.bg.transparent,
                        { borderRadius: 0, paddingHorizontal: 0, borderWidth: 0 },
                    ];
                default:
                    return [zt.bg.background, zt.border.default];
            }
        })();
        const sizeStyle = (() => {
            switch (size) {
                case "sm":
                    return [
                        zero.px[3],
                        zero.py[2],
                        { borderRadius: zero.borderRadius.md },
                    ];
                case "lg":
                    return [
                        zero.px[4],
                        zero.py[3],
                        { borderRadius: zero.borderRadius.md },
                    ];
                default:
                    return [
                        zero.px[3],
                        zero.py[2],
                        { borderRadius: zero.borderRadius.md },
                    ];
            }
        })();
        const focusStyle = isFocused ? zt.border.primary : null;
        return [variantStyle, sizeStyle, focusStyle].filter(Boolean);
    }, [variant, size, zt, isFocused]);
    const textStyles = react_1.default.useMemo(() => {
        const baseTextStyle = [zt.text.foreground, zt.bg.transparent];
        switch (size) {
            case "sm":
                return [...baseTextStyle, zt.text.sm];
            case "lg":
                return [...baseTextStyle, zt.text.lg];
            default:
                return [...baseTextStyle, zt.text.md];
        }
    }, [size, zt]);
    const handleFocus = react_1.default.useCallback((event) => {
        setIsFocused(true);
        if (props.onFocus) {
            props.onFocus(event);
        }
    }, [props.onFocus]);
    const handleBlur = react_1.default.useCallback((event) => {
        setIsFocused(false);
        if (props.onBlur) {
            props.onBlur(event);
        }
    }, [props.onBlur]);
    const handleContainerPress = react_1.default.useCallback(() => {
        if (inputRef.current && !disabled) {
            inputRef.current.focus();
        }
    }, [disabled]);
    const hasAddons = leftAddon || rightAddon;
    if (hasAddons) {
        return ((0, jsx_runtime_1.jsxs)(input_1.InputPrimitive.Group, { children: [label && ((0, jsx_runtime_1.jsx)(input_1.InputPrimitive.Label, { required: required, disabled: disabled, error: !!error, children: label })), (0, jsx_runtime_1.jsx)(react_native_1.TouchableWithoutFeedback, { onPress: handleContainerPress, children: (0, jsx_runtime_1.jsxs)(input_1.InputPrimitive.Container, { focused: isFocused, error: !!error, disabled: disabled, style: [containerStyles, containerStyle, { padding: 0 }], children: [leftAddon && ((0, jsx_runtime_1.jsx)(input_1.InputPrimitive.Addon, { position: "left", children: leftAddon })), (0, jsx_runtime_1.jsx)(input_1.InputPrimitive.Root, { ref: (node) => {
                                    inputRef.current = node;
                                    if (ref) {
                                        if (typeof ref === "function") {
                                            ref(node);
                                        }
                                        else {
                                            ref.current = node;
                                        }
                                    }
                                }, disabled: disabled, error: !!error, onFocus: handleFocus, onBlur: handleBlur, style: [textStyles, inputStyle, { outline: "none" }], placeholderTextColor: disabled ? theme.colors.textDisabled : theme.colors.textMuted, ...props }), rightAddon && ((0, jsx_runtime_1.jsx)(input_1.InputPrimitive.Addon, { position: "right", children: rightAddon }))] }) }), description && !error && ((0, jsx_runtime_1.jsx)(input_1.InputPrimitive.Description, { disabled: disabled, children: description })), (0, jsx_runtime_1.jsx)(input_1.InputPrimitive.Error, { visible: !!error, children: error })] }));
    }
    return ((0, jsx_runtime_1.jsxs)(input_1.InputPrimitive.Group, { children: [label && ((0, jsx_runtime_1.jsx)(input_1.InputPrimitive.Label, { required: required, disabled: disabled, error: !!error, children: label })), (0, jsx_runtime_1.jsx)(input_1.InputPrimitive.Root, { ref: (node) => {
                    inputRef.current = node;
                    if (ref) {
                        if (typeof ref === "function") {
                            ref(node);
                        }
                        else {
                            ref.current = node;
                        }
                    }
                }, disabled: disabled, error: !!error, onFocus: handleFocus, onBlur: handleBlur, style: [
                    containerStyles,
                    textStyles,
                    containerStyle,
                    inputStyle,
                    { outline: "none" },
                ], placeholderTextColor: disabled ? theme.colors.textDisabled : theme.colors.textMuted, ...props }), description && !error && ((0, jsx_runtime_1.jsx)(input_1.InputPrimitive.Description, { disabled: disabled, children: description })), (0, jsx_runtime_1.jsx)(input_1.InputPrimitive.Error, { visible: !!error, children: error })] }));
});
exports.Input.displayName = "Input";
//# sourceMappingURL=input.js.map