hypen-engine 0.4.942

A Rust implementation of the Hypen engine
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
use super::conditionals::{
    evaluate_value, find_matching_branch, find_matching_route_with_key,
};
use super::tree::DEFAULT_ROUTER_CACHE_SIZE;
use super::item_bindings::replace_ir_node_item_bindings;
use super::keyed::{generate_item_key, reconcile_iterable_children};
use super::resolve::{evaluate_binding, resolve_props_full};
use super::{ControlFlowKind, InstanceTree, Patch};
use crate::ir::{Element, IRNode, NodeId, Props, RouterRoute, Value};
use crate::reactive::DependencyGraph;
use indexmap::IndexMap;

/// Data sources type alias for readability
type DataSources = indexmap::IndexMap<String, serde_json::Value>;

/// Module instances map type alias
type Modules = indexmap::IndexMap<String, crate::lifecycle::ModuleInstance>;

/// Shared mutable context threaded through the recursive tree-building and
/// reconciliation helpers.  Grouping these fields removes the repetitive
/// parameter tuple that was being copy-pasted across every internal function.
pub(crate) struct ReconcileCtx<'a> {
    pub tree: &'a mut InstanceTree,
    pub state: &'a serde_json::Value,
    pub patches: &'a mut Vec<Patch>,
    pub dependencies: &'a mut DependencyGraph,
    pub data_sources: Option<&'a DataSources>,
    pub modules: Option<&'a Modules>,
}

impl<'a> ReconcileCtx<'a> {
    /// Resolve a raw `module_scope` to its effective form.
    ///
    /// Returns `Some(scope)` only when the named module is actually registered
    /// in `ctx.modules`. When the scope name has no matching named module
    /// (e.g. the legacy "wrap your primary module's DSL in `module App { ... }`"
    /// pattern), this returns `None` so dependency registration falls back to
    /// raw paths and state lookup falls back to the primary module's state.
    fn effective_scope<'s>(&self, raw: Option<&'s str>) -> Option<&'s str> {
        raw.filter(|scope| {
            self.modules
                .map(|m| m.contains_key(*scope))
                .unwrap_or(false)
        })
    }

    /// Resolve the state slot to read bindings against.
    ///
    /// Returns the named module's state when `effective_scope` matches a
    /// registered module, otherwise the primary state from the context.
    fn effective_state(&self, raw_scope: Option<&str>) -> &'a serde_json::Value {
        match self.effective_scope(raw_scope) {
            Some(scope) => self
                .modules
                .and_then(|m| m.get(scope))
                .map(|m| m.get_state())
                .unwrap_or(self.state),
            None => self.state,
        }
    }
}

/// Reconcile an IRNode tree against the instance tree and generate patches
/// This is the primary entry point for the IRNode-based reconciliation system,
/// which supports first-class ForEach, When/If, and custom item variable names.
pub fn reconcile_ir(
    tree: &mut InstanceTree,
    node: &IRNode,
    parent_id: Option<NodeId>,
    state: &serde_json::Value,
    dependencies: &mut DependencyGraph,
) -> Vec<Patch> {
    reconcile_ir_with_ds(tree, node, parent_id, state, dependencies, None, None)
}

/// Reconcile an IRNode tree with data source context
pub fn reconcile_ir_with_ds(
    tree: &mut InstanceTree,
    node: &IRNode,
    parent_id: Option<NodeId>,
    state: &serde_json::Value,
    dependencies: &mut DependencyGraph,
    data_sources: Option<&DataSources>,
    modules: Option<&indexmap::IndexMap<String, crate::lifecycle::ModuleInstance>>,
) -> Vec<Patch> {
    let mut patches = Vec::new();

    // For initial render, create the tree
    if tree.root().is_none() {
        let mut ctx = ReconcileCtx {
            tree,
            state,
            patches: &mut patches,
            dependencies,
            data_sources,
            modules,
        };
        let node_id = create_ir_node_tree_impl(&mut ctx, node, parent_id, true);
        ctx.tree.set_root(node_id);
        return patches;
    }

    // Incremental update: reconcile root against existing tree
    if let Some(root_id) = tree.root() {
        let mut ctx = ReconcileCtx {
            tree,
            state,
            patches: &mut patches,
            dependencies,
            data_sources,
            modules,
        };
        reconcile_ir_node_impl(&mut ctx, root_id, node);
    }

    patches
}

