#pragma once
// view — the REAPER-facing shell of the Design View feature (Phase D2). It is the
// mirror of the capture shell: the ViewModeModel (pure, D1) holds the mode/
// membership/snapshot state and emits the toggle plan; this shell reads the live
// project's folder tree, snapshots the tracks it is about to park, runs the model's
// planner, and applies the resulting flag + per-FX-offline writes to REAPER.
//
// It includes view_mode_model (pure) but NO REAPER headers — the .cpp is the one
// REAPER-facing translation unit (CLAUDE.md §contract: only main.cpp defines the
// API pointers; every other .cpp gets them extern). Callers (persist, actions)
// depend on this seam without dragging the SDK into their include sites.
//
// Hard invariants this shell enforces (CONTEXT.md §Design View, precision
// invariants) — verified in self-review, never crossed:
//   * Never touches the master track's visibility (SDK forbids B_SHOWINTCP/
//     B_SHOWINMIXER on master); the master is never a node in the tree.
//   * Never reads or writes B_MUTE / I_SOLO on any track.
//   * Manages ALL leaves via the mode system: an untagged leaf is an Arrange member,
//     so it is fully parked in non-Arrange modes and restored in Arrange, identically
//     to a tagged leaf. show-both is the always-visible escape; parents are
//     visibility-only (derived); the master is never touched.
//   * Snapshots every to-be-parked track's prior flags BEFORE parking, storing
//     them into the model so restore is faithful and survives a save-while-parked.

#include <string>

#include "view_mode_model.h"

// REAPER's opaque project handle. Forward-declared to keep this header SDK-free;
// the .cpp includes reaper_plugin_functions.h and sees the real class.
class ReaProject;

namespace reasampler {

// Applies `targetModeId` to the live project `proj`:
//   1. Reads the arrange-ordered track list, builds the FolderTree from
//      I_FOLDERDEPTH (via the pure buildFolderTree helper).
//   2. Runs model.planToggle(tree, targetModeId).
//   3. For each track about to be PARKED: snapshots its current B_SHOWINTCP /
//      B_SHOWINMIXER / B_MAINSEND / I_FXEN and per-FX offline state, stores the
//      snapshot into the model, THEN applies the park writes (expanding the
//      per-FX offline loop from TrackFX_GetCount, which the pure plan leaves empty).
//   4. For each track to RESTORE: applies the plan's snapshot-sourced flag + per-FX
//      offline writes verbatim.
//   5. For each PARENT (folder) node: drives B_SHOWINTCP / B_SHOWINMIXER to 1 if the
//      parent is in model.visibleTracks(tree, targetModeId), else 0 — derived from
//      membership, never parked/snapshotted. Only the two visibility flags.
//   6. Sets the model's active mode to `targetModeId`.
// All track mutations are wrapped in Undo_BeginBlock2 / Undo_EndBlock2.
//
// Returns false (no mutation, active mode unchanged) if `targetModeId` is not a
// registered mode. `proj` may be nullptr to mean REAPER's current project.
bool applyMode(ViewModeModel& model, const std::string& targetModeId, ReaProject* proj);

// Mints managed fixed lanes for any track in `proj` that is VISIBLE IN MORE THAN ONE
// MODE while carrying its own media, and assigns each item to its mode's managed lane
// (Phase D2 Wave 3; visibility trigger added by the folder-media fix).
//   1. Enumerates every track + its items; resolves each item's mode from the model's
//      membership (untagged ⇒ Arrange) and reads whether it currently sits on a MANUAL
//      lane (exempt). Builds the FolderTree (I_FOLDERDEPTH) so derived visibility counts.
//   2. Runs the pure planLaneMinting decision (model + tree aware). A track visible in
//      exactly one mode is left whole-track-parked (D1) — NOT lane-split. A track visible
//      in >1 mode while carrying own media splits: its own items span modes, OR it is a
//      content-bearing folder derived-visible across modes. show-both tracks never split.
//   3. For each track that must split: enables fixed-lane mode (I_FREEMODE=2), ensures
//      enough fixed lanes (I_NUMFIXEDLANES), stamps each managed lane's durable name
//      (P_LANENAME:n), records the lane MANAGED-for-its-mode in the model's ownership
//      index, and assigns each managed-eligible item to its mode's lane (I_FIXEDLANE).
//      Manual lanes and the items on them are NEVER minted-over or reassigned.
//   4. Reapplies the active mode's lane visibility so the just-minted lanes take their
//      correct play/show state immediately (the active mode's lane plays; others hide).
// The whole structural mutation is wrapped in ONE Undo_BeginBlock2/EndBlock2 — but only
// when the plan is non-empty (no undo point for a tick that mints nothing).
//
// Returns true if any lane was minted this call (⇒ the caller may want a repaint).
// `proj` may be nullptr to mean REAPER's current project. READ of the membership index
// only; the sole model mutation is recording new managed-lane ownership.
bool mintManagedLanes(ViewModeModel& model, ReaProject* proj);

// Reconciles the model's lane-ownership index against the live project's lanes on
// project open (Phase D2 Wave 3). REAPER's durable P_LANENAME is the source of truth for
// lane identity across sessions (design point #2): a lane whose name carries the managed
// prefix is tool-managed and owned by the mode encoded in that name. This walks every
// track's lanes and records each managed-named lane MANAGED-for-its-mode in the index —
// self-healing a saved project's classification WITHOUT re-minting (it never creates a
// lane, changes I_FREEMODE/I_NUMFIXEDLANES, or reassigns an item) and WITHOUT mass-
// tagging (it never touches membership). A lane without the managed prefix is left
// untouched (manual by default). Reload's active-mode lane visibility is then reapplied
// by the caller's applyMode, mirroring D1's reapply-on-open.
//
// `proj` may be nullptr to mean REAPER's current project. The only model mutation is
// recording managed ownership recovered from durable lane names.
void reconcileManagedLanes(ViewModeModel& model, ReaProject* proj);

} // namespace reasampler
