import { ReactNode, FC } from "react";

import { useIdentityStore } from "@/features/Login/lib/identity.store";

import { AuthorizedContext } from "./AuthorizedContext";


interface AuthProviderProps {
	children: ReactNode;
}

export const AuthorizedContextProvider: FC<AuthProviderProps> = ({
	children,
}) => {
	const user = useIdentityStore((x) => x.user);
	if (!user) {
		return null;
	}

	return (
		<AuthorizedContext.Provider
			value={{
				user: {
					id: user.id,
					username: user.username,
					role: user.role,
				},
			}}
		>
			{children}
		</AuthorizedContext.Provider>
	);
};
