/**
 * Strip the approval-status suffixes the agent transcript appends to a tool
 * call's function name. Lives here (not in AiResponseMessage) so ApprovalCard
 * can use it without an import cycle.
 */
export function cleanToolFunctionName(name) {
    return (name || "")
        .replace(" (approved)", "")
        .replace(" (rejected)", "")
        .replace(" (pending approval)", "")
        .trim();
}
/**
 * Return the pending approvals that belong to this message group, i.e. the
 * intersection of the session-wide pending list with the group's tool-call
 * ids, preserving `allPendingApprovals` order. The approval card renders (and
 * acts) on exactly this group-local set, so a card can never appear on a
 * historical group whose tool calls are all resolved.
 */
export function selectGroupPendingApprovals(toolCalls, allPendingApprovals) {
    if (!toolCalls ||
        toolCalls.length === 0 ||
        allPendingApprovals.length === 0) {
        return [];
    }
    const groupIds = new Set(toolCalls
        .map((tc) => ("toolCallId" in tc ? tc.toolCallId : tc.id))
        .filter(Boolean));
    return allPendingApprovals.filter((pending) => groupIds.has(pending.toolCallId));
}
/**
 * Decide whether a message group's batch-approval bar should be shown.
 *
 * The bar's visibility MUST agree with the session-wide pending count it
 * renders. That count comes from `selectPendingApprovals`
 * (see agentSessionSelectors.ts), which excludes any tool call that has already
 * completed (`isCompleted`) or been resolved. By deriving "does this group have
 * a pending approval?" from that same `allPendingApprovals` list — instead of
 * re-inspecting local tool-call fields — the gate and the count can never
 * diverge.
 *
 * The previous heuristic (`requiresApproval` set AND name lacking
 * "(approved)"/"(rejected)") ignored completion, so a tool call that finished
 * via any path other than a local Approve click — autonomous mode, an
 * auto-approve policy, server-side/other-tab execution, or a run resync — kept
 * its sticky `requiresApproval` metadata and its un-renamed function name. That
 * made the gate fire while the count was 0, leaking an empty
 * "Approve All (0)" / "Reject All (0)" bar onto an already-finished tool call.
 */
export function groupHasPendingApproval(toolCalls, allPendingApprovals) {
    return selectGroupPendingApprovals(toolCalls, allPendingApprovals).length > 0;
}
//# sourceMappingURL=toolCallApprovalGate.js.map