/// Create a tree node for an `Element`.
///
/// Used by the IRNode dispatcher's `IRNode::Element` arm. The
/// `render_parent` parameter exists so control-flow constructs (ForEach,
/// Conditional, Router) can mount their children's tree slot under the
/// CFN container while emitting the actual `Insert` patch into the
/// CFN's grandparent — the renderer treats CFN containers as transparent.
/// When `render_parent == logical_parent` the two parameters collapse
/// to the normal "render where the tree puts it" behavior.
fn create_element_node(
    ctx: &mut ReconcileCtx,
    element: &Element,
    logical_parent: Option<NodeId>,
    render_parent: Option<NodeId>,
    is_root: bool,
) -> NodeId {
    let module_scope_ref = ctx.effective_scope(element.module_scope.as_deref());
    let effective_state = ctx.effective_state(element.module_scope.as_deref());

    // Iterable element fast-path (List, Grid, …) — only valid in the
    // "normal" path where logical and render parents agree, since
    // create_list_tree_impl emits its own Insert patches against the
    // tree-side parent.
    if logical_parent == render_parent {
        if let Some(Value::Binding(_)) = element.props.get("0") {
            if !element.ir_children.is_empty() {
                return create_list_tree_impl(ctx, element, logical_parent, is_root);
            }
        }
    }

    // Allocate the node and register reactive dependencies for every
    // binding in its props.
    let node_id = ctx
        .tree
        .create_node_full(element, effective_state, ctx.data_sources);

    for value in element.props.values() {
        match value {
            Value::Binding(binding) => {
                ctx.dependencies
                    .add_dependency(node_id, binding, module_scope_ref);
            }
            Value::TemplateString { bindings, .. } => {
                for binding in bindings {
                    ctx.dependencies
                        .add_dependency(node_id, binding, module_scope_ref);
                }
            }
            _ => {}
        }
    }

    // Build the Create patch payload. Lazy elements stash the first
    // child's component name in a `__lazy_child` prop so renderers know
    // what to fetch when the user activates the slot.
    let is_lazy = element
        .props
        .get("__lazy")
        .and_then(|v| match v {
            Value::Static(val) => val.as_bool(),
            _ => None,
        })
        .unwrap_or(false);

    let mut props = ctx
        .tree
        .get(node_id)
        .map(|n| n.props.clone())
        .unwrap_or_else(|| std::sync::Arc::new(indexmap::IndexMap::new()));
    if is_lazy && !element.ir_children.is_empty() {
        if let Some(IRNode::Element(first_child)) = element.ir_children.first() {
            // Copy-on-write: only clone the map if it's still shared with
            // the InstanceNode we pulled it from.
            std::sync::Arc::make_mut(&mut props).insert(
                "__lazy_child".to_string(),
                serde_json::json!(first_child.element_type),
            );
        }
    }
    ctx.patches
        .push(Patch::create(node_id, element.element_type.clone(), props));

    // Tree-side: hang the node off its logical parent.
    if let Some(parent) = logical_parent {
        ctx.tree.add_child(parent, node_id, None);
    }

    // Patch-side: emit Insert into render_parent when set; otherwise
    // fall back to "root" (for root-level children of a control-flow
    // container whose own NodeId is not known to the renderer) before
    // finally falling through to the logical parent (which only fires
    // when render_parent was omitted by a caller that passed the same
    // parent for both).
    if let Some(rp) = render_parent {
        ctx.patches.push(Patch::insert(rp, node_id, None));
    } else if is_root {
        ctx.patches.push(Patch::insert_root(node_id));
    } else if let Some(lp) = logical_parent {
        ctx.patches.push(Patch::insert(lp, node_id, None));
    }

    // Recurse into children (skipped for lazy elements). Module-scoped
    // state propagates so `${state.x}` inside the subtree resolves
    // against the right module slot.
    if !is_lazy {
        let old_state = ctx.state;
        ctx.state = effective_state;
        for child_ir in &element.ir_children {
            create_ir_node_tree_impl(ctx, child_ir, Some(node_id), false);
        }
        ctx.state = old_state;
    }

    node_id
}

/// Create an iterable element (List, Grid, etc.) that iterates over an array in state
fn create_list_tree_impl(
    ctx: &mut ReconcileCtx,
    element: &Element,
    parent_id: Option<NodeId>,
    is_root: bool,
) -> NodeId {
    let module_scope_ref = ctx.effective_scope(element.module_scope.as_deref());
    let effective_state = ctx.effective_state(element.module_scope.as_deref());

    // Get the array binding from first prop (prop "0")
    let array = if let Some(Value::Binding(binding)) = element.props.get("0") {
        evaluate_binding(binding, effective_state).unwrap_or(serde_json::Value::Array(vec![]))
    } else {
        serde_json::Value::Array(vec![])
    };

    // Create a container element - use the original element type (List, Grid, etc.)
    // but remove the "0" prop since it's only for iteration, not rendering
    let mut list_element = Element::new(&element.element_type);
    for (key, value) in &element.props {
        if key != "0" {
            list_element.props.insert(key.clone(), value.clone());
        }
    }

    let node_id = ctx
        .tree
        .create_node_full(&list_element, effective_state, ctx.data_sources);

    // Register the List node as depending on the array binding
    if let Some(Value::Binding(binding)) = element.props.get("0") {
        ctx.dependencies
            .add_dependency(node_id, binding, module_scope_ref);
    }

    // Store the original element template for re-reconciliation
    if let Some(node) = ctx.tree.get_mut(node_id) {
        node.raw_props = element.props.clone();
        node.element_template = Some(std::sync::Arc::new(element.clone()));
    }

    // Generate Create patch for container
    let node = ctx.tree.get(node_id).unwrap();
    ctx.patches.push(Patch::create(
        node_id,
        node.element_type.clone(),
        node.props.clone(),
    ));

    // Insert container
    if let Some(parent) = parent_id {
        ctx.tree.add_child(parent, node_id, None);
        ctx.patches.push(Patch::insert(parent, node_id, None));
    } else if is_root {
        ctx.patches.push(Patch::insert_root(node_id));
    }

    // Create children for each item in the array. Stamp every per-template
    // child with a key so the next reconcile can match by identity instead
    // of position.
    if let serde_json::Value::Array(items) = &array {
        let key_path = element.props.get("key.0").and_then(|v| match v {
            Value::Static(serde_json::Value::String(s)) => Some(s.as_str()),
            _ => None,
        });
        let multi_template = element.ir_children.len() > 1;

        for (index, item) in items.iter().enumerate() {
            let item_key = generate_item_key(item, key_path, "item", index);

            for (template_idx, child_ir) in element.ir_children.iter().enumerate() {
                let child_key = if multi_template {
                    format!("{}#{}", item_key, template_idx)
                } else {
                    item_key.clone()
                };
                let child_with_item = replace_ir_node_item_bindings(
                    child_ir, item, index, "item", &item_key,
                );
                let child_id = create_ir_node_tree_impl(
                    ctx,
                    &child_with_item,
                    Some(node_id),
                    false,
                );
                if let Some(child_node) = ctx.tree.get_mut(child_id) {
                    child_node.key = Some(child_key);
                }
            }
        }
    }

    node_id
}

