import { getLogger } from "../../../logger.js";

//#region src/domains/run/services/PendingToolApprovalManager.ts
const logger = getLogger("PendingToolApprovalManager");
const APPROVAL_CLEANUP_INTERVAL_MS = 120 * 1e3;
const APPROVAL_TIMEOUT_MS = 600 * 1e3;
/**
* Manages pending tool approval requests during agent execution.
* Similar to ToolSessionManager but for approval workflows.
* Uses in-memory Map storage with automatic cleanup like OAuth PKCE store.
*/
var PendingToolApprovalManager = class PendingToolApprovalManager {
	static instance;
	pendingApprovals = /* @__PURE__ */ new Map();
	constructor() {
		setInterval(() => this.cleanupExpiredApprovals(), APPROVAL_CLEANUP_INTERVAL_MS);
	}
	static getInstance() {
		if (!PendingToolApprovalManager.instance) PendingToolApprovalManager.instance = new PendingToolApprovalManager();
		return PendingToolApprovalManager.instance;
	}
	/**
	* Create a new pending approval and return a promise that resolves with approval status
	*/
	async waitForApproval(toolCallId, toolName, args, conversationId, subAgentId) {
		return new Promise((resolve, reject) => {
			const timeoutId = setTimeout(() => {
				this.pendingApprovals.delete(toolCallId);
				resolve({
					approved: false,
					reason: `Tool approval timeout for ${toolName} (${toolCallId})`
				});
			}, APPROVAL_TIMEOUT_MS);
			const approval = {
				toolCallId,
				toolName,
				args,
				conversationId,
				subAgentId,
				createdAt: Date.now(),
				resolve,
				reject,
				timeoutId
			};
			this.pendingApprovals.set(toolCallId, approval);
			logger.info({
				toolCallId,
				toolName,
				conversationId,
				subAgentId
			}, "Tool approval request created, waiting for user response");
		});
	}
	/**
	* Approve a pending tool call
	*/
	approveToolCall(toolCallId) {
		const approval = this.pendingApprovals.get(toolCallId);
		if (!approval) {
			logger.warn({ toolCallId }, "Tool approval not found or already processed");
			return false;
		}
		logger.info({
			toolCallId,
			toolName: approval.toolName,
			conversationId: approval.conversationId
		}, "Tool approved by user, resuming execution");
		clearTimeout(approval.timeoutId);
		this.pendingApprovals.delete(toolCallId);
		approval.resolve({ approved: true });
		return true;
	}
	/**
	* Deny a pending tool call
	*/
	denyToolCall(toolCallId, reason) {
		const approval = this.pendingApprovals.get(toolCallId);
		if (!approval) {
			logger.warn({ toolCallId }, "Tool approval not found or already processed");
			return false;
		}
		logger.info({
			toolCallId,
			toolName: approval.toolName,
			conversationId: approval.conversationId,
			reason
		}, "Tool execution denied by user");
		clearTimeout(approval.timeoutId);
		this.pendingApprovals.delete(toolCallId);
		approval.resolve({
			approved: false,
			reason: `The user declined to run this tool. ${reason ? `Reason: ${reason}` : ""}`
		});
		return true;
	}
	/**
	* Clean up expired approvals (called by interval timer)
	*/
	cleanupExpiredApprovals() {
		const now = Date.now();
		let cleanedUp = 0;
		for (const [toolCallId, approval] of this.pendingApprovals) if (now - approval.createdAt > APPROVAL_TIMEOUT_MS) {
			clearTimeout(approval.timeoutId);
			this.pendingApprovals.delete(toolCallId);
			approval.resolve({
				approved: false,
				reason: "Tool approval expired"
			});
			cleanedUp++;
		}
		if (cleanedUp > 0) logger.info({ cleanedUp }, "Cleaned up expired tool approvals");
	}
	/**
	* Get current status for monitoring
	*/
	getStatus() {
		return {
			pendingApprovals: this.pendingApprovals.size,
			approvals: Array.from(this.pendingApprovals.values()).map((approval) => ({
				toolCallId: approval.toolCallId,
				toolName: approval.toolName,
				conversationId: approval.conversationId,
				subAgentId: approval.subAgentId,
				createdAt: approval.createdAt,
				age: Date.now() - approval.createdAt
			}))
		};
	}
};
const pendingToolApprovalManager = PendingToolApprovalManager.getInstance();

//#endregion
export { PendingToolApprovalManager, pendingToolApprovalManager };