//#region extensions/telegram/src/access-control.ts
/**
* Normalize allowFrom list to structured format
*/
function normalizeAllowFrom(list) {
	const entries = (list ?? []).map((value) => String(value).trim()).filter(Boolean);
	const hasWildcard = entries.includes("*");
	const ids = entries.filter((value) => value !== "*").map((value) => value.replace(/^(telegram|tg):/i, "")).filter((value) => /^\d+$/.test(value));
	return {
		entries: ids,
		hasWildcard,
		hasEntries: ids.length > 0
	};
}
/**
* Normalize allowFrom combining config and store
*/
function normalizeAllowFromWithStore(params) {
	return normalizeAllowFrom([...params.allowFrom ?? [], ...params.storeAllowFrom ?? []].map((value) => String(value).trim()).filter(Boolean));
}
/**
* Check if sender is allowed based on normalized allowFrom
*/
function isSenderAllowed(params) {
	const { allow, senderId } = params;
	if (!allow.hasEntries) return true;
	if (allow.hasWildcard) return true;
	if (senderId && allow.entries.includes(senderId)) return true;
	return false;
}
/**
* Resolve sender allowlist match
*/
function resolveSenderAllowMatch(params) {
	const { allow, senderId } = params;
	if (allow.hasWildcard) return {
		allowed: true,
		matchKey: "*",
		matchSource: "wildcard"
	};
	if (!allow.hasEntries) return { allowed: false };
	if (senderId && allow.entries.includes(senderId)) return {
		allowed: true,
		matchKey: senderId,
		matchSource: "id"
	};
	return { allowed: false };
}
/**
* Evaluate base group access (group/topic enabled, allowFrom override)
*/
function evaluateGroupBaseAccess(params) {
	if (!params.isGroup) return { allowed: true };
	if (params.groupConfig?.enabled === false) return {
		allowed: false,
		reason: "group-disabled"
	};
	if (params.topicConfig?.enabled === false) return {
		allowed: false,
		reason: "topic-disabled"
	};
	if (params.hasGroupAllowOverride) {
		const senderId = params.senderId ?? "";
		if (!senderId) return {
			allowed: false,
			reason: "unauthorized"
		};
		if (!isSenderAllowed({
			allow: params.effectiveGroupAllow,
			senderId,
			senderUsername: params.senderUsername
		})) return {
			allowed: false,
			reason: "unauthorized"
		};
	}
	return { allowed: true };
}
/**
* Evaluate group policy access (open/disabled/allowlist)
*/
function evaluateGroupPolicyAccess(params) {
	const { isGroup, groupPolicy, effectiveGroupAllow, senderId, senderUsername } = params;
	if (!isGroup) return {
		allowed: true,
		groupPolicy
	};
	if (groupPolicy === "disabled") return {
		allowed: false,
		reason: "policy-blocked",
		groupPolicy
	};
	if (groupPolicy === "open") return {
		allowed: true,
		groupPolicy
	};
	if (groupPolicy === "allowlist") {
		if (!effectiveGroupAllow.hasEntries && !effectiveGroupAllow.hasWildcard) return {
			allowed: false,
			reason: "policy-blocked",
			groupPolicy
		};
		if (!isSenderAllowed({
			allow: effectiveGroupAllow,
			senderId,
			senderUsername
		})) return {
			allowed: false,
			reason: "policy-blocked",
			groupPolicy
		};
	}
	return {
		allowed: true,
		groupPolicy
	};
}
/**
* Resolve group policy from config hierarchy
*/
function resolveGroupPolicy(params) {
	return params.topicConfig?.groupPolicy ?? params.groupConfig?.groupPolicy ?? params.accountGroupPolicy ?? params.defaultGroupPolicy ?? "open";
}
/**
* Resolve requireMention from config hierarchy
*/
function resolveRequireMention(params) {
	return params.topicConfig?.requireMention ?? params.groupConfig?.requireMention ?? params.defaultRequireMention ?? true;
}
/**
* Build mention regexes for bot username
*/
function buildMentionRegex(botUsername) {
	const escaped = botUsername.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
	return new RegExp(`@${escaped}`, "i");
}
/**
* Check if message has bot mention (text or entity)
*/
function hasBotMention(params) {
	const { botUsername, text = "", entities = [] } = params;
	const botUsernameLower = botUsername.toLowerCase();
	if (text.toLowerCase().includes(`@${botUsernameLower}`)) return true;
	for (const ent of entities) if (ent.type === "mention") {
		if (text.slice(ent.offset, ent.offset + ent.length).toLowerCase() === `@${botUsernameLower}`) return true;
	}
	return false;
}
/**
* Remove bot mention from text
*/
function removeBotMention(text, botUsername) {
	const botUsernameLower = botUsername.toLowerCase();
	return text.replace(new RegExp(`@${botUsernameLower}\\s*`, "gi"), "").trim();
}
//#endregion
export { buildMentionRegex, evaluateGroupBaseAccess, evaluateGroupPolicyAccess, hasBotMention, isSenderAllowed, normalizeAllowFrom, normalizeAllowFromWithStore, removeBotMention, resolveGroupPolicy, resolveRequireMention, resolveSenderAllowMatch };

//# sourceMappingURL=access-control.js.map