import {FocusEvent, Component} from 'react'

interface TypeFocusHandlerOpts {
    onFocus?: (e: FocusEvent) => void
    onBlur?: (e: FocusEvent) => void
}

export function createFocusHandler(
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    comp: Component<any, any>,
    propName: string
) {
    const opts: TypeFocusHandlerOpts = {}

    function setFocus(focus: boolean) {
        comp.setState({
            [propName]: focus,
        })
    }

    return {
        init(
            initOpts: TypeFocusHandlerOpts = {}
        ) {
            Object.assign(opts, initOpts)
        },
        handleFocus(e: FocusEvent) {
            setFocus(true)
            opts.onFocus && opts.onFocus(e)
        },
        handleBlur(e: FocusEvent) {
            setFocus(false)
            opts.onBlur && opts.onBlur(e)
        },
    }
}
