import { Modules } from "@/types/modules";
import { User } from "@/types/users";

// user will need access to the module.
// users role will determine there visual access
export function hasAccess(
    user: User | null,
    moduleName: string | null,
    modules: Modules[]
): boolean {
    // get the modules for the id
    const filteredModule = modules?.filter((f) => f.name === moduleName);
    //console.log(filteredModule[0]);
    // userroles and filter out by the module id,

    const roleCheck: any = user?.roles.find(
        (role) => role.module_id === filteredModule[0].module_id
    );

    if (filteredModule[0].roles.includes(roleCheck?.role)) {
        return true;
    }
    //if(filteredModule[0].roles.includes(roleCheck.))
    return false;
}

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

    // get only the module in the user profile
    //console.log(user);
    const userRole = user?.roles.filter((role) => role.module_id === module_id);
    //console.log(userRole[0]?.role);
    // if (role.includes(userRole[0]?.role)) {

    //     return true};
    if (userRole.length !== 0) return true;
    return false;
}