/// Reconcile an existing tree node against a new `Element`.
///
/// Inlined into [`reconcile_ir_node_impl`]'s `IRNode::Element` arm — there
/// is no public Element-only entry point any more, so this stays private
/// and lives next to the IR dispatcher that calls it.
fn reconcile_element_node(ctx: &mut ReconcileCtx, node_id: NodeId, element: &Element) {
    let node = match ctx.tree.get(node_id).cloned() {
        Some(n) => n,
        None => return,
    };

    let effective_state = ctx.effective_state(element.module_scope.as_deref());
    let module_scope_ref = element.module_scope.as_deref();

    // Special handling for iterable elements (List, Grid, …) — the source
    // array binding lives in props["0"] and the per-item template is in
    // ir_children.
    let is_iterable = element.props.get("0").is_some() && !element.ir_children.is_empty();

    if is_iterable {
        let array = if let Some(Value::Binding(binding)) = element.props.get("0") {
            evaluate_binding(binding, effective_state).unwrap_or(serde_json::Value::Array(vec![]))
        } else {
            serde_json::Value::Array(vec![])
        };

        if let serde_json::Value::Array(items) = &array {
            // Look for an explicit `key:` prop on the iterable element so
            // `Grid(@items, key: "uuid")` overrides the default id auto-detect.
            let key_path = element.props.get("key.0").and_then(|v| match v {
                Value::Static(serde_json::Value::String(s)) => Some(s.as_str()),
                _ => None,
            });

            reconcile_iterable_children(
                ctx,
                node_id,
                items,
                "item",
                key_path,
                &element.ir_children,
            );
        }

        return;
    }

    // If element type changed, replace the entire subtree.
    if node.element_type != element.element_type {
        replace_subtree_impl(ctx, node_id, &node, element);
        return;
    }

    // Register dependencies for every binding in the new props.
    for value in element.props.values() {
        match value {
            Value::Binding(binding) => {
                ctx.dependencies
                    .add_dependency(node_id, binding, module_scope_ref);
            }
            Value::TemplateString { bindings, .. } => {
                for binding in bindings {
                    ctx.dependencies
                        .add_dependency(node_id, binding, module_scope_ref);
                }
            }
            _ => {}
        }
    }

    // Diff and apply prop changes.
    let new_props = resolve_props_full(&element.props, effective_state, None, ctx.data_sources);
    let prop_patches = diff_props(node_id, &node.props, &new_props);
    ctx.patches.extend(prop_patches);

    if let Some(node) = ctx.tree.get_mut(node_id) {
        node.props = new_props; // move the Arc directly — no extra clone
        node.raw_props = element.props.clone();
    }

    // Reconcile children (skip when this element is lazy — the renderer
    // hasn't asked for the subtree yet).
    let is_lazy = element
        .props
        .get("__lazy")
        .and_then(|v| match v {
            Value::Static(val) => val.as_bool(),
            _ => None,
        })
        .unwrap_or(false);

    if !is_lazy {
        let old_children = node.children.clone();
        let new_children = &element.ir_children;

        for (i, new_child_ir) in new_children.iter().enumerate() {
            if let Some(&old_child_id) = old_children.get(i) {
                reconcile_ir_node_impl(ctx, old_child_id, new_child_ir);
            } else {
                create_ir_node_tree_impl(ctx, new_child_ir, Some(node_id), false);
            }
        }

        if old_children.len() > new_children.len() {
            for old_child_id in old_children.iter().skip(new_children.len()).copied() {
                let subtree_ids = collect_subtree_ids(ctx.tree, old_child_id);
                for &id in &subtree_ids {
                    ctx.patches.push(Patch::remove(id));
                    ctx.dependencies.remove_node(id);
                }
                ctx.tree.remove_child(node_id, old_child_id);
                ctx.tree.remove(old_child_id);
            }
        }
    }
}

