#pragma once
// The transient silencing a ranged ITEM render needs: REAPER's selected-tracks
// source prints everything upstream of the track — its folder children and its
// receives — which an item capture must not hear. Stack RAII, restored on every
// path.

#include <vector>

#include "shell/capture/capture.h"  // MediaTrack fwd

namespace reasampler::capture {

// RAII: for one render, cuts every route by which audio that is not `track`'s own
// items reaches `track` — each direct folder child's send-to-parent (B_MAINSEND) and
// each of `track`'s own receives (that receive's B_MUTE). Both are per-route levers,
// so a child or a sender keeps its FX, its fader, and its routing elsewhere intact.
// Every touched value is restored on every exit path; nothing is created or removed,
// so the project is unchanged afterwards (non-destructive invariant).
//
// An overlapping item on `track` ITSELF is deliberately NOT isolated — the reason is
// in src/shell/capture/CLAUDE.md.
class UpstreamIsolation
{
public:
    explicit UpstreamIsolation(MediaTrack* track);
    ~UpstreamIsolation();

    UpstreamIsolation(const UpstreamIsolation&)            = delete;
    UpstreamIsolation& operator=(const UpstreamIsolation&) = delete;

private:
    struct ChildSend {
        MediaTrack* track    = nullptr;
        double      mainSend = 0.0;  // original B_MAINSEND
    };

    MediaTrack*            track_ = nullptr;
    std::vector<ChildSend> children_;
    std::vector<double>    receiveMutes_;  // original B_MUTE, indexed by receive index
};

}  // namespace reasampler::capture
