fresh-editor 0.2.11

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
/// Split view system for displaying multiple buffers simultaneously
///
/// Design Philosophy (following Emacs model):
/// - A split is a tree structure: either a leaf (single buffer) or a node (horizontal/vertical split)
/// - Each split has a fixed size (in percentage or absolute lines/columns)
/// - Splits can be nested arbitrarily deep
/// - Only one split is "active" at a time (receives input)
/// - Splits can display the same buffer multiple times (useful for viewing different parts)
///
/// Example split layouts:
/// ```text
/// ┌────────────────────┐      ┌──────────┬─────────┐
/// │                    │      │          │         │
/// │   Single buffer    │      │  Buffer  │ Buffer  │
/// │                    │      │    A     │    B    │
/// └────────────────────┘      └──────────┴─────────┘
///   (no split)                  (vertical split)
///
/// ┌────────────────────┐      ┌──────────┬─────────┐
/// │     Buffer A       │      │          │ Buffer C│
/// ├────────────────────┤      │  Buffer  ├─────────┤
/// │     Buffer B       │      │    A     │ Buffer D│
/// └────────────────────┘      └──────────┴─────────┘
///  (horizontal split)          (mixed splits)
/// ```
use crate::model::buffer::Buffer;
use crate::model::cursor::Cursors;
use crate::model::event::{BufferId, ContainerId, LeafId, SplitDirection, SplitId};
use crate::model::marker::MarkerList;
use crate::view::folding::FoldManager;
use crate::view::ui::view_pipeline::Layout;
use crate::view::viewport::Viewport;
use crate::{services::plugins::api::ViewTransformPayload, state::ViewMode};
use ratatui::layout::Rect;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// A node in the split tree
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum SplitNode {
    /// Leaf node: displays a single buffer
    Leaf {
        /// Which buffer to display
        buffer_id: BufferId,
        /// Unique ID for this split pane
        split_id: LeafId,
    },
    /// Internal node: contains two child splits
    Split {
        /// Direction of the split
        direction: SplitDirection,
        /// First child (top or left)
        first: Box<Self>,
        /// Second child (bottom or right)
        second: Box<Self>,
        /// Size ratio (0.0 to 1.0) - how much space the first child gets
        /// 0.5 = equal split, 0.3 = first gets 30%, etc.
        ratio: f32,
        /// Unique ID for this split container
        split_id: ContainerId,
    },
}

/// Per-buffer view state within a split.
///
/// Each buffer opened in a split gets its own `BufferViewState` stored in the
/// split's `keyed_states` map. This ensures that switching buffers within a split
/// preserves cursor position, scroll state, view mode, and compose settings
/// independently for each buffer.
#[derive(Debug)]
pub struct BufferViewState {
    /// Independent cursor set (supports multi-cursor)
    pub cursors: Cursors,

    /// Independent scroll position
    pub viewport: Viewport,

    /// View mode (Source/Compose) for this buffer in this split
    pub view_mode: ViewMode,

    /// Optional compose width for centering/wrapping
    pub compose_width: Option<u16>,

    /// Column guides (e.g., tables)
    pub compose_column_guides: Option<Vec<u16>>,

    /// Vertical ruler positions (initialized from config, mutable per-buffer)
    pub rulers: Vec<usize>,

    /// Per-split line number visibility.
    /// This is the single source of truth for whether line numbers are shown
    /// in this split. Initialized from config when the split is created.
    /// Compose mode forces this to false; leaving compose restores from config.
    pub show_line_numbers: bool,

    /// Optional view transform payload
    pub view_transform: Option<ViewTransformPayload>,

    /// True when the buffer was edited since the last view_transform_request hook fired.
    /// While true, incoming SubmitViewTransform commands are rejected as stale
    /// (their tokens have source_offsets from before the edit).
    pub view_transform_stale: bool,

    /// Plugin-managed state (arbitrary key-value pairs).
    /// Plugins can store per-buffer-per-split state here via the `setViewState`/`getViewState` API.
    /// Persisted across sessions via workspace serialization.
    pub plugin_state: std::collections::HashMap<String, serde_json::Value>,

    /// Collapsed folding ranges for this buffer/view.
    pub folds: FoldManager,
}

impl BufferViewState {
    /// Resolve fold ranges and ensure the primary cursor is visible.
    ///
    /// This is the preferred entry point for all non-rendering callers — it
    /// resolves hidden fold byte ranges from the marker list and passes them
    /// to `viewport.ensure_visible` so that line counting skips folded lines.
    pub fn ensure_cursor_visible(&mut self, buffer: &mut Buffer, marker_list: &MarkerList) {
        let hidden: Vec<(usize, usize)> = self
            .folds
            .resolved_ranges(buffer, marker_list)
            .into_iter()
            .map(|r| (r.start_byte, r.end_byte))
            .collect();
        let cursor = *self.cursors.primary();
        self.viewport.ensure_visible(buffer, &cursor, &hidden);
    }

    /// Create a new buffer view state with defaults
    pub fn new(width: u16, height: u16) -> Self {
        Self {
            cursors: Cursors::new(),
            viewport: Viewport::new(width, height),
            view_mode: ViewMode::Source,
            compose_width: None,
            compose_column_guides: None,
            rulers: Vec::new(),
            show_line_numbers: true,
            view_transform: None,
            view_transform_stale: false,
            plugin_state: std::collections::HashMap::new(),
            folds: FoldManager::new(),
        }
    }
}

impl Clone for BufferViewState {
    fn clone(&self) -> Self {
        Self {
            cursors: self.cursors.clone(),
            viewport: self.viewport.clone(),
            view_mode: self.view_mode.clone(),
            compose_width: self.compose_width,
            compose_column_guides: self.compose_column_guides.clone(),
            rulers: self.rulers.clone(),
            show_line_numbers: self.show_line_numbers,
            view_transform: self.view_transform.clone(),
            view_transform_stale: self.view_transform_stale,
            plugin_state: self.plugin_state.clone(),
            // Fold markers are per-view; clones start with no folded ranges.
            folds: FoldManager::new(),
        }
    }
}

