'use client';

// ---------------------------------------------------------------------------------------------------------------------
//!                                                      Imports
// ---------------------------------------------------------------------------------------------------------------------

// -------------------------------------------------- React & Next -----------------------------------------------------
import { useEffect, useState } from 'react';
import { redirect, usePathname } from 'next/navigation';
import Navbar from '@/components/Navbar/Navbar';
// ---------------------------------------------------------------------------------------------------------------------

const Provider = ({ children }) => {
	const pathname = usePathname();
	const [isSuccess, setIsSuccess] = useState(false);

	useEffect(() => {
		if (pathname.startsWith('/auth')) {
			setIsSuccess(true);
			return;
		}

		const token = localStorage.getItem('token');

		if (!token) {
			redirect('/auth/login');
		}

		setIsSuccess(true);
	}, [pathname]);

	if (!isSuccess) {
		return;
	}

	if (pathname.startsWith('/auth')) {
		return children;
	}

	return (
		<>
			{children}
			<Navbar />
		</>
	);
};

export default Provider;