/// Replace an entire subtree when element types don't match.
fn replace_subtree_impl(
    ctx: &mut ReconcileCtx,
    old_node_id: NodeId,
    old_node: &super::InstanceNode,
    new_element: &Element,
) {
    let parent_id = old_node.parent;

    let old_position = if let Some(pid) = parent_id {
        ctx.tree
            .get(pid)
            .and_then(|parent| parent.children.iter().position(|&id| id == old_node_id))
    } else {
        None
    };

    let ids_to_remove = collect_subtree_ids(ctx.tree, old_node_id);

    for &id in &ids_to_remove {
        ctx.patches.push(Patch::remove(id));
        ctx.dependencies.remove_node(id);
    }

    if let Some(pid) = parent_id {
        if let Some(parent) = ctx.tree.get_mut(pid) {
            parent.children = parent
                .children
                .iter()
                .filter(|&&id| id != old_node_id)
                .copied()
                .collect();
        }
    }

    ctx.tree.remove(old_node_id);

    let is_root = parent_id.is_none();
    let new_node_id = create_element_node(ctx, new_element, parent_id, parent_id, is_root);

    if is_root {
        ctx.tree.set_root(new_node_id);
    } else if let Some(pid) = parent_id {
        if let Some(pos) = old_position {
            if let Some(parent) = ctx.tree.get_mut(pid) {
                let current_len = parent.children.len();
                if pos < current_len - 1 {
                    let new_id = parent.children.pop_back().unwrap();
                    parent.children.insert(pos, new_id);
                    let next_sibling = parent.children.get(pos + 1).copied();
                    ctx.patches
                        .push(Patch::move_node(pid, new_node_id, next_sibling));
                }
            }
        }
    }
}

/// Collect all node IDs in a subtree (post-order: children before parents)
fn collect_subtree_ids(tree: &InstanceTree, root_id: NodeId) -> Vec<NodeId> {
    let mut result = Vec::new();
    let mut stack: Vec<(NodeId, bool)> = vec![(root_id, false)];

    while let Some((node_id, children_processed)) = stack.pop() {
        if children_processed {
            result.push(node_id);
        } else {
            stack.push((node_id, true));
            if let Some(node) = tree.get(node_id) {
                for &child_id in node.children.iter().rev() {
                    stack.push((child_id, false));
                }
            }
        }
    }

    result
}

/// Diff two sets of props and generate SetProp/RemoveProp patches
pub fn diff_props(
    node_id: NodeId,
    old_props: &IndexMap<String, serde_json::Value>,
    new_props: &IndexMap<String, serde_json::Value>,
) -> Vec<Patch> {
    let mut patches = Vec::new();

    for (key, new_value) in new_props {
        if old_props.get(key) != Some(new_value) {
            patches.push(Patch::set_prop(node_id, key.clone(), new_value.clone()));
        }
    }

    for key in old_props.keys() {
        if !new_props.contains_key(key) {
            patches.push(Patch::remove_prop(node_id, key.clone()));
        }
    }

    patches
}

// ============================================================================
// IRNode-based reconciliation (first-class control flow constructs)
// ============================================================================

/// Create a tree from an IRNode using a `ReconcileCtx`.
///
/// Convenience wrapper that uses the same node for both logical (tree)
/// and render (patch) parents — the common case.
pub(crate) fn create_ir_node_tree_impl(
    ctx: &mut ReconcileCtx,
    node: &IRNode,
    parent_id: Option<NodeId>,
    is_root: bool,
) -> NodeId {
    create_ir_node_tree_full(ctx, node, parent_id, parent_id, is_root)
}

/// Create a tree from an IRNode with separate logical and render parents.
///
/// `logical_parent` controls where the new node lives in the instance
/// tree; `render_parent` controls which parent the `Insert` patch
/// references. They diverge for control-flow children — a ForEach item
/// is logically owned by the ForEach container, but its `Insert` patch
/// targets the ForEach's grandparent because the renderer treats the
/// container as transparent.
fn create_ir_node_tree_full(
    ctx: &mut ReconcileCtx,
    node: &IRNode,
    logical_parent: Option<NodeId>,
    render_parent: Option<NodeId>,
    is_root: bool,
) -> NodeId {
    match node {
        IRNode::Element(element) => {
            create_element_node(ctx, element, logical_parent, render_parent, is_root)
        }
        IRNode::ForEach { .. } | IRNode::Conditional { .. } | IRNode::Router { .. } => {
            // Control-flow containers always render under the logical
            // parent — they don't take a render-parent split themselves.
            create_control_flow_tree(ctx, node, logical_parent, is_root)
        }
    }
}

/// Create a ForEach or Conditional tree from an IRNode, destructuring inside.
fn create_control_flow_tree(
    ctx: &mut ReconcileCtx,
    node: &IRNode,
    parent_id: Option<NodeId>,
    is_root: bool,
) -> NodeId {
    match node {
        IRNode::ForEach { .. } => create_foreach_ir_tree(
            ctx,
            node,
            parent_id,
            is_root,
        ),
        IRNode::Conditional {
            value,
            branches,
            fallback,
            ..
        } => create_conditional_tree(
            ctx,
            value,
            branches,
            fallback.as_deref(),
            node,
            parent_id,
            is_root,
        ),
        IRNode::Router {
            location,
            routes,
            fallback,
            ..
        } => create_router_tree(
            ctx,
            location,
            routes,
            fallback.as_deref(),
            node,
            parent_id,
            is_root,
        ),
        IRNode::Element(_) => unreachable!("create_control_flow_tree called with Element"),
    }
}

