import { useCallback, useState } from "preact/hooks";

export const useInputState = (defaultValue = ''): [string, (e: Event | string) => void] => {
    const [value, setValue] = useState(defaultValue);

    const handleInput = useCallback((e: Event | string) => {
        if (typeof e === 'string') {
            setValue(e);
        } else {
            const { target } = e;
            if (target && 'value' in target && typeof target.value === 'string') {
                setValue(target.value);
            } else if (target instanceof HTMLElement) {
                setValue(target.innerHTML);
            }
        }
    }, []);

    return [value, handleInput];
}