import {useEffect} from "react";
import jQuery from "jquery";

type EffectCallback = () => void | (() => void | undefined);
type DependencyList = ReadonlyArray<any>;

function useFormSubmitSuccess(inputRef: any, effect: EffectCallback, deps?: DependencyList): void {
	useEffect(() => {
		if (!inputRef.current) {
			return;
		}

		const formElement = jQuery(inputRef.current).closest('.elementor-form');

		formElement.on('submit_success.hulk_affef', effect);
		return () => {
			formElement.off('submit_success.hulk_affef', effect);
		}
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, deps);
}

export default useFormSubmitSuccess;
