#pragma once
// The in-flight realtime-capture state machine + globals. A realtime record spans
// many timer ticks (it takes end-start wall-clock seconds and must not block
// REAPER's UI): the action starts it (RunCaptureRealtimeTrack -> g_rtBackend.begin),
// OnTimer drives it here (DriveRealtimeCapture -> g_rtBackend.tick) each tick until a
// terminal verdict, then the handle is cleared.
//
// The three globals are extern rather than wrapped so the timer's idle fast path
// stays a single pointer test at the call site — load-bearing:
//     if (capture::g_rtCapture) capture::DriveRealtimeCapture(g_session);
// No per-tick cross-TU call, no accessor indirection, when nothing is recording.
//
// The .cpp includes reaper_plugin_functions.h WITHOUT REAPERAPI_IMPLEMENT
// (main.cpp owns the API pointers).

#include "shell/capture/capture_realtime_shell.h"  // RealtimeRecordBackend / RealtimeCaptureHandle

namespace reasampler {
class ReaSamplerSession;
}

namespace reasampler::capture {

// Non-null g_rtCapture == a capture is in progress: used to reject a second one,
// drive the per-tick advance, and abort on project switch / unload.
extern RealtimeRecordBackend  g_rtBackend;
extern RealtimeCaptureHandle  g_rtCapture;

// The project the in-flight capture belongs to (opaque, compare-only) — lets
// OnTimer detect a project switch mid-capture and abort+restore rather than leak the
// temp track/arm/transport across projects. Meaningful only when g_rtCapture != nullptr.
extern ReaProject* g_rtCaptureProject;

// Commits a finished realtime capture (a Done tick/abort with an Ok result): adds
// the Sample to the active bank, records the owned file, bumps the generation,
// persists + MarkProjectDirty. On a non-Ok result, logs the failure only.
void CommitRealtimeResult(ReaSamplerSession& session, const CaptureResult& res);

// Advances any in-flight realtime capture one tick. Detects a project switch
// mid-capture and aborts+restores so the capture never leaks across projects.
// Called from OnTimer before session.poll().
void DriveRealtimeCapture(ReaSamplerSession& session);

// Unload teardown: abort any in-flight capture while the API pointers are still
// live — finalize-or-abort + restore so we never leave a temp track, an armed
// track, or an altered transport/cursor behind. Commits whatever was captured
// (best effort) before tearing down. No-op when idle.
void AbortRealtimeCaptureForUnload(ReaSamplerSession& session);

}  // namespace reasampler::capture