/// Per-split view state (independent of buffer content)
///
/// Following the Emacs model where each window (split) has its own:
/// - Point (cursor position) - independent per split
/// - Window-start (scroll position) - independent per split
/// - Tabs (open buffers) - independent per split
///
/// Buffer-specific state (cursors, viewport, view_mode, compose settings) is stored
/// in the `keyed_states` map, keyed by `BufferId`. The active buffer's state is
/// accessible via `Deref`/`DerefMut` (so `vs.cursors` transparently accesses the
/// active buffer's cursors), or explicitly via `active_state()`/`active_state_mut()`.
#[derive(Debug, Clone)]
pub struct SplitViewState {
    /// Which buffer is currently active in this split
    pub active_buffer: BufferId,

    /// Per-buffer view state map. The active buffer always has an entry.
    pub keyed_states: HashMap<BufferId, BufferViewState>,

    /// List of buffer IDs open in this split's tab bar (in order)
    /// The currently displayed buffer is tracked in the SplitNode::Leaf
    pub open_buffers: Vec<BufferId>,

    /// Horizontal scroll offset for the tabs in this split
    pub tab_scroll_offset: usize,

    /// Computed layout for this view (from view_transform or base tokens)
    /// This is View state - each split has its own Layout
    pub layout: Option<Layout>,

    /// Whether the layout needs to be rebuilt (buffer changed, transform changed, etc.)
    pub layout_dirty: bool,

    /// Focus history stack for this split (most recent at end)
    /// Used for "Switch to Previous Tab" and for returning to previous buffer when closing
    pub focus_history: Vec<BufferId>,

    /// Sync group ID for synchronized scrolling
    /// Splits with the same sync_group will scroll together
    pub sync_group: Option<u32>,

    /// When set, this split renders a composite view (e.g., side-by-side diff).
    /// The split's buffer_id is the focused source buffer, but rendering uses
    /// the composite layout. This makes the source buffer the "active buffer"
    /// so normal keybindings work directly.
    pub composite_view: Option<BufferId>,
}

impl std::ops::Deref for SplitViewState {
    type Target = BufferViewState;

    fn deref(&self) -> &BufferViewState {
        self.active_state()
    }
}

impl std::ops::DerefMut for SplitViewState {
    fn deref_mut(&mut self) -> &mut BufferViewState {
        self.active_state_mut()
    }
}

impl SplitViewState {
    /// Create a new split view state with an initial buffer open
    pub fn with_buffer(width: u16, height: u16, buffer_id: BufferId) -> Self {
        let buf_state = BufferViewState::new(width, height);
        let mut keyed_states = HashMap::new();
        keyed_states.insert(buffer_id, buf_state);
        Self {
            active_buffer: buffer_id,
            keyed_states,
            open_buffers: vec![buffer_id],
            tab_scroll_offset: 0,
            layout: None,
            layout_dirty: true,
            focus_history: Vec::new(),
            sync_group: None,
            composite_view: None,
        }
    }

    /// Get the active buffer's view state
    pub fn active_state(&self) -> &BufferViewState {
        self.keyed_states
            .get(&self.active_buffer)
            .expect("active_buffer must always have an entry in keyed_states")
    }

    /// Get a mutable reference to the active buffer's view state
    pub fn active_state_mut(&mut self) -> &mut BufferViewState {
        self.keyed_states
            .get_mut(&self.active_buffer)
            .expect("active_buffer must always have an entry in keyed_states")
    }

    /// Switch the active buffer in this split.
    ///
    /// If the new buffer has a saved state in `keyed_states`, it is restored.
    /// Otherwise a default `BufferViewState` is created with the split's current
    /// viewport dimensions.
    pub fn switch_buffer(&mut self, new_buffer_id: BufferId) {
        if new_buffer_id == self.active_buffer {
            return;
        }
        // Ensure the new buffer has keyed state (create default if first time)
        if !self.keyed_states.contains_key(&new_buffer_id) {
            let active = self.active_state();
            let width = active.viewport.width;
            let height = active.viewport.height;
            self.keyed_states
                .insert(new_buffer_id, BufferViewState::new(width, height));
        }
        self.active_buffer = new_buffer_id;
        // Invalidate layout since we're now showing different buffer content
        self.layout_dirty = true;
    }

    /// Get the view state for a specific buffer (if it exists)
    pub fn buffer_state(&self, buffer_id: BufferId) -> Option<&BufferViewState> {
        self.keyed_states.get(&buffer_id)
    }

    /// Get a mutable reference to the view state for a specific buffer (if it exists)
    pub fn buffer_state_mut(&mut self, buffer_id: BufferId) -> Option<&mut BufferViewState> {
        self.keyed_states.get_mut(&buffer_id)
    }

    /// Ensure a buffer has keyed state, creating a default if needed.
    /// Returns a mutable reference to the buffer's view state.
    pub fn ensure_buffer_state(&mut self, buffer_id: BufferId) -> &mut BufferViewState {
        let (width, height) = {
            let active = self.active_state();
            (active.viewport.width, active.viewport.height)
        };
        self.keyed_states
            .entry(buffer_id)
            .or_insert_with(|| BufferViewState::new(width, height))
    }

    /// Remove keyed state for a buffer (when buffer is closed from this split)
    pub fn remove_buffer_state(&mut self, buffer_id: BufferId) {
        if buffer_id != self.active_buffer {
            self.keyed_states.remove(&buffer_id);
        }
    }

    /// Mark layout as needing rebuild (call after buffer changes)
    pub fn invalidate_layout(&mut self) {
        self.layout_dirty = true;
    }

