import { forwardRef, useCallback, useEffect, useRef } from '@byted-doubao-apps/framework';
import { warnUnsupported } from '../base/warn-unsupported.js';
import { mapInputProps, resolveFocusBehavior, toFocusEventDetail, toInputCommonEvent, toInputEventDetail, toSelectionChangeEventDetail, toValueEventDetail } from './map.js';
export const Input = forwardRef(InputImpl);
function InputImpl(props, ref) {
    const { onInput, onFocus, onBlur, onConfirm, onSelectionChange, ...nonEventProps } = props;
    const { mapped, unsupported, inputId, resolvedFocus, resolvedAutoFocus } = mapInputProps(nonEventProps);
    const inputRef = useRef(null);
    const hasAutoFocusedRef = useRef(false);
    warnUnsupported('Input', unsupported);
    const assignRef = useCallback((instance) => {
        inputRef.current = instance;
        assignForwardedRef(ref, instance);
    }, [ref]);
    useEffect(() => {
        const { action, hasAutoFocused } = resolveFocusBehavior(resolvedFocus, resolvedAutoFocus, hasAutoFocusedRef.current);
        hasAutoFocusedRef.current = hasAutoFocused;
        if (action === 'focus') {
            void inputRef.current?.focus();
            return;
        }
        if (action === 'blur') {
            void inputRef.current?.blur();
        }
    }, [resolvedAutoFocus, resolvedFocus]);
    const handleInput = useCallback((event) => {
        onInput?.(toInputCommonEvent(toInputEventDetail(event), 'input', inputId));
    }, [inputId, onInput]);
    const handleFocus = useCallback((event) => {
        onFocus?.(toInputCommonEvent(toFocusEventDetail(event), 'focus', inputId));
    }, [inputId, onFocus]);
    const handleBlur = useCallback((event) => {
        onBlur?.(toInputCommonEvent(toValueEventDetail(event), 'blur', inputId));
    }, [inputId, onBlur]);
    const handleConfirm = useCallback((event) => {
        onConfirm?.(toInputCommonEvent(toValueEventDetail(event), 'confirm', inputId));
    }, [inputId, onConfirm]);
    const handleSelectionChange = useCallback((event) => {
        onSelectionChange?.(toInputCommonEvent(toSelectionChangeEventDetail(event), 'selectionchange', inputId));
    }, [inputId, onSelectionChange]);
    return (<input {...mapped} ref={assignRef} {...(onInput ? { onInput: handleInput } : {})} {...(onFocus ? { onFocus: handleFocus } : {})} {...(onBlur ? { onBlur: handleBlur } : {})} {...(onConfirm ? { onConfirm: handleConfirm } : {})} {...(onSelectionChange ? { onSelectionChange: handleSelectionChange } : {})}/>);
}
function assignForwardedRef(ref, value) {
    if (typeof ref === 'function') {
        ref(value);
        return;
    }
    if (ref) {
        ref.current = value;
    }
}
//# sourceMappingURL=index.jsx.map