#pragma once
// REAPER-facing shell of Design View (D2): reads the live folder tree, runs
// ViewModeModel's pure planner, and applies the resulting flag / per-FX /
// lane writes. The .cpp is the sole REAPER-facing TU here (CLAUDE.md contract:
// only main.cpp defines the API pointers). See src/shell/view/CLAUDE.md for
// the enforced invariants (never touch master/mute, solo cached per mode and
// never lost, snapshot-based restore).

#include <string>

#include "core/view/view_mode_model.h"

// Forward-declared to keep this header SDK-free; the .cpp includes the real SDK header.
class ReaProject;

namespace reasampler {

// True while `proj`'s transport is playing or recording — the condition under which
// applyMode refuses a real mode switch. Exposed so the panel can paint the footer's
// [Arrange|Design] segments disabled BEFORE the click rather than only refusing on
// it. `proj` is passed through to GetPlayStateEx as-is; the SDK documents proj=0
// meaning "current project" for GetTrack but is silent on GetPlayStateEx, so
// `nullptr` meaning the current project here is inferred by analogy, not confirmed.
bool transportBlocksModeSwitch(ReaProject* proj);

// Snapshots each about-to-park track's flags into `model`, caches/clears the
// outgoing mode's solo state and replays the incoming mode's, runs planToggle,
// applies park/restore writes plus parent visibility flags, then sets the active
// mode. Wrapped in one Undo block. Returns false (no mutation) if `targetModeId`
// isn't registered, or if this is a real switch (target != active) while the
// transport is running. A reapply (target == active) is never gated and never
// touches solo. `proj` == nullptr means the current project.
bool applyMode(ViewModeModel& model, const std::string& targetModeId, ReaProject* proj);

// Splits any track visible in more than one mode while carrying its own media
// into fixed lanes (one managed lane per involved mode), assigns items, and
// records ownership in `model`. Never touches manual lanes. Runs planLaneMinting
// (model + tree aware); wraps the mutation in one Undo block when non-empty.
// Returns true if any lane was minted (repaint hint). `proj` == nullptr means
// the current project.
bool mintManagedLanes(ViewModeModel& model, ReaProject* proj);

// Recovers managed-lane ownership from durable P_LANENAME on project open —
// self-healing, without minting/reassigning anything and without touching
// membership. A lane without the managed prefix is left untouched (manual).
// `proj` == nullptr means the current project.
void reconcileManagedLanes(ViewModeModel& model, ReaProject* proj);

} // namespace reasampler