    /// Ensure layout is valid, rebuilding if needed.
    /// Returns the Layout - never returns None. Following VSCode's ViewModel pattern.
    ///
    /// # Arguments
    /// * `tokens` - ViewTokenWire array (from view_transform or built from buffer)
    /// * `source_range` - The byte range this layout covers
    /// * `tab_size` - Tab width for rendering
    pub fn ensure_layout(
        &mut self,
        tokens: &[fresh_core::api::ViewTokenWire],
        source_range: std::ops::Range<usize>,
        tab_size: usize,
    ) -> &Layout {
        if self.layout.is_none() || self.layout_dirty {
            self.layout = Some(Layout::from_tokens(tokens, source_range, tab_size));
            self.layout_dirty = false;
        }
        self.layout.as_ref().unwrap()
    }

    /// Get the current layout if it exists and is valid
    pub fn get_layout(&self) -> Option<&Layout> {
        if self.layout_dirty {
            None
        } else {
            self.layout.as_ref()
        }
    }

    /// Add a buffer to this split's tabs (if not already present)
    pub fn add_buffer(&mut self, buffer_id: BufferId) {
        if !self.open_buffers.contains(&buffer_id) {
            self.open_buffers.push(buffer_id);
        }
    }

    /// Remove a buffer from this split's tabs and clean up its keyed state
    pub fn remove_buffer(&mut self, buffer_id: BufferId) {
        self.open_buffers.retain(|&id| id != buffer_id);
        // Clean up keyed state (but never remove the active buffer's state)
        if buffer_id != self.active_buffer {
            self.keyed_states.remove(&buffer_id);
        }
    }

    /// Check if a buffer is open in this split
    pub fn has_buffer(&self, buffer_id: BufferId) -> bool {
        self.open_buffers.contains(&buffer_id)
    }

    /// Push a buffer to the focus history (LRU-style)
    /// If the buffer is already in history, it's moved to the end
    pub fn push_focus(&mut self, buffer_id: BufferId) {
        // Remove if already in history (LRU-style)
        self.focus_history.retain(|&id| id != buffer_id);
        self.focus_history.push(buffer_id);
        // Limit to 50 entries
        if self.focus_history.len() > 50 {
            self.focus_history.remove(0);
        }
    }

    /// Get the most recently focused buffer (without removing it)
    pub fn previous_buffer(&self) -> Option<BufferId> {
        self.focus_history.last().copied()
    }

    /// Pop the most recent buffer from focus history
    pub fn pop_focus(&mut self) -> Option<BufferId> {
        self.focus_history.pop()
    }

    /// Remove a buffer from the focus history (called when buffer is closed)
    pub fn remove_from_history(&mut self, buffer_id: BufferId) {
        self.focus_history.retain(|&id| id != buffer_id);
    }
}

impl SplitNode {
    /// Create a new leaf node
    pub fn leaf(buffer_id: BufferId, split_id: SplitId) -> Self {
        Self::Leaf {
            buffer_id,
            split_id: LeafId(split_id),
        }
    }

    /// Create a new split node with two children
    pub fn split(
        direction: SplitDirection,
        first: SplitNode,
        second: SplitNode,
        ratio: f32,
        split_id: SplitId,
    ) -> Self {
        SplitNode::Split {
            direction,
            first: Box::new(first),
            second: Box::new(second),
            ratio: ratio.clamp(0.1, 0.9), // Prevent extreme ratios
            split_id: ContainerId(split_id),
        }
    }

    /// Get the split ID for this node
    pub fn id(&self) -> SplitId {
        match self {
            Self::Leaf { split_id, .. } => split_id.0,
            Self::Split { split_id, .. } => split_id.0,
        }
    }

    /// Get the buffer ID if this is a leaf node
    pub fn buffer_id(&self) -> Option<BufferId> {
        match self {
            Self::Leaf { buffer_id, .. } => Some(*buffer_id),
            Self::Split { .. } => None,
        }
    }

    /// Find a split by ID (returns mutable reference)
    pub fn find_mut(&mut self, target_id: SplitId) -> Option<&mut Self> {
        if self.id() == target_id {
            return Some(self);
        }

        match self {
            Self::Leaf { .. } => None,
            Self::Split { first, second, .. } => first
                .find_mut(target_id)
                .or_else(|| second.find_mut(target_id)),
        }
    }

    /// Find a split by ID (returns immutable reference)
    pub fn find(&self, target_id: SplitId) -> Option<&Self> {
        if self.id() == target_id {
            return Some(self);
        }

        match self {
            Self::Leaf { .. } => None,
            Self::Split { first, second, .. } => {
                first.find(target_id).or_else(|| second.find(target_id))
            }
        }
    }

    /// Find the parent container of a given split node
    pub fn parent_container_of(&self, target_id: SplitId) -> Option<ContainerId> {
        match self {
            Self::Leaf { .. } => None,
            Self::Split {
                split_id,
                first,
                second,
                ..
            } => {
                if first.id() == target_id || second.id() == target_id {
                    Some(*split_id)
                } else {
                    first
                        .parent_container_of(target_id)
                        .or_else(|| second.parent_container_of(target_id))
                }
            }
        }
    }

    /// Get all leaf nodes (buffer views) with their rectangles
    pub fn get_leaves_with_rects(&self, rect: Rect) -> Vec<(LeafId, BufferId, Rect)> {
        match self {
            Self::Leaf {
                buffer_id,
                split_id,
            } => {
                vec![(*split_id, *buffer_id, rect)]
            }
            Self::Split {
                direction,
                first,
                second,
                ratio,
                ..
            } => {
                let (first_rect, second_rect) = split_rect(rect, *direction, *ratio);
                let mut leaves = first.get_leaves_with_rects(first_rect);
                leaves.extend(second.get_leaves_with_rects(second_rect));
                leaves
            }
        }
    }

