export const useActions = ({
	loveToDelete,
	setLoveToDelete,
	setDeleteMessage,
	refreshDataset,
}) => {
	const handleEdit = (id: string) => {
		alert('Fonctionnalité en cours de développement');
	};

	const openPopup = (id: string) => {
		setDeleteMessage(null);
		setLoveToDelete(id);
	};

	const closePopup = () => {
		setDeleteMessage(null);
		setLoveToDelete(null);
	};

	const deleteLove = async () => {
		const token = localStorage.getItem('token');

		try {
			const response = await fetch('/api/point', {
				method: 'DELETE',
				headers: {
					Authorization: token,
					'Content-Type': 'application/json',
				},
				body: JSON.stringify({
					pointId: loveToDelete,
				}),
			});

			if (response.ok) {
				await refreshDataset();
				setLoveToDelete(null);
				return;
			}
		} catch (error) {
			console.error(error);
		}

		setDeleteMessage("Une erreur s'est produite. Veuillez réessayer");
	};

	return {
		handleEdit,
		openPopup,
		closePopup,
		deleteLove,
	};
};
