#pragma once
// sample_map — PURE mapping logic for the S4 Tier-0 instrument: turn the live
// "reasampler" bank ext-state + a decoded WAV into the plain data the sampler core
// plays, and (de)serialize the instance's selected-sample choice for VST3 component
// state. NO VST3, NO REAPER, NO SWELL, NO vendor/ includes at the boundary — the
// mirror of capture_paths / wav_trim / bridge_marshal splitting the fiddly, testable
// arithmetic out of a host-facing shell.
//
// WHY IT EXISTS (S4 seams). The instrument reads the bank over the live-state seam
// (the "banks" ext-state blob) and the audio over the file seam (the on-disk WAV).
// Both of those raw inputs cross the bridge/file boundary in the shell; everything
// after — parse the bank with the SHARED bank_model/bank_book JSON path (NOT a second
// parser; the S1 spike's string-scan reader is retired), pick the selected sample,
// downmix its decoded PCM to the core's mono contract, and build the Tier-0 chromatic
// Keymap — is pure and unit-tested here.
//
// It links bank_book (the shared BankBook::deserialize) and wav_trim (the shared
// 32-bit-float WAV parse — no third WAV reader) and sampler_core (the Keymap /
// SampleData it produces). All three are pure; this stays pure.

#include <cstdint>
#include <optional>
#include <string>
#include <vector>

#include "bank_book.h"       // BankBook::deserialize (shared bank JSON parse)
#include "sampler_core.h"    // Keymap, SampleData, SampleLoop
#include "wav_trim.h"        // parseWavLayout, extractFloatFrames (shared WAV parse)