    /// Get all split separator lines (for rendering borders)
    /// Returns (direction, x, y, length) tuples
    pub fn get_separators(&self, rect: Rect) -> Vec<(SplitDirection, u16, u16, u16)> {
        self.get_separators_with_ids(rect)
            .into_iter()
            .map(|(_, dir, x, y, len)| (dir, x, y, len))
            .collect()
    }

    /// Get all split separator lines with their split IDs (for mouse hit testing)
    /// Returns (split_id, direction, x, y, length) tuples
    pub fn get_separators_with_ids(
        &self,
        rect: Rect,
    ) -> Vec<(ContainerId, SplitDirection, u16, u16, u16)> {
        match self {
            Self::Leaf { .. } => vec![],
            Self::Split {
                direction,
                first,
                second,
                ratio,
                split_id,
            } => {
                let (first_rect, second_rect) = split_rect(rect, *direction, *ratio);
                let mut separators = Vec::new();

                // Add separator for this split (in the 1-char gap between first and second)
                match direction {
                    SplitDirection::Horizontal => {
                        // Horizontal split: separator line is between first and second
                        // y position is at the end of first rect (the gap line)
                        separators.push((
                            *split_id,
                            SplitDirection::Horizontal,
                            rect.x,
                            first_rect.y + first_rect.height,
                            rect.width,
                        ));
                    }
                    SplitDirection::Vertical => {
                        // Vertical split: separator line is between first and second
                        // x position is at the end of first rect (the gap column)
                        separators.push((
                            *split_id,
                            SplitDirection::Vertical,
                            first_rect.x + first_rect.width,
                            rect.y,
                            rect.height,
                        ));
                    }
                }

                // Recursively get separators from children
                separators.extend(first.get_separators_with_ids(first_rect));
                separators.extend(second.get_separators_with_ids(second_rect));
                separators
            }
        }
    }

    /// Collect all split IDs in the tree
    pub fn all_split_ids(&self) -> Vec<SplitId> {
        let mut ids = vec![self.id()];
        match self {
            Self::Leaf { .. } => ids,
            Self::Split { first, second, .. } => {
                ids.extend(first.all_split_ids());
                ids.extend(second.all_split_ids());
                ids
            }
        }
    }

    /// Collect only leaf split IDs (visible buffer splits, not container nodes)
    pub fn leaf_split_ids(&self) -> Vec<LeafId> {
        match self {
            Self::Leaf { split_id, .. } => vec![*split_id],
            Self::Split { first, second, .. } => {
                let mut ids = first.leaf_split_ids();
                ids.extend(second.leaf_split_ids());
                ids
            }
        }
    }

    /// Count the number of leaf nodes (visible buffers)
    pub fn count_leaves(&self) -> usize {
        match self {
            Self::Leaf { .. } => 1,
            Self::Split { first, second, .. } => first.count_leaves() + second.count_leaves(),
        }
    }
}

/// Split a rectangle into two parts based on direction and ratio
/// Leaves 1 character space for the separator line between splits
fn split_rect(rect: Rect, direction: SplitDirection, ratio: f32) -> (Rect, Rect) {
    match direction {
        SplitDirection::Horizontal => {
            // Split into top and bottom, with 1 line for separator
            let total_height = rect.height.saturating_sub(1); // Reserve 1 line for separator
            let first_height = (total_height as f32 * ratio).round() as u16;
            let second_height = total_height.saturating_sub(first_height);

            let first = Rect {
                x: rect.x,
                y: rect.y,
                width: rect.width,
                height: first_height,
            };

            let second = Rect {
                x: rect.x,
                y: rect.y + first_height + 1, // +1 for separator
                width: rect.width,
                height: second_height,
            };

            (first, second)
        }
        SplitDirection::Vertical => {
            // Split into left and right, with 1 column for separator
            let total_width = rect.width.saturating_sub(1); // Reserve 1 column for separator
            let first_width = (total_width as f32 * ratio).round() as u16;
            let second_width = total_width.saturating_sub(first_width);

            let first = Rect {
                x: rect.x,
                y: rect.y,
                width: first_width,
                height: rect.height,
            };

            let second = Rect {
                x: rect.x + first_width + 1, // +1 for separator
                y: rect.y,
                width: second_width,
                height: rect.height,
            };

            (first, second)
        }
    }
}

/// Manager for the split view system
#[derive(Debug)]
pub struct SplitManager {
    /// Root of the split tree
    root: SplitNode,

    /// Currently active split (receives input) — always a leaf
    active_split: LeafId,

    /// Next split ID to assign
    next_split_id: usize,

    /// Currently maximized split (if any). When set, only this split is visible.
    maximized_split: Option<SplitId>,

    /// Labels for leaf splits (e.g., "sidebar" to mark managed splits)
    labels: HashMap<SplitId, String>,
}

impl SplitManager {
    /// Create a new split manager with a single buffer
    pub fn new(buffer_id: BufferId) -> Self {
        let split_id = SplitId(0);
        Self {
            root: SplitNode::leaf(buffer_id, split_id),
            active_split: LeafId(split_id),
            next_split_id: 1,
            maximized_split: None,
            labels: HashMap::new(),
        }
    }

    /// Get the root split node
    pub fn root(&self) -> &SplitNode {
        &self.root
    }

    /// Get the currently active split ID
    pub fn active_split(&self) -> LeafId {
        self.active_split
    }

    /// Set the active split (must be a leaf)
    pub fn set_active_split(&mut self, split_id: LeafId) -> bool {
        // Verify the split exists
        if self.root.find(split_id.into()).is_some() {
            self.active_split = split_id;
            true
        } else {
            false
        }
    }

