import { useEffect, useState } from 'react';

export const useData = () => {
	const [userData, setUserData] = useState(null);
	const [isPopupDisplayed, setIsPopupDisplayed] = useState(false);
	const [usernameMessage, setUsernameMessage] = useState(null);
	const [passwordMessage, setPasswordMessage] = useState(null);

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

			const request = await fetch('/api/user', {
				method: 'GET',
				headers: {
					Authorization: token,
				},
			});

			const response = await request.json();

			const formattedResponse = {
				...response,
				createdat: new Date(response.createdat).toLocaleDateString(
					'fr-FR'
				),
			};

			setUserData(formattedResponse);
		})();
	}, []);

	return {
		userData,
		isPopupDisplayed,
		setIsPopupDisplayed,
		usernameMessage,
		setUsernameMessage,
		passwordMessage,
		setPasswordMessage,
	};
};