/// Create a ForEach iteration tree from IRNode::ForEach
fn create_foreach_ir_tree(
    ctx: &mut ReconcileCtx,
    node: &IRNode,
    parent_id: Option<NodeId>,
    is_root: bool,
) -> NodeId {
    let (source, item_name, key_path, template, props, raw_scope) = match node {
        IRNode::ForEach {
            source,
            item_name,
            key_path,
            template,
            props,
            module_scope,
        } => (
            source,
            item_name.as_str(),
            key_path.as_deref(),
            template.as_slice(),
            props,
            module_scope.as_deref(),
        ),
        _ => unreachable!("create_foreach_ir_tree called with non-ForEach node"),
    };

    // Resolve scope: only "real" if a named module is registered.
    let module_scope_ref = ctx.effective_scope(raw_scope);
    let effective_state = ctx.effective_state(raw_scope);

    let array =
        evaluate_binding(source, effective_state).unwrap_or(serde_json::Value::Array(vec![]));

    let resolved_props = resolve_props_full(props, effective_state, None, ctx.data_sources);

    let node_id = ctx.tree.create_control_flow_node(
        "__ForEach",
        resolved_props,
        props.clone(),
        ControlFlowKind::ForEach {
            item_name: item_name.to_string(),
            key_path: key_path.map(|s| s.to_string()),
        },
        node.clone(),
    );

    ctx.dependencies
        .add_dependency(node_id, source, module_scope_ref);

    if let Some(parent) = parent_id {
        ctx.tree.add_child(parent, node_id, None);
    }

    let render_parent = parent_id;

    if let serde_json::Value::Array(items) = &array {
        for (index, item) in items.iter().enumerate() {
            let item_key = generate_item_key(item, key_path, item_name, index);

            for child_template in template {
                let child_with_item = replace_ir_node_item_bindings(
                    child_template,
                    item,
                    index,
                    item_name,
                    &item_key,
                );
                create_ir_node_tree_full(
                    ctx,
                    &child_with_item,
                    Some(node_id),
                    render_parent,
                    is_root && render_parent.is_none(),
                );
            }
        }
    }

    node_id
}

/// Create a Conditional (When/If) tree from IRNode::Conditional
fn create_conditional_tree(
    ctx: &mut ReconcileCtx,
    value: &Value,
    branches: &[crate::ir::ConditionalBranch],
    fallback: Option<&[IRNode]>,
    original_node: &IRNode,
    parent_id: Option<NodeId>,
    is_root: bool,
) -> NodeId {
    let raw_scope = match original_node {
        IRNode::Conditional { module_scope, .. } => module_scope.as_deref(),
        _ => None,
    };
    let module_scope_ref = ctx.effective_scope(raw_scope);
    let effective_state = ctx.effective_state(raw_scope);

    let evaluated_value = evaluate_value(value, effective_state, ctx.data_sources);

    let mut raw_props = Props::new();
    raw_props.insert("__condition".to_string(), value.clone());

    let node_id = ctx.tree.create_control_flow_node(
        "__Conditional",
        std::sync::Arc::new(IndexMap::new()),
        raw_props,
        ControlFlowKind::Conditional,
        original_node.clone(),
    );

    if let Value::Binding(binding) = value {
        ctx.dependencies
            .add_dependency(node_id, binding, module_scope_ref);
    } else if let Value::TemplateString { bindings, .. } = value {
        for binding in bindings {
            ctx.dependencies
                .add_dependency(node_id, binding, module_scope_ref);
        }
    }

    if let Some(parent) = parent_id {
        ctx.tree.add_child(parent, node_id, None);
    }

    let matched_children =
        find_matching_branch(&evaluated_value, branches, fallback, effective_state, ctx.data_sources);

    let render_parent = parent_id;

    if let Some(children) = matched_children {
        for child in children {
            create_ir_node_tree_full(
                ctx,
                child,
                Some(node_id),
                render_parent,
                is_root && render_parent.is_none(),
            );
        }
    }

    node_id
}