    /// Get the buffer ID of the active split (if it's a leaf)
    pub fn active_buffer_id(&self) -> Option<BufferId> {
        self.root
            .find(self.active_split.into())
            .and_then(|node| node.buffer_id())
    }

    /// Get the buffer ID for a specific split (if it's a leaf)
    pub fn get_buffer_id(&self, split_id: SplitId) -> Option<BufferId> {
        self.root.find(split_id).and_then(|node| node.buffer_id())
    }

    /// Update the buffer ID of the active split
    pub fn set_active_buffer_id(&mut self, new_buffer_id: BufferId) -> bool {
        if let Some(SplitNode::Leaf { buffer_id, .. }) =
            self.root.find_mut(self.active_split.into())
        {
            *buffer_id = new_buffer_id;
            return true;
        }
        false
    }

    /// Update the buffer ID of a specific leaf split
    pub fn set_split_buffer(&mut self, leaf_id: LeafId, new_buffer_id: BufferId) {
        match self.root.find_mut(leaf_id.into()) {
            Some(SplitNode::Leaf { buffer_id, .. }) => {
                *buffer_id = new_buffer_id;
            }
            Some(SplitNode::Split { .. }) => {
                unreachable!("LeafId {:?} points to a container", leaf_id)
            }
            None => {
                unreachable!("LeafId {:?} not found in split tree", leaf_id)
            }
        }
    }

    /// Allocate a new split ID
    fn allocate_split_id(&mut self) -> SplitId {
        let id = SplitId(self.next_split_id);
        self.next_split_id += 1;
        id
    }

    /// Split the currently active pane
    pub fn split_active(
        &mut self,
        direction: SplitDirection,
        new_buffer_id: BufferId,
        ratio: f32,
    ) -> Result<LeafId, String> {
        self.split_active_positioned(direction, new_buffer_id, ratio, false)
    }

    /// Split the active pane, placing the new buffer before (left/top) the existing content.
    /// `ratio` still controls the first child's proportion of space.
    pub fn split_active_before(
        &mut self,
        direction: SplitDirection,
        new_buffer_id: BufferId,
        ratio: f32,
    ) -> Result<LeafId, String> {
        self.split_active_positioned(direction, new_buffer_id, ratio, true)
    }

    pub fn split_active_positioned(
        &mut self,
        direction: SplitDirection,
        new_buffer_id: BufferId,
        ratio: f32,
        before: bool,
    ) -> Result<LeafId, String> {
        let active_id: SplitId = self.active_split.into();

        // Find the parent of the active split
        let result =
            self.replace_split_with_split(active_id, direction, new_buffer_id, ratio, before);

        if let Ok(new_split_id) = &result {
            // Set the new split as active
            self.active_split = *new_split_id;
        }
        result
    }

    /// Replace a split with a new split container.
    /// When `before` is true, the new buffer is placed as the first child (left/top).
    fn replace_split_with_split(
        &mut self,
        target_id: SplitId,
        direction: SplitDirection,
        new_buffer_id: BufferId,
        ratio: f32,
        before: bool,
    ) -> Result<LeafId, String> {
        // Pre-allocate all IDs before any borrowing
        let temp_id = self.allocate_split_id();
        let new_split_id = self.allocate_split_id();
        let new_leaf_id = self.allocate_split_id();

        // Special case: if target is root, replace root
        if self.root.id() == target_id {
            let old_root =
                std::mem::replace(&mut self.root, SplitNode::leaf(new_buffer_id, temp_id));
            let new_leaf = SplitNode::leaf(new_buffer_id, new_leaf_id);

            let (first, second) = if before {
                (new_leaf, old_root)
            } else {
                (old_root, new_leaf)
            };

            self.root = SplitNode::split(direction, first, second, ratio, new_split_id);

            return Ok(LeafId(new_leaf_id));
        }

        // Find and replace the target node
        if let Some(node) = self.root.find_mut(target_id) {
            let old_node = std::mem::replace(node, SplitNode::leaf(new_buffer_id, temp_id));
            let new_leaf = SplitNode::leaf(new_buffer_id, new_leaf_id);

            let (first, second) = if before {
                (new_leaf, old_node)
            } else {
                (old_node, new_leaf)
            };

            *node = SplitNode::split(direction, first, second, ratio, new_split_id);

            Ok(LeafId(new_leaf_id))
        } else {
            Err(format!("Split {:?} not found", target_id))
        }
    }

    /// Close a split pane (if not the last one)
    pub fn close_split(&mut self, split_id: LeafId) -> Result<(), String> {
        // Can't close if it's the only split
        if self.root.count_leaves() <= 1 {
            return Err("Cannot close the last split".to_string());
        }

        // Can't close if it's the root and root is a leaf
        if self.root.id() == split_id.into() && self.root.buffer_id().is_some() {
            return Err("Cannot close the only split".to_string());
        }

        // If the split being closed is maximized, unmaximize first
        if self.maximized_split == Some(split_id.into()) {
            self.maximized_split = None;
        }

        // Collect all split IDs that will be removed (the target and its children)
        let removed_ids: Vec<SplitId> = self
            .root
            .find(split_id.into())
            .map(|node| node.all_split_ids())
            .unwrap_or_default();

        // Find the parent of the split to close
        // This requires a parent-tracking traversal
        let result = self.remove_split_node(split_id.into());

        if result.is_ok() {
            // Clean up labels for all removed splits
            for id in &removed_ids {
                self.labels.remove(id);
            }

            // If we closed the active split, update active_split to another split
            if self.active_split == split_id {
                let leaf_ids = self.root.leaf_split_ids();
                if let Some(&first_leaf) = leaf_ids.first() {
                    self.active_split = first_leaf;
                }
            }
        }

        result
    }

