import type { IUserData } from "@/interfaces/userdata.interface";
import { useEncodedLocalStorage } from "@/services/localStorage";
import React from "react";

export interface IUserDataProvider {
	userData: IUserData | undefined;
	setUserData: React.Dispatch<React.SetStateAction<IUserData | undefined>>;
}

export const UserDataContext = React.createContext<IUserDataProvider | undefined>(
	undefined,
);

export const UserDataProvider = ({ children }: { children: React.ReactNode }) => {
	const [userData, setUserData] = useEncodedLocalStorage<IUserData | undefined>(
		"user_data",
		undefined,
	);

	return (
		<UserDataContext.Provider value={{ userData, setUserData }}>
			{children}
		</UserDataContext.Provider>
	);
};