/// Create a Router tree from IRNode::Router.
///
/// Mirrors `create_conditional_tree`: builds a single `__Router` control-flow
/// node, registers a dependency on the location binding, picks the matching
/// route, and renders only that route's children. The renderer never sees
/// `Router` or `Route` element types — it just sees the matched children
/// inserted under the Router's render parent.
fn create_router_tree(
    ctx: &mut ReconcileCtx,
    location: &Value,
    routes: &[RouterRoute],
    fallback: Option<&[IRNode]>,
    original_node: &IRNode,
    parent_id: Option<NodeId>,
    is_root: bool,
) -> NodeId {
    let raw_scope = match original_node {
        IRNode::Router { module_scope, .. } => module_scope.as_deref(),
        _ => None,
    };
    let module_scope_ref = ctx.effective_scope(raw_scope);
    let effective_state = ctx.effective_state(raw_scope);

    // Resolve location to a string. Anything that isn't a string falls back
    // to the empty path so the fallback (or no route) is selected.
    let evaluated = evaluate_value(location, effective_state, ctx.data_sources);
    let location_str = match &evaluated {
        serde_json::Value::String(s) => s.clone(),
        serde_json::Value::Null => String::new(),
        other => other.to_string(),
    };

    let mut raw_props = Props::new();
    raw_props.insert("__location".to_string(), location.clone());

    // Seed the Router with an empty cache and the initial route key
    // (filled in once we know which route matched below).
    let node_id = ctx.tree.create_control_flow_node(
        "__Router",
        std::sync::Arc::new(IndexMap::new()),
        raw_props,
        ControlFlowKind::Router {
            cache: IndexMap::new(),
            current_route_key: None,
            max_cache_size: DEFAULT_ROUTER_CACHE_SIZE,
        },
        original_node.clone(),
    );

    // Register dependency on the location binding so updates to state.location
    // dirty this Router node and trigger reconciliation.
    if let Value::Binding(binding) = location {
        ctx.dependencies
            .add_dependency(node_id, binding, module_scope_ref);
    } else if let Value::TemplateString { bindings, .. } = location {
        for binding in bindings {
            ctx.dependencies
                .add_dependency(node_id, binding, module_scope_ref);
        }
    }

    if let Some(parent) = parent_id {
        ctx.tree.add_child(parent, node_id, None);
    }

    // Find the matched route (with its pattern key) so we can remember
    // which route produced the current children. On the next location
    // change, the Router reconciler will cache *these* NodeIds under
    // that key before swapping in new children.
    let matched = find_matching_route_with_key(&location_str, routes, fallback);
    let render_parent = parent_id;

    if let Some((route_key, children)) = matched.as_ref() {
        for child in *children {
            create_ir_node_tree_full(
                ctx,
                child,
                Some(node_id),
                render_parent,
                is_root && render_parent.is_none(),
            );
        }

        // Record the route we just rendered so the reconciler knows
        // which cache bucket to populate on navigation away.
        if let Some(router_node) = ctx.tree.get_mut(node_id) {
            if let Some(ControlFlowKind::Router {
                current_route_key, ..
            }) = router_node.control_flow.as_mut()
            {
                *current_route_key = Some(route_key.clone());
            }
        }
    }

    node_id
}