    /// Remove a split node from the tree
    fn remove_split_node(&mut self, target_id: SplitId) -> Result<(), String> {
        // Special case: removing root
        if self.root.id() == target_id {
            if let SplitNode::Split { first, .. } = &self.root {
                // Replace root with the other child
                // Choose first child arbitrarily
                self.root = (**first).clone();
                return Ok(());
            }
        }

        // Recursively find and remove
        Self::remove_child_static(&mut self.root, target_id)
    }

    /// Helper to remove a child from a split node (static to avoid borrow issues)
    fn remove_child_static(node: &mut SplitNode, target_id: SplitId) -> Result<(), String> {
        match node {
            SplitNode::Leaf { .. } => Err("Target not found".to_string()),
            SplitNode::Split { first, second, .. } => {
                // Check if either child is the target
                if first.id() == target_id {
                    // Replace this node with the second child
                    *node = (**second).clone();
                    Ok(())
                } else if second.id() == target_id {
                    // Replace this node with the first child
                    *node = (**first).clone();
                    Ok(())
                } else {
                    // Recurse into children
                    Self::remove_child_static(first, target_id)
                        .or_else(|_| Self::remove_child_static(second, target_id))
                }
            }
        }
    }

    /// Adjust the split ratio of a container
    pub fn adjust_ratio(&mut self, container_id: ContainerId, delta: f32) {
        match self.root.find_mut(container_id.into()) {
            Some(SplitNode::Split { ratio, .. }) => {
                *ratio = (*ratio + delta).clamp(0.1, 0.9);
            }
            Some(SplitNode::Leaf { .. }) => {
                unreachable!("ContainerId {:?} points to a leaf", container_id)
            }
            None => {
                unreachable!("ContainerId {:?} not found in split tree", container_id)
            }
        }
    }

    /// Find the parent container of a leaf
    pub fn parent_container_of(&self, leaf_id: LeafId) -> Option<ContainerId> {
        self.root.parent_container_of(leaf_id.into())
    }

    /// Get all visible buffer views with their rectangles
    pub fn get_visible_buffers(&self, viewport_rect: Rect) -> Vec<(LeafId, BufferId, Rect)> {
        // If a split is maximized, only show that split taking up the full viewport
        if let Some(maximized_id) = self.maximized_split {
            if let Some(node) = self.root.find(maximized_id) {
                if let SplitNode::Leaf {
                    buffer_id,
                    split_id,
                } = node
                {
                    return vec![(*split_id, *buffer_id, viewport_rect)];
                }
            }
            // Maximized split no longer exists, clear it and fall through
        }
        self.root.get_leaves_with_rects(viewport_rect)
    }

    /// Get all split separator positions for rendering borders
    /// Returns (direction, x, y, length) tuples
    pub fn get_separators(&self, viewport_rect: Rect) -> Vec<(SplitDirection, u16, u16, u16)> {
        // No separators when a split is maximized
        if self.maximized_split.is_some() {
            return vec![];
        }
        self.root.get_separators(viewport_rect)
    }

    /// Get all split separator positions with their split IDs (for mouse hit testing)
    /// Returns (container_id, direction, x, y, length) tuples
    pub fn get_separators_with_ids(
        &self,
        viewport_rect: Rect,
    ) -> Vec<(ContainerId, SplitDirection, u16, u16, u16)> {
        // No separators when a split is maximized
        if self.maximized_split.is_some() {
            return vec![];
        }
        self.root.get_separators_with_ids(viewport_rect)
    }

    /// Get the current ratio of a split container
    pub fn get_ratio(&self, split_id: SplitId) -> Option<f32> {
        if let Some(SplitNode::Split { ratio, .. }) = self.root.find(split_id) {
            Some(*ratio)
        } else {
            None
        }
    }

    /// Set the exact ratio of a split container
    pub fn set_ratio(&mut self, container_id: ContainerId, new_ratio: f32) {
        match self.root.find_mut(container_id.into()) {
            Some(SplitNode::Split { ratio, .. }) => {
                *ratio = new_ratio.clamp(0.1, 0.9);
            }
            Some(SplitNode::Leaf { .. }) => {
                unreachable!("ContainerId {:?} points to a leaf", container_id)
            }
            None => {
                unreachable!("ContainerId {:?} not found in split tree", container_id)
            }
        }
    }

    /// Distribute all visible splits evenly
    /// This sets the ratios of all container splits so that leaf splits get equal space
    pub fn distribute_splits_evenly(&mut self) {
        Self::distribute_node_evenly(&mut self.root);
    }

    /// Recursively distribute a node's splits evenly
    /// Returns the number of leaves in this subtree
    fn distribute_node_evenly(node: &mut SplitNode) -> usize {
        match node {
            SplitNode::Leaf { .. } => 1,
            SplitNode::Split {
                first,
                second,
                ratio,
                ..
            } => {
                let first_leaves = Self::distribute_node_evenly(first);
                let second_leaves = Self::distribute_node_evenly(second);
                let total_leaves = first_leaves + second_leaves;

                // Set ratio so each leaf gets equal space
                // ratio = proportion for first pane
                *ratio = (first_leaves as f32 / total_leaves as f32).clamp(0.1, 0.9);

                total_leaves
            }
        }
    }

    /// Navigate to the next split (circular)
    pub fn next_split(&mut self) {
        let leaf_ids = self.root.leaf_split_ids();
        if let Some(pos) = leaf_ids.iter().position(|id| *id == self.active_split) {
            let next_pos = (pos + 1) % leaf_ids.len();
            self.active_split = leaf_ids[next_pos];
        }
    }

    /// Navigate to the previous split (circular)
    pub fn prev_split(&mut self) {
        let leaf_ids = self.root.leaf_split_ids();
        if let Some(pos) = leaf_ids.iter().position(|id| *id == self.active_split) {
            let prev_pos = if pos == 0 { leaf_ids.len() } else { pos } - 1;
            self.active_split = leaf_ids[prev_pos];
        }
    }

