// user will need access to the module.

import type { User } from "../-types/users";

// users role will determine there visual access
export function hasAccess(user: any, moduleName: string | null): boolean {
	//console.log("has access user", user, moduleName);
	// get the modules for the id

	const filteredModule = user?.roles?.filter(
		(f: any) => f.module === moduleName,
	);
	//console.log(filteredModule[0]);
	// userroles and filter out by the module id,
	//console.log("Has Module access", filteredModule);
	// const roleCheck: any = user?.roles.find(
	// 	(role) => role.module_id === filteredModule[0].module_id,
	// );

	if (filteredModule && filteredModule.length > 0) {
		return true;
	}
	//if(filteredModule[0].roles.includes(roleCheck.))
	return false;
}

export function hasPageAccess(
	user: User | null,
	role: any,
	moduleName: string,
): boolean {
	if (role.includes("viewer")) return true;
	if (!user) return false;

	//console.log(user);
	const userRole = user?.roles?.filter(
		(role: any) => role.module === moduleName,
	);

	//console.log(user);

	// if (role.includes(userRole[0]?.role)) {

	//     return true};
	//if (userRole.length > 0) return true;
	if (userRole?.length >= 1) {
		//console.log(userRole);
		return true;
	} else {
		return false;
	}

	//return false;
}
