import { __esmMin } from "../../_virtual/_rolldown/runtime.js";
import { init_account_id, normalizeAccountId } from "./account-id.js";
import { init_session_key, sanitizeSegment } from "./session-key.js";
//#region src/routing/bindings.ts
/**
* Simple glob match (`*` only).
*/
function globMatch(pattern, value) {
	if (pattern === "*") return true;
	const regexPattern = pattern.split("*").map((segment) => segment.replace(/[.+?^${}()|[\]\\]/g, "\\$&")).join(".*");
	return new RegExp(`^${regexPattern}$`, "i").test(value);
}
/**
* Whether `input` satisfies `match`.
*/
function matchesBinding(input, match) {
	if (input.channel.toLowerCase() !== match.channel.toLowerCase()) return false;
	if (match.accountId && match.accountId !== "*") {
		const inputAccountId = normalizeAccountId(input.accountId);
		if (!globMatch(normalizeAccountId(match.accountId), inputAccountId)) return false;
	}
	if (match.peerKind) {
		if ((input.peerKind ?? "").toLowerCase() !== match.peerKind.toLowerCase()) return false;
	}
	if (match.peerId) {
		const inputPeerId = (input.peerId ?? "").toLowerCase();
		if (!globMatch(match.peerId.toLowerCase(), inputPeerId)) return false;
	}
	if (match.guildId) {
		if ((input.guildId ?? "").toLowerCase() !== match.guildId.toLowerCase()) return false;
	}
	if (match.teamId) {
		if ((input.teamId ?? "").toLowerCase() !== match.teamId.toLowerCase()) return false;
	}
	if (match.memberRoleIds && match.memberRoleIds.length > 0) {
		const inputRoleIds = new Set((input.memberRoleIds ?? []).map((r) => r.toLowerCase()));
		if (!match.memberRoleIds.some((role) => inputRoleIds.has(role.toLowerCase()))) return false;
	}
	return true;
}
/**
* Parse binding rules from config JSON.
*/
function parseBindingRules(config) {
	const rules = [];
	if (!config?.bindings || !Array.isArray(config.bindings)) return rules;
	for (const [index, raw] of Object.entries(config.bindings)) {
		const rule = parseBindingRule(raw, Number(index));
		if (rule) rules.push(rule);
	}
	return rules.sort((a, b) => b.priority - a.priority);
}
/**
* Parse one raw binding object or return null.
*/
function parseBindingRule(raw, index) {
	if (!raw || typeof raw !== "object") return null;
	const match = raw.match;
	if (!match || typeof match !== "object" || !match.channel) return null;
	return {
		id: raw.id ?? `binding-${index}`,
		agentId: sanitizeSegment(raw.agentId) || "main",
		priority: typeof raw.priority === "number" ? raw.priority : index,
		match: {
			channel: match.channel.toLowerCase(),
			accountId: match.accountId,
			peerKind: match.peerKind,
			peerId: match.peerId,
			guildId: match.guildId,
			teamId: match.teamId,
			memberRoleIds: Array.isArray(match.memberRoleIds) ? match.memberRoleIds : void 0
		},
		enabled: raw.enabled !== false
	};
}
/**
* Pick the first matching rule or fall back to `defaultAgentId`.
*/
function resolveRoute(input, rules, defaultAgentId = "main") {
	const normalizedAccountId = normalizeAccountId(input.accountId);
	for (const rule of rules) {
		if (rule.enabled === false) continue;
		if (matchesBinding(input, rule.match)) return {
			agentId: rule.agentId,
			accountId: normalizedAccountId,
			matchedRule: rule,
			matchedBy: "binding"
		};
	}
	return {
		agentId: defaultAgentId,
		accountId: normalizedAccountId,
		matchedBy: "default"
	};
}
var init_bindings = __esmMin((() => {
	init_account_id();
	init_session_key();
}));
//#endregion
init_bindings();
export { globMatch, init_bindings, matchesBinding, parseBindingRule, parseBindingRules, resolveRoute };

//# sourceMappingURL=bindings.js.map