    /// Get all split IDs that display a specific buffer
    pub fn splits_for_buffer(&self, target_buffer_id: BufferId) -> Vec<LeafId> {
        self.root
            .get_leaves_with_rects(Rect {
                x: 0,
                y: 0,
                width: 1,
                height: 1,
            })
            .into_iter()
            .filter(|(_, buffer_id, _)| *buffer_id == target_buffer_id)
            .map(|(split_id, _, _)| split_id)
            .collect()
    }

    /// Get the buffer ID for a specific leaf split
    pub fn buffer_for_split(&self, target_split_id: LeafId) -> Option<BufferId> {
        self.root
            .get_leaves_with_rects(Rect {
                x: 0,
                y: 0,
                width: 1,
                height: 1,
            })
            .into_iter()
            .find(|(split_id, _, _)| *split_id == target_split_id)
            .map(|(_, buffer_id, _)| buffer_id)
    }

    /// Maximize the active split (hide all other splits temporarily)
    /// Returns Ok(()) if successful, Err if there's only one split
    pub fn maximize_split(&mut self) -> Result<(), String> {
        // Can't maximize if there's only one split
        if self.root.count_leaves() <= 1 {
            return Err("Cannot maximize: only one split exists".to_string());
        }

        // Can't maximize if already maximized
        if self.maximized_split.is_some() {
            return Err("A split is already maximized".to_string());
        }

        // Maximize the active split
        self.maximized_split = Some(self.active_split.into());
        Ok(())
    }

    /// Unmaximize the currently maximized split (restore all splits)
    /// Returns Ok(()) if successful, Err if no split is maximized
    pub fn unmaximize_split(&mut self) -> Result<(), String> {
        if self.maximized_split.is_none() {
            return Err("No split is maximized".to_string());
        }

        self.maximized_split = None;
        Ok(())
    }

    /// Check if a split is currently maximized
    pub fn is_maximized(&self) -> bool {
        self.maximized_split.is_some()
    }

    /// Get the currently maximized split ID (if any)
    pub fn maximized_split(&self) -> Option<SplitId> {
        self.maximized_split
    }

    /// Toggle maximize state for the active split
    /// If maximized, unmaximize. If not maximized, maximize.
    /// Returns true if maximized, false if ununmaximized.
    pub fn toggle_maximize(&mut self) -> Result<bool, String> {
        if self.is_maximized() {
            self.unmaximize_split()?;
            Ok(false)
        } else {
            self.maximize_split()?;
            Ok(true)
        }
    }

    /// Get all leaf split IDs that belong to a specific sync group
    pub fn get_splits_in_group(
        &self,
        group_id: u32,
        view_states: &std::collections::HashMap<LeafId, SplitViewState>,
    ) -> Vec<LeafId> {
        self.root
            .leaf_split_ids()
            .into_iter()
            .filter(|id| {
                view_states
                    .get(id)
                    .and_then(|vs| vs.sync_group)
                    .is_some_and(|g| g == group_id)
            })
            .collect()
    }

    // === Split labels ===

    /// Set a label on a leaf split (e.g., "sidebar")
    pub fn set_label(&mut self, split_id: LeafId, label: String) {
        self.labels.insert(split_id.into(), label);
    }

    /// Remove a label from a split
    pub fn clear_label(&mut self, split_id: SplitId) {
        self.labels.remove(&split_id);
    }

    /// Get the label for a split (if any)
    pub fn get_label(&self, split_id: SplitId) -> Option<&str> {
        self.labels.get(&split_id).map(|s| s.as_str())
    }

    /// Get all split labels (for workspace serialization)
    pub fn labels(&self) -> &HashMap<SplitId, String> {
        &self.labels
    }

    /// Find the first leaf split with the given label
    pub fn find_split_by_label(&self, label: &str) -> Option<LeafId> {
        self.root
            .leaf_split_ids()
            .into_iter()
            .find(|id| self.labels.get(&(*id).into()).is_some_and(|l| l == label))
    }