namespace reasampler {

// The bank sample this instance is bound to, distilled from the live "banks" blob:
// the project-relative WAV path the file seam must resolve+decode, plus the S2 bank
// intrinsics the core repitches / loops by. A pure value — no host, no PCM yet.
struct SelectedSample {
    std::string relativePath;   // project-relative; the shell resolves it (M4 convention)
    int rootNote = 60;          // S2 intrinsic; defaults to middle C when the bank left it empty
    SampleLoop loop;            // S2 intrinsic; hasLoop=false when the bank left it empty
};

// Resolve the bound sample from the live bank blob. `banksJson` is the raw "banks"
// ext-state value the bridge read (may be empty / malformed — an unsaved or pre-bank
// project). `sampleId` is this instance's stored selection.
//
// Precedence, all pure:
//   * empty / malformed banksJson            -> nullopt (nothing to play)
//   * sampleId empty                         -> nullopt (NO selection -> silence)
//   * sampleId names a sample in ANY bank    -> that sample (searched pool + named)
//   * sampleId set but not found (stale)     -> nullopt (the sample was deleted/moved;
//                                                the editor returns to the empty state)
//
// POLICY REVERSAL (S10, 2026-07-26 — supersedes the S4 first-sample fallback). A fresh
// instance with no stored selection resolves to nullopt (SILENCE), NOT the bank's first
// sample: the metric is time-to-first-note via an explicit pick, and a mystery auto-play
// of sample #1 was the anti-pattern. A stale stored id (no longer resolves) ALSO returns
// nullopt rather than silently substituting a different sample — the editor reflects the
// missing selection with its "pick a capture" empty state instead of masking it.
std::optional<SelectedSample> selectSample(const std::string& banksJson,
                                           const std::string& sampleId);

// One entry in the capture browser's card list: the stable id + display name plus the S2
// intrinsics + bank the browser draws as a card (peak thumbnail + name + root/key badge,
// filterable by bank). Peaks are NOT here — they are computed shell-side from the decoded
// PCM (the `Sample` metadata carries no envelope; see reasampler_editor's thumbnail cache,
// the mirror of bank_panel::thumbnailFor). This carries only what the bank blob already
// holds: the metadata the card badge + bank filter need. Pure projection over the shared
// parse — the UI never parses JSON itself.
//
//  - rootNote:  the S2 rootNote intrinsic when the bank set it (nullopt otherwise — the
//               badge shows "root: —" / no root, never a guessed value).
//  - key:       the optional human musical key label ("F#m"), when the bank set it.
//  - bankId:    the id of the bank this sample lives in (the bank filter matches on it).
struct SampleChoice {
    std::string id;
    std::string displayName;
    std::optional<int> rootNote;
    std::optional<std::string> key;
    std::string bankId;
};
std::vector<SampleChoice> listSamples(const std::string& banksJson);

// One bank the filter tab strip offers: its stable id + display name, in ordinal order
// (pool first). The browser prepends an "All" tab (no id) shell-side. Empty for an empty /
// malformed blob. Pure projection over the shared parse.
struct BankChoice {
    std::string id;
    std::string displayName;
};
std::vector<BankChoice> listBanks(const std::string& banksJson);

// Downmix interleaved float frames (the shape wav_trim::extractFloatFrames yields:
// [f0c0,f0c1,...,f1c0,...]) to the core's MONO contract by AVERAGING channels per
// frame. `channelCount` is the interleave stride (>= 1). CHANNEL POLICY (Tier 0,
// documented + surfaced): the S3 core is mono-per-sample by design; bank WAVs preserve
// their source channel count, so a stereo (or N-channel) capture is folded to a single
// mono stream here by an equal-weight average. Averaging (not "take L", not summing) is
// the least-surprising, no-clip default — a centered mono source stays unity, and a
// hard-panned source is attenuated rather than silenced or doubled. Empty / zero-stride
// in -> empty out. Pure.
std::vector<AudioSample> downmixToMono(const std::vector<AudioSample>& interleaved,
                                       int channelCount);

// Deinterleave one channel (`which`, 0-based) out of interleaved frames. `channelCount` is
// the interleave stride (>= 1); `which` is clamped to a valid channel (a request past the
// source's last channel reads the last channel, so a mono source asked for channel 1 yields
// channel 0 again — the dual-mono building block). Empty / zero-stride in -> empty out. Pure.
std::vector<AudioSample> extractChannel(const std::vector<AudioSample>& interleaved,
                                        int channelCount, int which);

// --- Stored (wall-clock SECONDS) per-zone play params -------------------------
//
// DOMAIN SPLIT (S12 remediation — Daniel's ruling: no hardcoded sample rate in the program).
// The instrument stores and edits WALL-CLOCK performance times as SECONDS, rate-free; the
// engine (sampler_core's ZonePlayParams, on SampleData) receives FRAMES resolved from the
// LIVE sample rate at keymap build. AHDSR (A/H/D/S/R) and the AD pitch envelope (attack/decay)
// are wall-clock — the voice advances them once per OUTPUT frame — so they live here in seconds.
// Quantities anchored to the source file's timeline (start point, loop points, Trigger %-length
// and its fades — the fades anchor to the source-frame read offset, PLAN.md §S15) stay in source
// frames / fractions and are carried through unchanged (TriggerParams is reused verbatim).
//
// The stored AHDSR times (seconds). sustainLevel is dimensionless (0..1), not a time.
struct AdsrSeconds {
    double attackSeconds  = 0.003;  // tier-0 default
    double holdSeconds    = 0.0;
    double decaySeconds   = 0.0;
    double sustainLevel   = 1.0;
    double releaseSeconds = 0.060;  // tier-0 default
};

// The stored AD pitch-envelope times (seconds). enabled + peakSemitones are dimensionless.
struct PitchEnvSeconds {
    bool   enabled       = false;
    double attackSeconds = 0.0;
    double decaySeconds  = 0.0;
    double peakSemitones = 0.0;    // signed depth at the peak
};

// The stored per-zone play bundle: wall-clock times in SECONDS, source-timeline quantities in
// frames/fractions (TriggerParams). This is the instrument-owned (D-B), serialized, editor-facing
// representation — distinct from sampler_core's engine-facing ZonePlayParams (frames). The keymap
// builders resolve this to a frame-domain ZonePlayParams against the live sample rate.
struct ZonePlaySeconds {
    PlayMode        playMode = PlayMode::Gate;
    AdsrSeconds     adsr;                                  // Gate: AHDSR (seconds)
    TriggerParams   trigger;                              // Trigger: %-length + fades (source frames)
    PitchEngine     pitchEngine = kDefaultPitchEngine;    // product default: Preserve (S16-F1)
    PitchEnvSeconds pitchEnv;                             // AD pitch modulation (seconds), off by default
};

// Resolve a stored seconds bundle to the engine's frame-domain ZonePlayParams against a live
// sample rate (frames = round(seconds * rate)). Source-timeline fields (trigger, engine, mode,
// peak, enabled) carry through unchanged. `sampleRate` must be > 0 (the caller guards this).
ZonePlayParams resolvePlay(const ZonePlaySeconds& stored, int sampleRate);

// Build the Tier-0 chromatic keymap for one decoded sample: one zone spanning the whole
// keyboard, repitched from `rootNote`, looped per `loop`. The single-sample degenerate case
// (Keymap::singleSampleChromatic) with the S2 intrinsics threaded in. `frames` is channel 0
// (mono, or L); `framesR` is channel 1 (R) — pass EMPTY for a mono sample (the default),
// which yields a mono SampleData byte-identical to the pre-S7 build. A `framesR` whose length
// mismatches `frames` is dropped (SampleData::channelCount() falls back to mono), so a bad
// pair never half-plays. `sampleRate` is the WAV's rate.
// `play` carries the S15/S16 per-zone play params (SECONDS) for the single-capture path; it
// defaults to the PRODUCT defaults (Gate + tier-0 AHDSR seconds + Preserve engine, S16-F1) so a
// picked single capture plays under the same default engine as a zone would. This function
// resolves the wall-clock seconds to frames against `sampleRate` before stamping the SampleData.
Keymap buildTier0Keymap(std::vector<AudioSample> frames, int sampleRate,
                        int rootNote, const SampleLoop& loop,
                        std::vector<AudioSample> framesR = {},
                        const ZonePlaySeconds& play = ZonePlaySeconds{});

// --- Performance map (Tier 1, D-B: the instrument's OWN state) ---------------
//
// The performance map is the keymap the user authors IN the instrument: several bank
// samples zoned across the keyboard, each with a key range and a root note. It is a
// PERFORMANCE CHOICE (D-B), so it lives in the instrument (VST3 component state), never
// written back to the bank. Root note per zone is SEEDED from the S2 bank intrinsic but
// OVERRIDABLE here — the override lives on the zone, never on `Sample`.
//
// Pure value type: it names bank samples by id (the stable seam key) and holds no PCM.
// The shell resolves each id's WAV over the file seam and decodes it; the pure zone-build
// stitches the decoded frames + this map into a sampler_core Keymap.

// One authored zone: a bank sample mapped to an inclusive [lowNote, highNote] key range,
// with an optional root-note override. rootOverride absent -> repitch from the bank
// sample's own S2 rootNote intrinsic (or middle C when the bank left it empty).
//
// S11 loop/start overrides (instrument-owned, D-B — mirror of rootOverride): the sustain
// loop and the initial read position are FACTS about the file (S2 bank intrinsics), but the
// instrument may override them per zone WITHOUT writing back to the bank. loopOverride wins
// over the bank's S2 loop intrinsic when set; startPoint sets the voice's initial read frame
// (absent -> frame 0). Both are seeded from the bank intrinsic in the editor and stored here;
// resolvePerformance folds override-beats-intrinsic into the effective ResolvedZone.
struct PerformanceZone {
    std::string sampleId;             // bank sample id this zone plays
    int lowNote = 0;                  // inclusive
    int highNote = 127;               // inclusive
    std::optional<int> rootOverride;  // instrument-owned override; absent -> bank intrinsic
    std::optional<SampleLoop> loopOverride;  // instrument-owned sustain loop; absent -> bank intrinsic
    std::optional<std::int64_t> startPoint;  // instrument-owned initial read frame; absent -> 0

