import {useEffect} from "react";

function useError(error: string, enableValidation: boolean, intlTelRef: any) {
	// Error element append
	useEffect(() => {
		if (!enableValidation) {
			return;
		}
		const inputElement = intlTelRef.current?.getInput();
		if (!inputElement) {
			return;
		}
		const wrapper = inputElement.closest('.hulk_affef_telephone_wrap');
		const errorElement = wrapper?.querySelector('.elementor-message');
		if (errorElement) {
			if (error) {
				errorElement.innerHTML = error;
			} else {
				errorElement.remove();
			}
		} else if (error) {
			const createElement = document.createElement('span');
			createElement.className = 'elementor-message elementor-message-danger elementor-help-inline elementor-form-help-inline';
			createElement.innerHTML = error;
			wrapper?.appendChild(createElement);
		}
	}, [enableValidation, error, intlTelRef]);
}

export default useError