/// Reconcile an existing tree against a new IRNode using a `ReconcileCtx`.
pub(crate) fn reconcile_ir_node_impl(ctx: &mut ReconcileCtx, node_id: NodeId, node: &IRNode) {
    let existing_node = ctx.tree.get(node_id).cloned();
    if existing_node.is_none() {
        return;
    }
    let existing = existing_node.unwrap();

    match node {
        IRNode::Element(element) => {
            reconcile_element_node(ctx, node_id, element);
        }
        IRNode::ForEach {
            source,
            item_name,
            key_path,
            template,
            props: _,
            module_scope,
        } => {
            if !existing.is_foreach() {
                let parent_id = existing.parent;
                remove_subtree(ctx.tree, node_id, ctx.patches, ctx.dependencies);
                create_ir_node_tree_impl(ctx, node, parent_id, parent_id.is_none());
                return;
            }

            let module_scope_ref = ctx.effective_scope(module_scope.as_deref());
            let effective_state = ctx.effective_state(module_scope.as_deref());

            ctx.dependencies
                .add_dependency(node_id, source, module_scope_ref);

            let array = evaluate_binding(source, effective_state)
                .unwrap_or(serde_json::Value::Array(vec![]));

            if let serde_json::Value::Array(items) = &array {
                let old_children = existing.children.clone();
                let expected_children_count = items.len() * template.len();
                let render_parent = existing.parent.unwrap_or(node_id);

                if old_children.len() != expected_children_count {
                    for &old_child_id in &old_children {
                        ctx.patches.push(Patch::remove(old_child_id));
                    }

                    if let Some(node) = ctx.tree.get_mut(node_id) {
                        node.children.clear();
                    }

                    for (index, item) in items.iter().enumerate() {
                        let item_key =
                            generate_item_key(item, key_path.as_deref(), item_name, index);

                        for child_template in template {
                            let child_with_item = replace_ir_node_item_bindings(
                                child_template,
                                item,
                                index,
                                item_name,
                                &item_key,
                            );
                            create_ir_node_tree_impl(
                                ctx,
                                &child_with_item,
                                Some(render_parent),
                                false,
                            );
                        }
                    }
                } else {
                    let mut child_index = 0;
                    for (item_index, item) in items.iter().enumerate() {
                        let item_key =
                            generate_item_key(item, key_path.as_deref(), item_name, item_index);

                        for child_template in template {
                            if let Some(&old_child_id) = old_children.get(child_index) {
                                let child_with_item = replace_ir_node_item_bindings(
                                    child_template,
                                    item,
                                    item_index,
                                    item_name,
                                    &item_key,
                                );
                                reconcile_ir_node_impl(ctx, old_child_id, &child_with_item);
                            }
                            child_index += 1;
                        }
                    }
                }
            }
        }
        IRNode::Conditional {
            value,
            branches,
            fallback,
            module_scope,
        } => {
            if !existing.is_conditional() {
                let parent_id = existing.parent;
                remove_subtree(ctx.tree, node_id, ctx.patches, ctx.dependencies);
                create_ir_node_tree_impl(ctx, node, parent_id, parent_id.is_none());
                return;
            }

            let module_scope_ref = ctx.effective_scope(module_scope.as_deref());
            let effective_state = ctx.effective_state(module_scope.as_deref());

            if let Value::Binding(binding) = value {
                ctx.dependencies
                    .add_dependency(node_id, binding, module_scope_ref);
            } else if let Value::TemplateString { bindings, .. } = value {
                for binding in bindings {
                    ctx.dependencies
                        .add_dependency(node_id, binding, module_scope_ref);
                }
            }

            let evaluated_value = evaluate_value(value, effective_state, ctx.data_sources);
            let matched_children = find_matching_branch(
                &evaluated_value,
                branches,
                fallback.as_deref(),
                effective_state,
                ctx.data_sources,
            );

            let old_children = existing.children.clone();
            let old_len = old_children.len();
            let render_parent = existing.parent;

            if let Some(children) = matched_children {
                let new_len = children.len();
                let common = old_len.min(new_len);

                for (i, child) in children.iter().enumerate().take(common) {
                    if let Some(&old_child_id) = old_children.get(i) {
                        reconcile_ir_node_impl(ctx, old_child_id, child);
                    }
                }

                for i in common..old_len {
                    if let Some(&old_child_id) = old_children.get(i) {
                        remove_subtree(ctx.tree, old_child_id, ctx.patches, ctx.dependencies);
                        if let Some(cond_node) = ctx.tree.get_mut(node_id) {
                            cond_node.children = cond_node
                                .children
                                .iter()
                                .filter(|&&id| id != old_child_id)
                                .copied()
                                .collect();
                        }
                    }
                }

                let children_is_root = render_parent.is_none();
                for child in &children[common..] {
                    create_ir_node_tree_full(
                        ctx,
                        child,
                        Some(node_id),
                        render_parent,
                        children_is_root,
                    );
                }
            } else {
                for &old_child_id in &old_children {
                    remove_subtree(ctx.tree, old_child_id, ctx.patches, ctx.dependencies);
                }

                if let Some(cond_node) = ctx.tree.get_mut(node_id) {
                    cond_node.children.clear();
                }
            }
        }
        IRNode::Router {
            location,
            routes,
            fallback,
            module_scope,
        } => {
            // If the existing node isn't a Router, replace it wholesale.
            if !existing.is_router() {
                let parent_id = existing.parent;
                remove_subtree(ctx.tree, node_id, ctx.patches, ctx.dependencies);
                create_ir_node_tree_impl(ctx, node, parent_id, parent_id.is_none());
                return;
            }

            let module_scope_ref = ctx.effective_scope(module_scope.as_deref());
            let effective_state = ctx.effective_state(module_scope.as_deref());

            // Re-register the location dependency in case it was cleared.
            if let Value::Binding(binding) = location {
                ctx.dependencies
                    .add_dependency(node_id, binding, module_scope_ref);
            } else if let Value::TemplateString { bindings, .. } = location {
                for binding in bindings {
                    ctx.dependencies
                        .add_dependency(node_id, binding, module_scope_ref);
                }
            }

            let evaluated = evaluate_value(location, effective_state, ctx.data_sources);
            let location_str = match &evaluated {
                serde_json::Value::String(s) => s.clone(),
                serde_json::Value::Null => String::new(),
                other => other.to_string(),
            };

            // Read the current cache state out of the existing node. Each
            // Router instance carries its own detached-subtree cache keyed
            // by route pattern (see ControlFlowKind::Router).
            let (mut cache, prev_route_key, max_cache_size) =
                match existing.control_flow.as_ref() {
                    Some(ControlFlowKind::Router {
                        cache,
                        current_route_key,
                        max_cache_size,
                    }) => (cache.clone(), current_route_key.clone(), *max_cache_size),
                    _ => (IndexMap::new(), None, DEFAULT_ROUTER_CACHE_SIZE),
                };

            let matched = find_matching_route_with_key(
                &location_str,
                routes,
                fallback.as_deref(),
            );
            let new_route_key = matched.as_ref().map(|(k, _)| k.clone());

            // Same route as before — nothing structural to do. Descendant
            // nodes get their own dirty marks when state changes; the
            // Router only reconciles when the *route* changes.
            if prev_route_key.is_some() && prev_route_key == new_route_key {
                return;
            }

            let render_parent = existing.parent;
            let old_children: Vec<NodeId> = existing.children.iter().copied().collect();

            // Step 1: take the currently-rendered children off the Router.
            //   - If we have a prev_route_key → detach them and stash under
            //     that key (keep-alive: nodes, deps, props all live on).
            //   - If we don't (first reconcile after a cache miss or
            //     mismatched prior state) → fall back to hard teardown so
            //     we don't leak orphans.
            if let Some(prev_key) = prev_route_key.as_ref() {
                for &child_id in &old_children {
                    // Emit a Detach patch so the renderer unlinks its
                    // native node but keeps it alive. The engine-side
                    // node stays in the tree; its descendants stay
                    // under it; dependencies stay registered so state
                    // updates flow through while off-screen.
                    ctx.patches.push(Patch::detach(child_id));
                    if let Some(child) = ctx.tree.get_mut(child_id) {
                        child.parent = None;
                    }
                }
                if !old_children.is_empty() {
                    // Replace any prior entry for this key (e.g. if we
                    // navigated away, back, and away again).
                    cache.shift_remove(prev_key);
                    cache.insert(prev_key.clone(), old_children);
                }
            } else {
                for &old_child_id in &old_children {
                    remove_subtree(ctx.tree, old_child_id, ctx.patches, ctx.dependencies);
                }
            }

            if let Some(router_node) = ctx.tree.get_mut(node_id) {
                router_node.children.clear();
            }

            // Step 2: LRU-evict old cache entries if we exceeded the cap.
            // Dropping an entry means its subtree is gone for good, so
            // we tear it down properly (frees nodes + deps).
            while cache.len() > max_cache_size {
                let evicted_key = cache.keys().next().cloned();
                if let Some(evicted_key) = evicted_key {
                    if let Some(evicted_ids) = cache.shift_remove(&evicted_key) {
                        for evicted_id in evicted_ids {
                            remove_subtree(
                                ctx.tree,
                                evicted_id,
                                ctx.patches,
                                ctx.dependencies,
                            );
                        }
                    }
                } else {
                    break;
                }
            }

            // Step 3: attach the new route's subtree — reuse cached
            // nodes if we've seen this route before, otherwise build
            // from scratch.
            if let Some(new_key) = new_route_key.as_ref() {
                if let Some(cached_ids) = cache.shift_remove(new_key) {
                    // Cache hit: reattach the detached subtrees in order.
                    // When the Router is itself the IR root, its own
                    // NodeId is a control-flow pseudo-node the renderer
                    // never created — attach to "root" in that case.
                    for cached_id in &cached_ids {
                        if let Some(child) = ctx.tree.get_mut(*cached_id) {
                            child.parent = Some(node_id);
                        }
                        if let Some(router_node) = ctx.tree.get_mut(node_id) {
                            router_node.children.push_back(*cached_id);
                        }
                        let attach_patch = match render_parent {
                            Some(rp) => Patch::attach(rp, *cached_id, None),
                            None => Patch::attach_root(*cached_id, None),
                        };
                        ctx.patches.push(attach_patch);
                    }
                } else if let Some((_, children)) = matched.as_ref() {
                    // Cache miss: build fresh IR tree for this route.
                    // When the Router is at the IR root (render_parent
                    // is None) the children must be inserted at "root";
                    // that's what `is_root` signals to create_element_node.
                    let children_is_root = render_parent.is_none();
                    for child in *children {
                        create_ir_node_tree_full(
                            ctx,
                            child,
                            Some(node_id),
                            render_parent,
                            children_is_root,
                        );
                    }
                }
            }

            // Step 4: write the updated cache + current route back to
            // the Router node. If nothing matched (no route, no
            // fallback) we keep the cache but clear current_route_key
            // so the next reconcile treats this as a fresh state.
            if let Some(router_node) = ctx.tree.get_mut(node_id) {
                router_node.control_flow = Some(ControlFlowKind::Router {
                    cache,
                    current_route_key: new_route_key,
                    max_cache_size,
                });
            }
        }
    }
}