    // S-VIEW-6 key-tracking scalar (instrument-owned, D-B — mirror of rootOverride): how far
    // playback pitch tracks the keyboard around the root. 1.0 (100%) is standard 12-tone-ET (the
    // DEFAULT; a pre-S-VIEW-6 blob with no keyTrack tail lifts to exactly 1.0, so already-saved
    // instances are bit-identical); 0.0 = no tracking (every key plays root pitch); 2.0 = double.
    // NOT flag-gated — always present in the CURRENT payload (v6). Carried through to KeyZone by
    // resolvePerformance and applied in keyTrackedRatio inside BOTH repitch engines.
    double keyTrack = 1.0;

    // S15/S16 per-zone play parameters (play mode + AHDSR + Trigger %-length/fades; pitch
    // engine + AD pitch envelope). Instrument-owned (D-B), never a bank fact — mirror of the
    // loop/start overrides. Wall-clock times are stored in SECONDS (rate-free); the keymap build
    // resolves them to frames at the live sample rate. Defaults to the PRODUCT defaults for a NEW
    // zone: Gate play mode, tier-0 AHDSR seconds (0.003 attack / 0.060 release), hold 0, no fades,
    // PRESERVE pitch engine (S16-F1), pitch env off. An older zone-payload blob (no S15/S16 tail)
    // lifts to exactly these defaults on read (see the PAYLOAD versioning).
    ZonePlaySeconds play;
};

// The instrument's performance map: an ordered list of zones. Order is authoritative for
// overlap resolution (OVERLAP POLICY: first zone in order wins, mirroring the S3 core's
// first-match Keymap::resolve — overlaps are neither rejected nor clamped, the earlier
// zone simply takes the contested keys; documented, deterministic).
struct PerformanceMap {
    std::vector<PerformanceZone> zones;

