import { bindFeishuConversation, unbindBySessionKey } from "./state/thread-bindings.js";
//#region extensions/feishu/src/subagent-hooks.ts
function normLower(v) {
	return typeof v === "string" ? v.trim().toLowerCase() : "";
}
function stripProviderPrefix(raw) {
	return raw.replace(/^(feishu|lark):/i, "").trim();
}
async function handleFeishuSubagentSpawning(event, ctx = {}) {
	if (!event?.threadRequested) return void 0;
	if (normLower(event?.requester?.channel) !== "feishu") return void 0;
	const accountId = String(event?.requester?.accountId ?? "").trim() || "default";
	const to = String(event?.requester?.to ?? "").trim();
	const threadId = event?.requester?.threadId != null && event.requester.threadId !== "" ? String(event.requester.threadId).trim() : "";
	const withoutProvider = to ? stripProviderPrefix(to) : "";
	const isChatTarget = /^(chat|group|channel):/i.test(withoutProvider);
	if (!to) return {
		status: "error",
		error: "Missing requester delivery target (to)"
	};
	if (isChatTarget && !threadId) return {
		status: "error",
		error: "Thread binding requires a topic threadId for chat targets"
	};
	bindFeishuConversation({
		accountId,
		conversationId: isChatTarget ? `${withoutProvider}|topic:${threadId}` : withoutProvider,
		parentConversationId: isChatTarget ? withoutProvider : void 0,
		targetSessionKey: String(event.childSessionKey),
		metadata: {
			requesterSessionKey: ctx.requesterSessionKey,
			deliveryTo: to,
			deliveryThreadId: threadId || void 0,
			agentId: event?.agentId,
			label: event?.label
		}
	});
	return {
		status: "ok",
		threadBindingReady: true
	};
}
function handleFeishuSubagentEnded(event) {
	const accountId = String(event?.accountId ?? "").trim() || "default";
	const sk = String(event?.targetSessionKey ?? "").trim();
	if (!sk) return;
	unbindBySessionKey(accountId, sk);
}
//#endregion
export { handleFeishuSubagentEnded, handleFeishuSubagentSpawning };

//# sourceMappingURL=subagent-hooks.js.map