/// Remove a subtree and generate Remove patches
fn remove_subtree(
    tree: &mut InstanceTree,
    node_id: NodeId,
    patches: &mut Vec<Patch>,
    dependencies: &mut DependencyGraph,
) {
    let ids = collect_subtree_ids(tree, node_id);
    for &id in &ids {
        patches.push(Patch::remove(id));
        dependencies.remove_node(id);
    }
    tree.remove(node_id);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::Value;
    use serde_json::json;

    #[test]
    fn test_create_simple_tree() {
        use crate::reactive::DependencyGraph;

        let mut tree = InstanceTree::new();
        let mut patches = Vec::new();
        let mut dependencies = DependencyGraph::new();

        let element = Element::new("Column")
            .with_child(Element::new("Text").with_prop("text", Value::Static(json!("Hello"))));

        let state = json!({});
        let mut ctx = ReconcileCtx {
            tree: &mut tree,
            state: &state,
            patches: &mut patches,
            dependencies: &mut dependencies,
            data_sources: None,
            modules: None,
        };
        create_element_node(&mut ctx, &element, None, None, true);

        // Should create 2 nodes (Column + Text) + 2 Inserts (root + child)
        // Create Column, Insert Column into root, Create Text, Insert Text into Column
        assert_eq!(patches.len(), 4);

        // Verify root insert patch exists
        let root_insert = patches
            .iter()
            .find(|p| matches!(p, Patch::Insert { parent_id, .. } if parent_id == "root"));
        assert!(root_insert.is_some(), "Root insert patch should exist");
    }

    #[test]
    fn test_diff_props() {
        let node_id = NodeId::default();
        let old = indexmap::indexmap! {
            "color".to_string() => json!("red"),
            "size".to_string() => json!(16),
        };
        let new = indexmap::indexmap! {
            "color".to_string() => json!("blue"),
            "size".to_string() => json!(16),
        };

        let patches = diff_props(node_id, &old, &new);

        // Only color changed
        assert_eq!(patches.len(), 1);
    }
}