    bool empty() const { return zones.empty(); }
};

// One resolved zone ready for the shell to decode + the pure build to stitch: the bank
// sample's project-relative WAV path (file seam), the EFFECTIVE root note (override beats
// bank intrinsic beats middle-C default), the loop intrinsic, and the key range. Distinct
// from PerformanceZone (which names an id) — this is the id resolved against the live bank.
struct ResolvedZone {
    std::string relativePath;   // project-relative; the shell resolves + decodes it
    int lowNote = 0;
    int highNote = 127;
    int rootNote = 60;          // effective: override, else bank intrinsic, else 60
    double keyTrack = 1.0;      // S-VIEW-6 key-tracking scalar, carried from PerformanceZone (1.0 = 100% ET)
    SampleLoop loop;            // effective: loopOverride, else bank S2 intrinsic (S11)
    std::int64_t startFrame = 0;  // effective initial read frame: startPoint, else 0 (S11)
    ZonePlaySeconds play;  // S15/S16 per-zone play params (SECONDS; resolved to frames at build)
};

// The result of resolving a performance map against the live bank blob. `zones` are the
// zones whose sampleId still resolves to a bank sample, IN MAP ORDER (so overlap-order is
// preserved). `droppedSampleIds` are the ids that no longer resolve (STALE-ID POLICY: a
// zone naming a deleted/moved-out sample is DROPPED cleanly — not an error, not silence
// for the whole map — and its id is reported here so the editor can flag/prune it).
struct ResolvedPerformance {
    std::vector<ResolvedZone> zones;
    std::vector<std::string> droppedSampleIds;
};

// Resolve a performance map against the live "banks" ext-state blob. Pure: shared
// bank_book parse, no host, no PCM. Each zone's sampleId is looked up across every bank
// (pool + named); a hit yields a ResolvedZone with the effective root note (rootOverride,
// else the sample's S2 rootNote, else 60) and the sample's loop intrinsic; a miss appends
// the id to droppedSampleIds. Empty/malformed blob or empty map -> empty result (the shell
// then falls back to Tier-0 — see reloadFromBank).
ResolvedPerformance resolvePerformance(const std::string& banksJson,
                                       const PerformanceMap& map);

// Build a zoned Keymap from resolved zones + their decoded mono PCM. `decoded[i]` is the
// downmixed frames + sample rate for `zones[i]` (same length + order as `zones`). One
// SampleData per zone (Tier 1: one sample per key-region; a sample used by two zones is
// decoded twice — acceptable at this tier, the shell may dedup by path later). Zone order
// is preserved so first-match overlap resolution matches the map's authored order. A zone
// whose decoded frames are empty is SKIPPED (an unreadable WAV drops the zone, not the
// map). Empty zones in -> empty Keymap (silence).
struct DecodedZonePcm {
    std::vector<AudioSample> monoFrames;   // channel 0 (mono, or L of a stereo decode)
    int sampleRate = 0;                    // 0 is explicitly invalid; every consumer must
                                           // receive the WAV's real rate before use.
    std::vector<AudioSample> framesR;      // channel 1 (R); EMPTY for a mono decode
};
Keymap buildZonedKeymap(const std::vector<ResolvedZone>& zones,
                        const std::vector<DecodedZonePcm>& decoded);

// Apply the S7 cross-mode channel policy (D-E) to freshly-decoded interleaved PCM, yielding
// the 1- or 2-channel DecodedZonePcm the keymap build consumes. `interleaved` is the WAV's
// float frames (stride = `sourceChannels`); `mode` is the instance's channel mode.
//   * MONO mode           -> downmix to one channel (the existing policy: average all source
//                            channels). framesR EMPTY. A mono or stereo source both collapse.
//   * STEREO mode, mono src   -> DUAL-MONO: channel 0 duplicated into channel 1 (centered).
//   * STEREO mode, stereo src -> channels 0 and 1 taken as-is (L/R). A source with >2 channels
//                            takes channels 0 and 1 (documented; the sampler's stereo image is
//                            the first two channels — no surround fold).
// Empty / zero-channel input -> a DecodedZonePcm with empty frames (the caller drops the zone
// or plays silence). Pure — the shell does the file I/O and hands the interleaved buffer here.
DecodedZonePcm decodeChannels(const std::vector<AudioSample>& interleaved,
                              int sourceChannels, ChannelMode mode, int sampleRate);

// --- Performance-map instance state (VST3 setState/getState) -----------------
//
// The performance map is the instrument's OWN state (D-B), serialized to the VST3
// component-state IBStream — NOT written to the "reasampler" bank ext-state (the
// instrument is a read-only bank consumer; S4 precedent). Versioned binary, tolerant of
// truncation/wrong-version by design (bounded reads, never throws across the host).
//
// Format: 4-byte LE ENVELOPE version tag (== kPerformanceStateVersion, == 2), then the
// ZONES PAYLOAD.
//
// ZONES-PAYLOAD FORMAT VERSIONING (S11 — self-describing, envelope-independent). The zones
// payload carries its OWN version so the per-zone record can grow (S11's loop/start overrides)
// WITHOUT bumping the envelope version — the envelope (this v2 blob and the v3 ComponentState
// below, and S7's forthcoming v4) simply wraps whatever payload version it holds. This is the
// key composition property: the zone-record extension is versioned inside the map blob, not on
// the envelope, so S11 (zone-record fields) and S7 (envelope v4 for channel mode) do not
// collide on a single version number.
//   * PAYLOAD v1 (pre-S11, on-the-wire shipped): 4-byte LE zone count, then per zone:
//       4-byte LE id length, id bytes, 4-byte LE lowNote, 4-byte LE highNote,
//       1 byte hasRootOverride (0/1), 4-byte LE rootOverride (present iff hasRootOverride).
//     A payload starting with a small u32 (the zone count) is v1 — there is no marker.
//   * PAYLOAD v2 (S11): a 4-byte LE MARKER (kZonesFormatMarker, a high sentinel no real zone
//     count can equal) + a 4-byte LE payload version (== 2), THEN the v1 body PLUS, appended
//     to each zone record after rootOverride:
//       1 byte hasLoopOverride (0/1); iff set: 1 byte loop.hasLoop, 8-byte LE loop.start,
//         8-byte LE loop.end (both two's-complement int64);
//       1 byte hasStartPoint (0/1); iff set: 8-byte LE startPoint (two's-complement int64).
//     The reader detects the marker to know the record shape — a v1 payload (no marker) reads
//     the shorter record; a v2 payload reads the extended one. Both compose under ANY envelope.
//   * PAYLOAD v3 (S15/S16, LEGACY — exists in Daniel's beta projects): the same marker + payload
//     version (== 3), THEN the v2 body PLUS, appended to each zone record after the S11 startPoint
//     tail (the S15/S16 per-zone play params — always present, NOT flag-gated):
//       1 byte playMode (0 = Gate, 1 = Trigger);
//       8-byte LE adsr.holdFrames (int64) — the S15 AHDSR hold stage, FRAMES at 44.1k nominal;
//       8-byte LE trigger.lengthFraction as an IEEE-754 double (bit-cast to u64 LE);
//       8-byte LE trigger.fadeInFrames (int64); 8-byte LE trigger.fadeOutFrames (int64);
//       1 byte pitchEngine (0 = Varispeed, 1 = Preserve);
//       1 byte pitchEnv.enabled (0/1); 8-byte LE pitchEnv.attackFrames (int64, FRAMES 44.1k nom);
//         8-byte LE pitchEnv.decayFrames (int64, FRAMES 44.1k nom); 8-byte LE peakSemitones double.
//     A v1/v2 payload (no v3 tail) lifts each zone to the PRODUCT defaults (Gate + Preserve +
//     no fades + disabled pitch env) — the deliberate S16-F1 behavior change for already-saved
//     instruments. A truncated mid-v3-tail record keeps the zones that parsed and drops the rest.
//     LEGACY-READ CONVERSION (S12): the v3 wall-clock frame counts (hold, pitchEnv A/D) were ALWAYS
//     written by the S15/S16 editor as nominal frames at a baked-in rate. They convert to the seconds
//     domain by dividing by the PROJECT sample rate threaded into the v3 lift path at read time (passed
//     as a parameter — no constant). Source-timeline fields (trigger %-length + fades) stay frames.
//     A/D/S/R are absent in v3 -> lifted to the tier-0 seconds defaults (0.003 / 0 / 1.0 / 0.060).
//   * PAYLOAD v5 (S12 remediation — CURRENT WRITE FORMAT): the same marker + payload version (== 5),
//     THEN the v2 body PLUS, appended to each zone record after the S11 startPoint tail, the full
//     per-zone play params with WALL-CLOCK TIMES STORED AS SECONDS (rate-free, IEEE-754 doubles):
//       1 byte playMode (0 = Gate, 1 = Trigger);
//       8-byte LE adsr.holdSeconds (double); 8-byte LE trigger.lengthFraction (double);
//       8-byte LE trigger.fadeInFrames (int64); 8-byte LE trigger.fadeOutFrames (int64);
//       1 byte pitchEngine; 1 byte pitchEnv.enabled;
//       8-byte LE pitchEnv.attackSeconds (double); 8-byte LE pitchEnv.decaySeconds (double);
//       8-byte LE pitchEnv.peakSemitones (double);
//       8-byte LE adsr.attackSeconds (double); 8-byte LE adsr.decaySeconds (double);
//       8-byte LE adsr.sustainLevel (double); 8-byte LE adsr.releaseSeconds (double).
//     Trigger fades stay int64 SOURCE frames (a source-timeline fact, PLAN.md §S15). PAYLOAD v4
//     (the branch-only frames-tail) was NEVER shipped and is intentionally dropped from the reader
//     — a v4 blob cannot exist outside this branch. The keymap builders resolve the stored seconds
//     to frames at the LIVE sample rate; no rate is baked into storage or the program.
// BACK-COMPAT: a v1 ENVELOPE blob (the S4 single-selection format: version tag 1 + id bytes) is
// lifted to a single full-keyboard zone playing that id (no override) — so an instance saved
// under Tier 0 restores as a one-zone Tier-1 map. A truncated/unknown/empty blob deserializes
// to an EMPTY map.
//
// These two functions serialize the ZONES only. Since S10 the instrument's full component
// state is {single-capture selection id, zones} — see ComponentState / serializeComponentState
// below, the v3 format the processor actually reads/writes. serializePerformance/
// deserializePerformance are retained for the zones payload + the v1/v2 back-compat lift.

inline constexpr std::uint32_t kPerformanceStateVersion = 2;

// The zones-payload format version and its detection marker (S11/S15/S16/S12). serializePerformance
// and serializeComponentState both emit the CURRENT payload version (v5 — marker + version +
// records with the S11 loop/start tail AND the full play-params tail with wall-clock times in
// SECONDS) so the overrides round-trip through EITHER envelope. Readers accept a v1 payload (no
// marker), a v2 payload (marker + version 2, no play tail), and a v3 payload (legacy S15/S16
// play tail with wall-clock frame counts) for back-compat, lifting missing fields to defaults.
// v4 was never shipped and is not read. The marker is a high sentinel that a legitimate zone
// count (bounded by 128 MIDI zones in practice, always tiny) can never collide with.
//   * PAYLOAD v6 (S-VIEW-6 — CURRENT WRITE FORMAT): identical to v5, PLUS one field appended to
//     each zone record after the full v5 play-params tail:
//       8-byte LE keyTrack (IEEE-754 double) — the per-zone key-tracking scalar (1.0 = 100% ET).
//     A v1–v5 payload (no keyTrack field) lifts every zone to keyTrack = 1.0 (the PerformanceZone
//     default), so already-saved instances are BIT-IDENTICAL — the 100% default reproduces the
//     pre-S-VIEW-6 repitch exactly. A truncated mid-keyTrack record keeps the zones that parsed.
inline constexpr std::uint32_t kZonesPayloadVersion = 6;  // S-VIEW-6: + per-zone keyTrack scalar
inline constexpr std::uint32_t kZonesFormatMarker = 0xFFFFFF00u;

// (No kLegacyV3NominalRate constant.) The legacy v3 zone payload's wall-clock frame counts are
// converted to seconds at the v3 read boundary using the PROJECT sample rate threaded in as a
// parameter — frames ÷ projectRate = seconds. The project rate is the same rate keymap build
// already receives, so the seconds domain is consistent across both paths. No constant is baked in.

// The performance map serialized to bytes for IBStream (getState).
std::vector<std::uint8_t> serializePerformance(const PerformanceMap& map);

// The performance map parsed back from IBStream bytes (setState). A v2 blob parses
// directly; a v1 blob lifts to a single full-keyboard zone; anything else -> empty map.
// `projectRate` is the live host/project sample rate (must be > 0) used to convert the
// legacy v3 wall-clock frame counts to the seconds domain at the read boundary.
PerformanceMap deserializePerformance(const std::vector<std::uint8_t>& bytes,
                                      double projectRate);

// --- Combined component state (VST3 setState/getState, v3 — S10) -------------
//
// Since S10 the single-capture SELECTION and the opt-in ZONES are distinct concepts that
// BOTH persist: the default face is one picked capture (the selection id), and zones are a
// demoted opt-in overlay (the performance map). The component state carries both so a saved
// project restores an instance's pick AND its zones — and, per the S10 policy reversal, an
// instance with NO pick and NO zones restores EMPTY (silence + the "pick a capture" empty
// state), never auto-playing sample #1.
//
// Format (envelope v5): 4-byte LE version tag (== 5), then a 1-byte channel-mode field (0 = mono,
// 1 = stereo), then an 8-byte LE last-consumed-assignment generation (S8/S9 reader marker),
// then a 4-byte LE selection-id length + id bytes, then the CURRENT zones payload (identical to
// serializePerformance's body — its own self-describing version, see the ZONES-PAYLOAD block).
// The 8-byte marker is the ONLY envelope-v5 addition over envelope-v4 — the envelope grew a field,
// the zones payload is untouched (a PARALLEL track owns zone-record extension under its own
// versioning; the two version numbers are independent axes). BACK-COMPAT on
// read (every older blob lifts to channelMode = MONO and lastConsumedAssignGeneration = 0,
// preserving current behavior for already-saved instances):
//   * v5 blob        -> {channelMode, lastConsumedAssignGeneration, selectionId, zones} direct.
//   * v4 blob        -> {channelMode, 0, selectionId, zones}: pre-S8/S9 reader (no marker).
//   * v3 blob        -> {mono, 0, selectionId, zones}: pre-S7 had no channel mode.
//   * v2 blob        -> {mono, 0, "", zones}: an S5 instance had zones but no separate selection.
//   * v1 blob        -> {mono, 0, id, one full-keyboard zone}: the S4 single-selection lift.
//   * empty/unknown  -> {mono, 0, "", no zones}: EMPTY (the S10 silent empty state).
//
// WHY THE MARKER PERSISTS (S8 reader requirement). The last-consumed assignment generation is
// the disambiguator that stops a re-opened instance re-applying a stale assign_request the user
// already got and then manually changed away from: on re-open the instance re-reads the pending
// request, and only a generation STRICTLY GREATER than this stored marker re-applies (see
// bank_sync::consumeDecision). A fresh instance defaults to 0, so a genuinely new first assign
// (generation >= 1) still applies. It is the instrument's OWN state (D-B), never written to the
// bank — the extension owns the assign_request key; the instrument only tracks what it consumed.
struct ComponentState {
    std::string selectionId;                     // the single-capture pick; "" = no pick
    PerformanceMap map;                          // the opt-in zones; empty = no zones
    ChannelMode channelMode = ChannelMode::Mono; // S7 output mode; default mono (D-E)
    std::int64_t lastConsumedAssignGeneration = 0;  // S8/S9: last assign_request generation consumed
};

inline constexpr std::uint32_t kComponentStateVersion = 5;

// The pre-S8/S9-reader combined-state version (selection + zones + channel mode, no consumed
// marker). Retained so deserializeComponentState can lift a v4 blob to {mode, 0, sel, zones}.
inline constexpr std::uint32_t kSelectionZonesModeV4Version = 4;

// The pre-S7 combined-state version (selection + zones, no channel mode). Retained as a named
// constant so deserializeComponentState can lift a v3 blob to {mono, selection, zones}.
inline constexpr std::uint32_t kSelectionZonesV3Version = 3;

// The full instance state serialized to bytes for IBStream (getState).
std::vector<std::uint8_t> serializeComponentState(const ComponentState& state);

// The full instance state parsed back from IBStream bytes (setState). Tolerant of
// truncation/wrong-version (bounded reads, never throws); older blobs lift per the table
// above so already-saved instances restore cleanly.
// `projectRate` is the live host/project sample rate (must be > 0) used to convert the
// legacy v3 wall-clock frame counts to the seconds domain at the read boundary.
ComponentState deserializeComponentState(const std::vector<std::uint8_t>& bytes,
                                         double projectRate);

// --- Instance state (VST3 setState/getState) --------------------------------
//
// The instrument's OWN state is which bank sample it plays (D-B: the selection is a
// performance choice, held by the instrument, never written back to the bank). It is a
// single string id. serialize/deserialize keep the on-the-wire form explicit and
// versioned so a future Tier can extend it without breaking already-saved instances.
//
// Format (v1): a 4-byte little-endian version tag (== 1) followed by the id bytes. No
// length prefix is needed — the id runs to the end of the stream (the host tells us the
// byte count). deserializeSelection tolerates a truncated / wrong-version / empty blob
// by returning "" (no selection — under the S10 policy reversal an empty selection is
// SILENCE + the "pick a capture" empty state, not the bank's first sample), never
// throwing across the host boundary. Retained for the v1→v3 back-compat lift in
// deserializeComponentState; the processor's live state is the v3 ComponentState above.

inline constexpr std::uint32_t kSelectionStateVersion = 1;

// The selected-sample id serialized to bytes for IBStream (getState).
std::vector<std::uint8_t> serializeSelection(const std::string& sampleId);

// The selected-sample id parsed back from IBStream bytes (setState). Unknown version,
// too-short, or empty -> "" (graceful no-selection).
std::string deserializeSelection(const std::vector<std::uint8_t>& bytes);

}  // namespace reasampler
