// Listen filter (whitelist/blacklist) — pure, so it is unit-testable without
// booting the Discord client.
//
// Rules:
// - a blacklisted user OR a user with a blacklisted role is always excluded;
// - if any whitelist (user or role) is set, only whitelisted speakers pass;
// - otherwise everyone passes.
export function isAllowed(userId, roleIds, lists) {
  const L = lists || {};
  const idSet = (arr) => new Set((arr || []).map((x) => x.id));
  const roles = new Set(roleIds || []);
  const hasRole = (arr) => (arr || []).some((r) => roles.has(r.id));
  if (idSet(L.blacklistUsers).has(userId) || hasRole(L.blacklistRoles)) return false;
  const wlUsers = L.whitelistUsers || [];
  const wlRoles = L.whitelistRoles || [];
  if (wlUsers.length === 0 && wlRoles.length === 0) return true;
  return idSet(wlUsers).has(userId) || hasRole(wlRoles);
}
