//#region src/channels/security.ts
function compileAllowlist(entries) {
	const set = new Set(entries.map((e) => String(e).trim().toLowerCase()).filter(Boolean));
	return {
		set,
		wildcard: set.has("*")
	};
}
function resolveAllowlistMatch(params) {
	const compiled = compileAllowlist(params.allowFrom);
	if (compiled.set.size === 0) return { allowed: false };
	if (compiled.wildcard) return {
		allowed: true,
		matchKey: "*",
		matchSource: "wildcard"
	};
	const senderId = params.senderId.toLowerCase();
	if (compiled.set.has(senderId)) return {
		allowed: true,
		matchKey: senderId,
		matchSource: "id"
	};
	if (params.allowNameMatching && params.senderUsername) {
		const senderUsername = params.senderUsername.toLowerCase();
		if (compiled.set.has(senderUsername)) return {
			allowed: true,
			matchKey: senderUsername,
			matchSource: "username"
		};
	}
	for (const entry of compiled.set) if (entry.endsWith(":") && senderId.startsWith(entry)) return {
		allowed: true,
		matchKey: entry,
		matchSource: "prefixed-id"
	};
	return { allowed: false };
}
function resolveAllowlistMatchSimple(params) {
	return resolveAllowlistMatch(params);
}
function resolveDmPolicy(configValue, defaultValue = "open") {
	return configValue ?? defaultValue;
}
function resolveGroupPolicy(configValue, defaultValue = "open") {
	return configValue ?? defaultValue;
}
function evaluateAccess(params) {
	const { context } = params;
	if (context.isDm) return evaluateDmAccess({
		context,
		policy: resolveDmPolicy(params.dmPolicy),
		allowFrom: params.allowFrom ?? [],
		allowNameMatching: params.allowNameMatching
	});
	return evaluateGroupAccess({
		context,
		policy: resolveGroupPolicy(params.groupPolicy),
		allowFrom: params.groupAllowFrom ?? params.allowFrom ?? [],
		allowNameMatching: params.allowNameMatching
	});
}
function evaluateDmAccess(params) {
	const { context, policy, allowFrom } = params;
	if (policy === "disabled") return {
		allowed: false,
		reason: "DM is disabled",
		policy
	};
	if (policy === "open") return {
		allowed: true,
		policy
	};
	if (policy === "pairing" || policy === "allowlist") {
		const match = resolveAllowlistMatchSimple({
			allowFrom,
			senderId: context.senderId,
			senderName: context.senderName,
			allowNameMatching: params.allowNameMatching
		});
		if (match.allowed) return {
			allowed: true,
			policy,
			match
		};
		if (policy === "pairing") return {
			allowed: false,
			reason: "pairing-required",
			policy,
			match
		};
		return {
			allowed: false,
			reason: "Not in DM allowlist",
			policy,
			match
		};
	}
	return {
		allowed: false,
		reason: "Unknown policy",
		policy
	};
}
function evaluateGroupAccess(params) {
	const { context, policy, allowFrom } = params;
	if (policy === "disabled") return {
		allowed: false,
		reason: "Group messages are blocked",
		policy
	};
	if (policy === "open") return {
		allowed: true,
		policy
	};
	if (policy === "allowlist") {
		const match = resolveAllowlistMatchSimple({
			allowFrom,
			senderId: context.senderId,
			senderName: context.senderName,
			allowNameMatching: params.allowNameMatching
		});
		return {
			allowed: match.allowed,
			reason: match.allowed ? void 0 : "Not in group allowlist",
			policy,
			match
		};
	}
	return {
		allowed: false,
		reason: "Unknown policy",
		policy
	};
}
function hasBotMention(params) {
	const { text, botUsername, entities } = params;
	const lowerText = text.toLowerCase();
	const lowerUsername = botUsername.toLowerCase();
	if (lowerText.includes(`@${lowerUsername}`)) return true;
	if (entities) {
		for (const entity of entities) if (entity.type === "mention") {
			if (text.slice(entity.offset, entity.offset + entity.length).toLowerCase().includes(lowerUsername)) return true;
		}
	}
	return false;
}
function removeBotMention(params) {
	const { text, botUsername } = params;
	const lowerUsername = botUsername.toLowerCase();
	let result = text.replace(new RegExp(`@${lowerUsername}`, "gi"), "");
	result = result.replace(new RegExp(`@${botUsername}`, "g"), "");
	return result.replace(/\s+/g, " ").trim();
}
//#endregion
export { compileAllowlist, evaluateAccess, hasBotMention, removeBotMention, resolveAllowlistMatch, resolveAllowlistMatchSimple, resolveDmPolicy, resolveGroupPolicy };

//# sourceMappingURL=security.js.map