import {
	canWrite,
	getRoutePermission,
	hasPermission,
	type BackofficePermission
} from '$lib/constants/backoffice-role';
import type { BackofficeRole, BackofficeSession } from '$lib/types/backoffice-user';

export function assertBackofficePermission(
	user: BackofficeSession | null,
	permission: BackofficePermission
): user is BackofficeSession {
	return Boolean(user && hasPermission(user.role, permission));
}

export function assertBackofficeWrite(user: BackofficeSession | null): user is BackofficeSession {
	return Boolean(user && canWrite(user.role));
}

export function canAccessPath(role: BackofficeRole, pathname: string): boolean {
	const permission = getRoutePermission(pathname);
	if (!permission) return true;
	return hasPermission(role, permission);
}
