import { usePage } from '@inertiajs/vue3';
import { computed } from 'vue';

export function useCan() {
    const page = usePage();

    const permissions = computed(() => page.props.auth.user?.permissions ?? []);
    const roles = computed(() => page.props.auth.user?.roles ?? []);

    function can(permission: string): boolean {
        return permissions.value.includes(permission);
    }

    function hasRole(role: string): boolean {
        return roles.value.includes(role);
    }

    function hasAnyRole(roleList: string[]): boolean {
        return roleList.some((role) => roles.value.includes(role));
    }

    return {
        permissions,
        roles,
        can,
        hasRole,
        hasAnyRole,
    };
}