    /// Find the first leaf split without a label
    pub fn find_unlabeled_leaf(&self) -> Option<LeafId> {
        self.root
            .leaf_split_ids()
            .into_iter()
            .find(|id| !self.labels.contains_key(&(*id).into()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_create_split_manager() {
        let buffer_id = BufferId(0);
        let manager = SplitManager::new(buffer_id);

        assert_eq!(manager.active_buffer_id(), Some(buffer_id));
        assert_eq!(manager.root().count_leaves(), 1);
    }

    #[test]
    fn test_horizontal_split() {
        let buffer_a = BufferId(0);
        let buffer_b = BufferId(1);

        let mut manager = SplitManager::new(buffer_a);
        let result = manager.split_active(SplitDirection::Horizontal, buffer_b, 0.5);

        assert!(result.is_ok());
        assert_eq!(manager.root().count_leaves(), 2);
    }

    #[test]
    fn test_vertical_split() {
        let buffer_a = BufferId(0);
        let buffer_b = BufferId(1);

        let mut manager = SplitManager::new(buffer_a);
        let result = manager.split_active(SplitDirection::Vertical, buffer_b, 0.5);

        assert!(result.is_ok());
        assert_eq!(manager.root().count_leaves(), 2);
    }

    #[test]
    fn test_nested_splits() {
        let buffer_a = BufferId(0);
        let buffer_b = BufferId(1);
        let buffer_c = BufferId(2);

        let mut manager = SplitManager::new(buffer_a);

        // Split horizontally
        manager
            .split_active(SplitDirection::Horizontal, buffer_b, 0.5)
            .unwrap();

        // Split the second pane vertically
        manager
            .split_active(SplitDirection::Vertical, buffer_c, 0.5)
            .unwrap();

        assert_eq!(manager.root().count_leaves(), 3);
    }

    #[test]
    fn test_close_split() {
        let buffer_a = BufferId(0);
        let buffer_b = BufferId(1);

        let mut manager = SplitManager::new(buffer_a);
        let new_split = manager
            .split_active(SplitDirection::Horizontal, buffer_b, 0.5)
            .unwrap();

        assert_eq!(manager.root().count_leaves(), 2);

        // Close the new split
        let result = manager.close_split(new_split);
        assert!(result.is_ok());
        assert_eq!(manager.root().count_leaves(), 1);
    }

    #[test]
    fn test_cannot_close_last_split() {
        let buffer_a = BufferId(0);
        let mut manager = SplitManager::new(buffer_a);

        let result = manager.close_split(manager.active_split());
        assert!(result.is_err());
    }

    #[test]
    fn test_split_rect_horizontal() {
        let rect = Rect {
            x: 0,
            y: 0,
            width: 100,
            height: 100,
        };

        let (first, second) = split_rect(rect, SplitDirection::Horizontal, 0.5);

        // With 1 line reserved for separator: (100-1)/2 = 49.5 rounds to 50 and 49
        assert_eq!(first.height, 50);
        assert_eq!(second.height, 49);
        assert_eq!(first.width, 100);
        assert_eq!(second.width, 100);
        assert_eq!(first.y, 0);
        assert_eq!(second.y, 51); // first.y + first.height + 1 (separator)
    }

    #[test]
    fn test_split_rect_vertical() {
        let rect = Rect {
            x: 0,
            y: 0,
            width: 100,
            height: 100,
        };

        let (first, second) = split_rect(rect, SplitDirection::Vertical, 0.5);

        // With 1 column reserved for separator: (100-1)/2 = 49.5 rounds to 50 and 49
        assert_eq!(first.width, 50);
        assert_eq!(second.width, 49);
        assert_eq!(first.height, 100);
        assert_eq!(second.height, 100);
        assert_eq!(first.x, 0);
        assert_eq!(second.x, 51); // first.x + first.width + 1 (separator)
    }

    // === Split label tests ===

    #[test]
    fn test_set_and_get_label() {
        let mut manager = SplitManager::new(BufferId(0));
        let split = manager.active_split();

        assert_eq!(manager.get_label(split.into()), None);

        manager.set_label(split, "sidebar".to_string());
        assert_eq!(manager.get_label(split.into()), Some("sidebar"));
    }

    #[test]
    fn test_clear_label() {
        let mut manager = SplitManager::new(BufferId(0));
        let split = manager.active_split();

        manager.set_label(split, "sidebar".to_string());
        assert!(manager.get_label(split.into()).is_some());

        manager.clear_label(split.into());
        assert_eq!(manager.get_label(split.into()), None);
    }

    #[test]
    fn test_find_split_by_label() {
        let mut manager = SplitManager::new(BufferId(0));
        let first_split = manager.active_split();

        let second_split = manager
            .split_active(SplitDirection::Vertical, BufferId(1), 0.5)
            .unwrap();

        manager.set_label(first_split, "sidebar".to_string());

        assert_eq!(manager.find_split_by_label("sidebar"), Some(first_split));
        assert_eq!(manager.find_split_by_label("terminal"), None);

        // The second split has no label
        assert_ne!(manager.find_split_by_label("sidebar"), Some(second_split));
    }

    #[test]
    fn test_find_unlabeled_leaf() {
        let mut manager = SplitManager::new(BufferId(0));
        let first_split = manager.active_split();

        let second_split = manager
            .split_active(SplitDirection::Vertical, BufferId(1), 0.5)
            .unwrap();

        // No labels — first leaf returned
        assert!(manager.find_unlabeled_leaf().is_some());

        // Label the first split — unlabeled should return the second
        manager.set_label(first_split, "sidebar".to_string());
        assert_eq!(manager.find_unlabeled_leaf(), Some(second_split));

        // Label both — no unlabeled leaf
        manager.set_label(second_split, "terminal".to_string());
        assert_eq!(manager.find_unlabeled_leaf(), None);
    }

    #[test]
    fn test_close_split_cleans_up_label() {
        let mut manager = SplitManager::new(BufferId(0));
        let _first_split = manager.active_split();

        let second_split = manager
            .split_active(SplitDirection::Vertical, BufferId(1), 0.5)
            .unwrap();

        manager.set_label(second_split, "sidebar".to_string());
        assert_eq!(manager.find_split_by_label("sidebar"), Some(second_split));

        manager.close_split(second_split).unwrap();

        // Label should be cleaned up
        assert_eq!(manager.find_split_by_label("sidebar"), None);
        assert_eq!(manager.get_label(second_split.into()), None);
    }

    #[test]
    fn test_label_overwrite() {
        let mut manager = SplitManager::new(BufferId(0));
        let split = manager.active_split();

        manager.set_label(split, "sidebar".to_string());
        assert_eq!(manager.get_label(split.into()), Some("sidebar"));

        manager.set_label(split, "terminal".to_string());
        assert_eq!(manager.get_label(split.into()), Some("terminal"));
        assert_eq!(manager.find_split_by_label("sidebar"), None);
        assert_eq!(manager.find_split_by_label("terminal"), Some(split));
    }

    #[test]
    fn test_find_unlabeled_leaf_single_split_no_label() {
        let manager = SplitManager::new(BufferId(0));
        // Single unlabeled split — should return it
        assert_eq!(manager.find_unlabeled_leaf(), Some(manager.active_split()));
    }

    #[test]
    fn test_find_unlabeled_leaf_single_split_labeled() {
        let mut manager = SplitManager::new(BufferId(0));
        let split = manager.active_split();
        manager.set_label(split, "only".to_string());
        // Only split is labeled — returns None
        assert_eq!(manager.find_unlabeled_leaf(), None);
    }
}