orrery 0.4.1

A diagram language for creating component and sequence diagrams
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
//! Positioned diagram elements and arrow placement.
//!
//! A [`Component`] wraps a semantic node with its computed position and shape.
//! An [`ArrowPlacer`] decides how relations between the same component pair
//! are turned into visually distinct arrow paths (straight overlap vs. offset
//! cubic-Bézier lanes).

use std::{collections::HashMap, rc::Rc};

use itertools::Itertools;
use log::{debug, error};

use orrery_core::{
    draw::{
        Arrow, ArrowPath, ArrowStyle, ArrowWithText, PositionedArrowWithText, PositionedDrawable,
        ShapeWithText,
    },
    geometry::{Bounds, Point},
    identifier::Id,
    semantic::{Node, Relation},
};

use crate::{
    error::RenderError,
    layout::{layer::ContentStack, positioning::LayoutBounds},
    structure::ComponentGraph,
};

// TODO: Do I need Clone?!
// TODO: Find a better name and location for this struct.
/// A positioned diagram component linking a semantic node to its rendered shape and location.
#[derive(Debug, Clone)]
pub struct Component<'a> {
    node_id: Id,                                         // TODO: Can I get rid of this?
    drawable: Rc<PositionedDrawable<ShapeWithText<'a>>>, // TODO: Consider removing Rc.
}

impl<'a> Component<'a> {
    /// Creates a new component with the specified properties.
    pub fn new(node: &Node, shape_with_text: ShapeWithText<'a>, position: Point) -> Component<'a> {
        let drawable = Rc::new(PositionedDrawable::new(shape_with_text).with_position(position));
        Component {
            node_id: node.id(),
            drawable,
        }
    }

    /// Returns a reference to the component's shape.
    pub fn drawable(&self) -> &PositionedDrawable<ShapeWithText<'_>> {
        &self.drawable
    }

    /// Returns the center position of the component.
    ///
    /// The position represents the center point of the component in the layout
    /// coordinate system.
    pub fn position(&self) -> Point {
        self.drawable.position()
    }

    /// Calculates the bounds of this component.
    ///
    /// The position is treated as the center of the component,
    /// and the bounds extend half the width/height in each direction.
    pub fn bounds(&self) -> Bounds {
        self.drawable.bounds()
    }

    /// Returns the unique identifier of the AST node this component represents.
    // TODO: Can I get rid of this method?
    pub fn node_id(&self) -> Id {
        self.node_id
    }

    /// Calculates the intersection point where a line from this component's center
    /// to an external point crosses this component's shape boundary.
    ///
    /// # Arguments
    ///
    /// * `external_point` - The point to draw a line toward from this component's center.
    ///
    /// # Returns
    ///
    /// The point on this component's shape boundary where the line exits.
    pub fn find_intersection(&self, external_point: Point) -> Point {
        self.drawable
            .inner()
            .find_intersection(self.position(), external_point)
    }
}

/// Strategy for routing a bucket of relations between the same unordered
/// component pair into positioned arrows.
///
/// # Implementing
///
/// - `source` is the component whose [`node_id`](Component::node_id) is the
///   *canonical* end of the pair, and `target` is the other end. For
///   self-loop buckets the same [`Component`] is passed for both.
/// - Relations within the bucket may go either direction; inspect
///   [`Relation::source`](semantic::Relation::source) to determine orientation.
/// - Return exactly one arrow per input relation.
pub trait ArrowPlacer {
    /// Places a bucket of relations between the same component pair.
    fn place<'a>(
        &self,
        relations: &[&'a Relation],
        source: &Component<'_>,
        target: &Component<'_>,
    ) -> Vec<PositionedArrowWithText<'a>>;
}

/// [`ArrowPlacer`] that emits a straight centerline path for every relation.
///
/// Parallel/reverse relations overlap into a single line. Exists as the
/// behaviour-preserving baseline; [`CurvedArrowPlacer`] is the default.
#[derive(Debug, Default, Clone, Copy)]
pub struct StraightArrowPlacer;

impl StraightArrowPlacer {
    /// Straight-line arrow from `source` boundary to `target` boundary.
    fn place_one<'a>(
        relation: &'a Relation,
        source: &Component<'_>,
        target: &Component<'_>,
    ) -> PositionedArrowWithText<'a> {
        let arrow_def = Rc::clone(relation.arrow_definition());
        let arrow = Arrow::new(arrow_def, relation.arrow_direction());
        let arrow_with_text = ArrowWithText::new(arrow, relation.text());

        let source_edge = source.find_intersection(target.position());
        let target_edge = target.find_intersection(source.position());
        let path = ArrowPath::straight(source_edge, target_edge);

        PositionedArrowWithText::new(arrow_with_text, path)
    }
}

impl ArrowPlacer for StraightArrowPlacer {
    fn place<'a>(
        &self,
        relations: &[&'a Relation],
        source: &Component<'_>,
        target: &Component<'_>,
    ) -> Vec<PositionedArrowWithText<'a>> {
        relations
            .iter()
            .map(|relation| {
                let (rel_src, rel_tgt) = align_to_relation(relation, source, target);
                Self::place_one(relation, rel_src, rel_tgt)
            })
            .collect()
    }
}

/// [`ArrowPlacer`] that places parallel/reverse relations on offset lanes
/// encoded as cubic Bézier curves.
///
/// Each relation at bucket position `k` of `N` gets lane offset
/// `(k - (N-1)/2) * lane_spacing`. Reverse-direction relations have their
/// offset sign-flipped so `a -> b` and `b -> a` curve to opposite sides.
///
/// Lane endpoints are found by aiming each shape's intersection ray at the
/// offset center of the other shape; control points sit at 1/3 and 2/3 of
/// the resulting lane line. For `N == 1` the offset is zero, producing a
/// straight path identical to [`StraightArrowPlacer`].
/// Self-loop buckets produce lobes distributed evenly around the component
/// boundary (see [`Self::self_loop_angle`]).
#[derive(Debug, Clone, Copy)]
pub struct CurvedArrowPlacer {
    lane_spacing: f32,
    self_loop_radius: f32,
    /// Angular spread (radians) between self-loop source and destination
    /// boundary points.
    self_loop_spread: f32,
}

impl CurvedArrowPlacer {
    const DEFAULT_LANE_SPACING: f32 = 22.0;
    const DEFAULT_SELF_LOOP_RADIUS: f32 = 35.0;
    const DEFAULT_SELF_LOOP_SPREAD: f32 = std::f32::consts::FRAC_PI_6;

    pub fn new() -> Self {
        Self {
            lane_spacing: Self::DEFAULT_LANE_SPACING,
            self_loop_radius: Self::DEFAULT_SELF_LOOP_RADIUS,
            self_loop_spread: Self::DEFAULT_SELF_LOOP_SPREAD,
        }
    }

    /// Packages `lane_geometry` output into a [`PositionedArrowWithText`](PositionedArrowWithText).
    fn curved_arrow<'a>(
        relation: &'a Relation,
        source: &Component<'_>,
        target: &Component<'_>,
        lane_offset: f32,
    ) -> PositionedArrowWithText<'a> {
        let Some((path, label_position)) = Self::lane_geometry(source, target, lane_offset) else {
            // Degenerate (zero-length centerline): fall back to straight.
            return StraightArrowPlacer::place_one(relation, source, target);
        };

        let arrow_def = Rc::clone(relation.arrow_definition());
        let arrow = Arrow::new(arrow_def, relation.arrow_direction());
        let arrow_with_text = ArrowWithText::new(arrow, relation.text());

        PositionedArrowWithText::new(arrow_with_text, path).with_text_position(Some(label_position))
    }

    /// Computes the cubic-Bézier path and label position for a lane offset.
    ///
    /// Returns `None` when component centers coincide (zero-length centerline).
    fn lane_geometry(
        source: &Component<'_>,
        target: &Component<'_>,
        lane_offset: f32,
    ) -> Option<(ArrowPath, Point)> {
        let src_center = source.position();
        let tgt_center = target.position();

        let delta = tgt_center.sub_point(src_center);
        let len = delta.hypot();
        if len == 0.0 {
            return None;
        }
        let perp = Point::new(
            -delta.y() / len * lane_offset,
            delta.x() / len * lane_offset,
        );

        let midpoint = src_center.midpoint(tgt_center).add_point(perp);
        let src_edge = source.find_intersection(midpoint);
        let tgt_edge = target.find_intersection(midpoint);

        // Control points at 1/3 and 2/3 of src_edge→tgt_edge, offset perpendicular.
        let (third, two_thirds) = line_segment_thirds(src_edge, tgt_edge);
        let cp1 = third.add_point(perp);
        let cp2 = two_thirds.add_point(perp);

        let path = ArrowPath::new(src_edge, tgt_edge, vec![cp1, cp2]);
        let label_position = cubic_bezier_midpoint(src_edge, cp1, cp2, tgt_edge);
        Some((path, label_position))
    }

    /// Lane offset for position `k` of `n`, sign-flipped for reverse relations.
    fn lane_offset_at(&self, k: usize, n: usize, relation: &Relation, source_id: Id) -> f32 {
        let offset = ((k as f32) - ((n - 1) as f32) / 2.0) * self.lane_spacing;
        if relation.source() != source_id {
            -offset
        } else {
            offset
        }
    }

    /// Outward angle (radians) for the `k`-th self-loop out of `n` total.
    fn self_loop_angle(k: usize, n: usize) -> f32 {
        debug_assert_ne!(n, 0, "n must be greater than 0");
        (k as f32) * std::f32::consts::TAU / (n as f32)
    }

    /// Teardrop-shaped Bézier lobe path for a self-loop at `angle`.
    fn self_loop_geometry(&self, component: &Component<'_>, angle: f32) -> (ArrowPath, Point) {
        let center = component.position();
        let direction = Point::new(angle.cos(), angle.sin());
        let component_size = component.bounds().to_size();

        // A large radius guarantees find_intersection hits the boundary.
        let far = (component_size.height().abs() + component_size.width().abs()) * 2.0;

        // Source exits above (clockwise-before) the lobe center;
        // destination enters below (clockwise-after).
        let half_spread = self.self_loop_spread / 2.0;
        let src_angle = angle - half_spread;
        let dst_angle = angle + half_spread;
        let src_dir = Point::new(src_angle.cos(), src_angle.sin());
        let dst_dir = Point::new(dst_angle.cos(), dst_angle.sin());

        let src_far = center.add_point(src_dir.scale(far));
        let dst_far = center.add_point(dst_dir.scale(far));

        let src_edge = component.find_intersection(src_far);
        let dst_edge = component.find_intersection(dst_far);

        // Control points push outward by `self_loop_radius` from each exit
        // point, in the lobe’s main direction.
        let outward = direction.scale(self.self_loop_radius);
        let cp1 = src_edge.add_point(outward);
        let cp2 = dst_edge.add_point(outward);

        let path = ArrowPath::new(src_edge, dst_edge, vec![cp1, cp2]);
        let label_position = cubic_bezier_midpoint(src_edge, cp1, cp2, dst_edge);

        (path, label_position)
    }

    /// Positioned self-loop arrow at the given outward `angle`.
    fn self_loop_arrow<'a>(
        &self,
        relation: &'a Relation,
        component: &Component<'_>,
        angle: f32,
    ) -> PositionedArrowWithText<'a> {
        let (path, label_position) = self.self_loop_geometry(component, angle);

        let arrow_def = Rc::clone(relation.arrow_definition());
        let arrow = Arrow::new(arrow_def, relation.arrow_direction());
        let arrow_with_text = ArrowWithText::new(arrow, relation.text());

        PositionedArrowWithText::new(arrow_with_text, path).with_text_position(Some(label_position))
    }
}

impl Default for CurvedArrowPlacer {
    fn default() -> Self {
        Self::new()
    }
}

impl ArrowPlacer for CurvedArrowPlacer {
    fn place<'a>(
        &self,
        relations: &[&'a Relation],
        source: &Component<'_>,
        target: &Component<'_>,
    ) -> Vec<PositionedArrowWithText<'a>> {
        let n = relations.len();
        if source.node_id() == target.node_id() {
            relations
                .iter()
                .enumerate()
                .map(|(k, relation)| {
                    let angle = Self::self_loop_angle(k, n);
                    self.self_loop_arrow(relation, source, angle)
                })
                .collect()
        } else {
            relations
                .iter()
                .enumerate()
                .map(|(k, relation)| {
                    let (rel_src, rel_tgt) = align_to_relation(relation, source, target);

                    let lane_offset = self.lane_offset_at(k, n, relation, source.node_id());

                    if lane_offset == 0.0 {
                        // Median lane (or N == 1): straight line.
                        return StraightArrowPlacer::place_one(relation, rel_src, rel_tgt);
                    }

                    Self::curved_arrow(relation, rel_src, rel_tgt, lane_offset)
                })
                .collect()
        }
    }
}

/// [`ArrowPlacer`] that routes every relation as an orthogonal path made of
/// horizontal and vertical segments.
///
/// Each path turns once at the midpoint, going horizontal-first when the pair
/// is wider than it is tall and vertical-first otherwise. Like
/// [`StraightArrowPlacer`], parallel/reverse relations overlap into a single
/// path; it does not offset them onto separate lanes.
#[derive(Debug, Default, Clone, Copy)]
pub struct OrthogonalArrowPlacer;

impl OrthogonalArrowPlacer {
    /// Builds the right-angle [`ArrowPath`] between two boundary points.
    fn path(source: Point, target: Point) -> ArrowPath {
        let abs_dist = target.sub_point(source).abs();
        let mid = source.midpoint(target);

        let cps = if abs_dist.x() > abs_dist.y() {
            vec![mid.with_y(source.y()), mid.with_y(target.y())]
        } else {
            vec![mid.with_x(source.x()), mid.with_x(target.x())]
        };
        ArrowPath::new(source, target, cps)
    }

    /// Orthogonal arrow from `source` boundary to `target` boundary.
    fn place_one<'a>(
        relation: &'a Relation,
        source: &Component<'_>,
        target: &Component<'_>,
    ) -> PositionedArrowWithText<'a> {
        let arrow_def = Rc::clone(relation.arrow_definition());
        let arrow = Arrow::new(arrow_def, relation.arrow_direction());
        let arrow_with_text = ArrowWithText::new(arrow, relation.text());

        let source_edge = source.find_intersection(target.position());
        let target_edge = target.find_intersection(source.position());
        let path = Self::path(source_edge, target_edge);

        PositionedArrowWithText::new(arrow_with_text, path)
    }
}

impl ArrowPlacer for OrthogonalArrowPlacer {
    fn place<'a>(
        &self,
        relations: &[&'a Relation],
        source: &Component<'_>,
        target: &Component<'_>,
    ) -> Vec<PositionedArrowWithText<'a>> {
        relations
            .iter()
            .map(|relation| {
                let (rel_src, rel_tgt) = align_to_relation(relation, source, target);
                Self::place_one(relation, rel_src, rel_tgt)
            })
            .collect()
    }
}

/// [`ArrowPlacer`] that dispatches each relation to a concrete placer based on
/// its [`ArrowStyle`].
///
/// Non-self-loop relations are grouped by style and routed by
/// [`StraightArrowPlacer`], [`CurvedArrowPlacer`], or [`OrthogonalArrowPlacer`]
/// accordingly. Self-loops are always routed by [`CurvedArrowPlacer`]
/// regardless of style.
///
/// # Known limitations
///
/// Because relations are grouped by style before routing, each delegate only
/// sees the relations of its own style. [`CurvedArrowPlacer`] therefore lanes
/// parallel edges only within the curved subset, so mixed-style parallel edges
/// between the same pair may overlap. Output order is not preserved across
/// style groups.
#[derive(Debug, Default, Clone, Copy)]
pub struct SmartArrowPlacer {
    straight: StraightArrowPlacer,
    curved: CurvedArrowPlacer,
    orthogonal: OrthogonalArrowPlacer,
}

impl SmartArrowPlacer {
    /// Creates a new `SmartArrowPlacer` with default delegate placers.
    pub fn new() -> Self {
        Self::default()
    }
}

impl ArrowPlacer for SmartArrowPlacer {
    fn place<'a>(
        &self,
        relations: &[&'a Relation],
        source: &Component<'_>,
        target: &Component<'_>,
    ) -> Vec<PositionedArrowWithText<'a>> {
        if source.node_id() == target.node_id() {
            return self.curved.place(relations, source, target);
        }
        let grouped = relations
            .iter()
            .copied()
            .into_group_map_by(|r| r.arrow_definition().style());
        grouped
            .into_iter()
            .flat_map(|(style, relations)| match style {
                ArrowStyle::Straight => self.straight.place(&relations, source, target),
                ArrowStyle::Curved => self.curved.place(&relations, source, target),
                ArrowStyle::Orthogonal => self.orthogonal.place(&relations, source, target),
            })
            .collect()
    }
}

/// A complete layout of components and their relationships.
///
/// A `Layout` contains all the positioned components and their connecting relations
/// for a diagram. It provides methods to access related components and calculate
/// overall layout dimensions.
#[derive(Debug, Clone)]
pub struct Layout<'a> {
    components: Vec<Component<'a>>,
    relations: Vec<PositionedArrowWithText<'a>>,
    bounds: Bounds,
}

impl<'a> Layout<'a> {
    /// Creates a new layout with the given components and relations.
    pub fn new(
        components: Vec<Component<'a>>,
        relations: Vec<PositionedArrowWithText<'a>>,
    ) -> Self {
        let bounds = if components.is_empty() {
            Bounds::default()
        } else {
            components
                .iter()
                .skip(1)
                .fold(components[0].bounds(), |acc, comp| {
                    acc.merge(&comp.bounds())
                })
        };

        Self {
            components,
            relations,
            bounds,
        }
    }

    /// Returns a reference to the components in this layout.
    pub fn components(&self) -> &[Component<'a>] {
        &self.components
    }

    /// Returns a reference to the relations in this layout.
    pub fn relations(&self) -> &[PositionedArrowWithText<'a>] {
        &self.relations
    }
}

impl<'a> LayoutBounds for Layout<'a> {
    fn layout_bounds(&self) -> Bounds {
        self.bounds
    }
}

/// Adjusts the offset of positioned contents in a content stack based on containment relationships.
///
/// This function handles the proper positioning of nested elements within their containers.
///
/// # Arguments
/// * `content_stack` - Mutable reference to the content stack containing all layout layers
/// * `graph` - Reference to the containment graph that defines parent-child relationships
///
/// # Behavior
/// The function processes containment scopes in reverse order to ensure proper nesting.
/// For each nested element, it:
/// 1. Finds the container component in the source layer
/// 2. Calculates the target offset based on the container's bounds and shape properties
/// 3. Updates the destination layer's offset to position the nested content correctly
///
/// # Errors
/// Returns `RenderError::Layout` if a component referenced in the containment graph
/// is not found in its corresponding layout layer.
// TODO: Once added enough abstractions, make this a method on ContentStack.
pub fn adjust_positioned_contents_offset<'a>(
    content_stack: &mut ContentStack<Layout>,
    graph: &'a ComponentGraph<'a, '_>,
) -> Result<(), RenderError> {
    let container_indices: HashMap<_, _> = graph
        .containment_scopes()
        .enumerate()
        .filter_map(|(idx, scope)| scope.container().map(|container| (container, idx)))
        .collect();

    for (source_idx, source_scope) in graph.containment_scopes().enumerate().rev() {
        for (node_id, destination_idx) in source_scope.node_ids().filter_map(|node_id| {
            container_indices
                .get(&node_id)
                .map(|&destination_idx| (node_id, destination_idx))
        }) {
            if source_idx == destination_idx {
                // If the source and destination are the same, skip
                error!(index = source_idx; "Source and destination indices are the same");
                continue;
            }
            let source = content_stack.get_unchecked(source_idx);
            let node = graph.node_by_id(node_id).ok_or_else(|| {
                RenderError::Layout(format!(
                    "Node with id {node_id} not found in graph during layout adjustment"
                ))
            })?;

            // Find the component in the source layer that matches the node
            let source_component = source
                .content()
                .components()
                .iter()
                .find(|component| component.node_id == node.id())
                .ok_or_else(|| {
                    RenderError::Layout(format!(
                        "Component with id {node} not found in source layer {source_idx}"
                    ))
                })?;
            let target_offset = source
                .offset()
                .add_point(source_component.bounds().min_point())
                .add_point(
                    source_component
                        .drawable
                        .inner()
                        .shape_to_inner_content_min_point(),
                ); // TODO: This does not account for text.
            debug!(
                node_id:% = node,
                source_offset:? = source.offset();
                "Adjusting positioned content offset [source]",
            );
            let target = content_stack.get_mut_unchecked(destination_idx);
            debug!(
                node_id:% = node,
                original_offset:? = target.offset(),
                new_offset:? = target_offset;
                "Adjusting positioned content offset [target]",
            );
            target.set_offset(target_offset);
        }
    }
    Ok(())
}

/// Returns `(source, target)` or `(target, source)` depending on which
/// component matches `relation.source()`.
fn align_to_relation<'a, 'b>(
    relation: &Relation,
    source: &'b Component<'a>,
    target: &'b Component<'a>,
) -> (&'b Component<'a>, &'b Component<'a>) {
    if relation.source() == source.node_id() {
        (source, target)
    } else {
        (target, source)
    }
}

fn line_segment_thirds(start: Point, end: Point) -> (Point, Point) {
    let dx = (end.x() - start.x()) / 3.0;
    let dy = (end.y() - start.y()) / 3.0;
    (
        Point::new(start.x() + dx, start.y() + dy),
        Point::new(start.x() + 2.0 * dx, start.y() + 2.0 * dy),
    )
}

fn cubic_bezier_midpoint(start: Point, cp1: Point, cp2: Point, end: Point) -> Point {
    Point::new(
        0.125 * start.x() + 0.375 * cp1.x() + 0.375 * cp2.x() + 0.125 * end.x(),
        0.125 * start.y() + 0.375 * cp1.y() + 0.375 * cp2.y() + 0.125 * end.y(),
    )
}

#[cfg(test)]
mod tests {
    use std::f32::consts::{FRAC_PI_2, PI};

    use float_cmp::{approx_eq, assert_approx_eq};

    use orrery_core::{
        draw::{ArrowDefinition, ArrowDirection, RectangleDefinition, Shape, ShapeDefinition},
        semantic::Block,
    };

    use super::*;

    fn assert_point_approx_eq(actual: Point, expected: Point) {
        assert!(
            approx_eq!(f32, actual.x(), expected.x(), epsilon = 0.01)
                && approx_eq!(f32, actual.y(), expected.y(), epsilon = 0.01),
            "expected ({}, {}), got ({}, {})",
            expected.x(),
            expected.y(),
            actual.x(),
            actual.y(),
        );
    }

    fn make_relation(source: Id, target: Id) -> Relation {
        Relation::new(
            source,
            target,
            ArrowDirection::Forward,
            None,
            Rc::new(ArrowDefinition::default()),
        )
    }

    fn make_relation_with_style(source: Id, target: Id, style: ArrowStyle) -> Relation {
        let mut arrow_def = ArrowDefinition::default();
        arrow_def.set_style(style);
        Relation::new(
            source,
            target,
            ArrowDirection::Forward,
            None,
            Rc::new(arrow_def),
        )
    }

    fn make_node(name: &str) -> Node {
        let id = Id::new(name);
        let shape_def = Rc::new(Box::new(RectangleDefinition::new()) as Box<dyn ShapeDefinition>);
        Node::new(id, None, Block::None, shape_def)
    }

    fn make_component<'a>(node: &'a Node, position: Point) -> Component<'a> {
        let shape = Shape::new(Rc::clone(node.shape_definition()));
        let shape_with_text = ShapeWithText::new(shape, None);
        Component::new(node, shape_with_text, position)
    }

    #[test]
    fn line_segment_thirds_returns_third_points() {
        let (cp1, cp2) = line_segment_thirds(Point::new(0.0, 0.0), Point::new(30.0, 60.0));
        assert_eq!(cp1, Point::new(10.0, 20.0));
        assert_eq!(cp2, Point::new(20.0, 40.0));
    }

    #[test]
    fn cubic_bezier_midpoint_at_half() {
        // For a degenerate cubic (control points colinear with endpoints), the
        // parametric midpoint coincides with the geometric midpoint.
        let s = Point::new(0.0, 0.0);
        let cp1 = Point::new(30.0, 0.0);
        let cp2 = Point::new(60.0, 0.0);
        let d = Point::new(90.0, 0.0);
        let mid = cubic_bezier_midpoint(s, cp1, cp2, d);
        assert_eq!(mid, Point::new(45.0, 0.0));
    }

    #[test]
    fn lane_offset_at_assigns_symmetric_lanes() {
        let a_id = Id::new("a");
        let b_id = Id::new("b");
        let r0 = make_relation(a_id, b_id);
        let r1 = make_relation(a_id, b_id);
        let router = CurvedArrowPlacer {
            lane_spacing: 10.0,
            ..CurvedArrowPlacer::new()
        };

        let off0 = router.lane_offset_at(0, 2, &r0, a_id);
        let off1 = router.lane_offset_at(1, 2, &r1, a_id);
        assert_eq!(off0, -5.0);
        assert_eq!(off1, 5.0);
    }

    #[test]
    fn lane_offset_at_three_relations_has_zero_median() {
        let a_id = Id::new("a");
        let b_id = Id::new("b");
        let r = make_relation(a_id, b_id);
        let router = CurvedArrowPlacer {
            lane_spacing: 10.0,
            ..CurvedArrowPlacer::new()
        };
        assert_eq!(router.lane_offset_at(1, 3, &r, a_id), 0.0);
        assert_eq!(router.lane_offset_at(2, 3, &r, a_id), 10.0);
    }

    #[test]
    fn lane_offset_at_single_relation_is_zero() {
        let a_id = Id::new("a");
        let b_id = Id::new("b");
        let r = make_relation(a_id, b_id);
        let router = CurvedArrowPlacer::new();
        assert_eq!(router.lane_offset_at(0, 1, &r, a_id), 0.0);
    }

    #[test]
    fn lane_geometry_emits_cubic_bezier_with_two_control_points() {
        // Shape size is 12x12 (half = 6). Components at (0,0) and (1000,0).
        // lane_offset = 18, perp = (0, 18), midpoint = (500, 18).
        // src_edge: ray from (0,0) toward (500,18) exits at x=6 → (6, 0.216).
        // tgt_edge: ray from (1000,0) toward (500,18) exits at x=994 → (994, 0.216).
        // cp1 = 1/3 of edge segment + perp = (335.33, 18.216).
        // cp2 = 2/3 of edge segment + perp = (664.67, 18.216).
        // label = cubic midpoint ≈ (500, 13.716).
        let a_node = make_node("a");
        let b_node = make_node("b");
        let a = make_component(&a_node, Point::new(0.0, 0.0));
        let b = make_component(&b_node, Point::new(1000.0, 0.0));

        let (path, label) =
            CurvedArrowPlacer::lane_geometry(&a, &b, 18.0).expect("non-degenerate centerline");

        assert_eq!(path.control_points().len(), 2);

        assert_point_approx_eq(path.source(), Point::new(6.0, 0.216));
        assert_point_approx_eq(path.destination(), Point::new(994.0, 0.216));
        assert_point_approx_eq(path.control_points()[0], Point::new(335.33, 18.216));
        assert_point_approx_eq(path.control_points()[1], Point::new(664.67, 18.216));
        assert_point_approx_eq(label, Point::new(500.0, 13.716));
    }

    #[test]
    fn lane_geometry_opposite_offsets_produce_mirrored_control_points() {
        let a_node = make_node("a");
        let b_node = make_node("b");
        let a = make_component(&a_node, Point::new(0.0, 0.0));
        let b = make_component(&b_node, Point::new(1000.0, 0.0));

        let (path_pos, _) = CurvedArrowPlacer::lane_geometry(&a, &b, 18.0).unwrap();
        let (path_neg, _) = CurvedArrowPlacer::lane_geometry(&a, &b, -18.0).unwrap();

        let cp1_pos = path_pos.control_points()[0];
        let cp1_neg = path_neg.control_points()[0];
        assert_point_approx_eq(cp1_neg, Point::new(cp1_pos.x(), -cp1_pos.y()));
    }

    #[test]
    fn lane_geometry_offset_scales_with_magnitude() {
        let a_node = make_node("a");
        let b_node = make_node("b");
        let a = make_component(&a_node, Point::new(0.0, 0.0));
        let b = make_component(&b_node, Point::new(1000.0, 0.0));

        let (path_small, _) = CurvedArrowPlacer::lane_geometry(&a, &b, 10.0).unwrap();
        let (path_large, _) = CurvedArrowPlacer::lane_geometry(&a, &b, 40.0).unwrap();

        let cp1_small = path_small.control_points()[0];
        let cp1_large = path_large.control_points()[0];
        // 4x lane offset → 4x perpendicular offset on control points.
        assert_approx_eq!(f32, cp1_large.y() / cp1_small.y(), 4.0);
    }

    #[test]
    fn lane_geometry_returns_none_for_zero_length_centerline() {
        let a_node = make_node("a");
        let a = make_component(&a_node, Point::new(0.0, 0.0));
        assert!(CurvedArrowPlacer::lane_geometry(&a, &a, 18.0).is_none());
    }

    #[test]
    fn self_loop_angle_distributes_evenly() {
        // Single loop → 0° (right).
        assert_approx_eq!(f32, CurvedArrowPlacer::self_loop_angle(0, 1), 0.0);

        // Two loops → 0° and 180°.
        assert_approx_eq!(f32, CurvedArrowPlacer::self_loop_angle(0, 2), 0.0);
        assert_approx_eq!(f32, CurvedArrowPlacer::self_loop_angle(1, 2), PI);

        // Four loops → 0°, 90°, 180°, 270°.
        assert_approx_eq!(f32, CurvedArrowPlacer::self_loop_angle(0, 4), 0.0);
        assert_approx_eq!(f32, CurvedArrowPlacer::self_loop_angle(1, 4), FRAC_PI_2);
        assert_approx_eq!(f32, CurvedArrowPlacer::self_loop_angle(2, 4), PI);
        assert_approx_eq!(
            f32,
            CurvedArrowPlacer::self_loop_angle(3, 4),
            3.0 * FRAC_PI_2
        );
    }

    #[test]
    fn self_loop_geometry_source_and_dest_on_boundary() {
        let node = make_node("a");
        let comp = make_component(&node, Point::new(50.0, 50.0));
        let router = CurvedArrowPlacer::new();

        let (path, _) = router.self_loop_geometry(&comp, 0.0);

        // Shape is 12x12 centered at (50,50). Source and dest should be on the
        // right boundary (x ≈ 56) since angle=0 points right.
        assert_approx_eq!(f32, path.source().x(), 56.0);
        assert_approx_eq!(f32, path.destination().x(), 56.0);

        // Source above center, destination below (spread pushes them apart).
        assert!(path.source().y() < 50.0);
        assert!(path.destination().y() > 50.0);
    }

    #[test]
    fn self_loop_geometry_control_points_outside_bounds() {
        let node = make_node("a");
        let comp = make_component(&node, Point::new(50.0, 50.0));
        let router = CurvedArrowPlacer::new();
        let bounds = comp.bounds();

        let (path, _) = router.self_loop_geometry(&comp, 0.0);

        // Both control points should be to the right of the component bounds.
        for cp in path.control_points() {
            assert!(
                cp.x() > bounds.max_x(),
                "control point x={} should exceed bounds max_x={}",
                cp.x(),
                bounds.max_x()
            );
        }
    }

    #[test]
    fn self_loop_geometry_label_outside_bounds() {
        let node = make_node("a");
        let comp = make_component(&node, Point::new(50.0, 50.0));
        let router = CurvedArrowPlacer::new();
        let bounds = comp.bounds();

        let (_, label) = router.self_loop_geometry(&comp, 0.0);

        // Label at angle=0 should be to the right of the boundary.
        assert!(
            label.x() > bounds.max_x(),
            "label x={} should exceed bounds max_x={}",
            label.x(),
            bounds.max_x()
        );
    }

    #[test]
    fn self_loop_geometry_different_angles_produce_distinct_paths() {
        let node = make_node("a");
        let comp = make_component(&node, Point::new(50.0, 50.0));
        let router = CurvedArrowPlacer::new();

        let (path_right, _) = router.self_loop_geometry(&comp, 0.0);
        let (path_left, _) = router.self_loop_geometry(&comp, PI);

        // Angle=0 exits right (x > center), angle=π exits left (x < center).
        assert!(path_right.source().x() > 50.0);
        assert!(path_left.source().x() < 50.0);
    }

    #[test]
    fn place_produces_one_arrow_per_relation() {
        let a_node = make_node("a");
        let b_node = make_node("b");
        let a = make_component(&a_node, Point::new(0.0, 0.0));
        let b = make_component(&b_node, Point::new(100.0, 0.0));
        let r1 = make_relation(a.node_id(), b.node_id());
        let r2 = make_relation(a.node_id(), b.node_id());
        let r3 = make_relation(b.node_id(), a.node_id());

        let router = CurvedArrowPlacer::new();
        let out = router.place(&[&r1, &r2, &r3], &a, &b);
        assert_eq!(out.len(), 3);

        // Self-loop bucket also returns one arrow per relation.
        let r4 = make_relation(a.node_id(), a.node_id());
        let r5 = make_relation(a.node_id(), a.node_id());
        let out_self = router.place(&[&r4, &r5], &a, &a);
        assert_eq!(out_self.len(), 2);
    }

    #[test]
    fn orthogonal_path_horizontal_first_when_wider() {
        let source = Point::new(0.0, 0.0);
        let target = Point::new(100.0, 20.0);
        let path = OrthogonalArrowPlacer::path(source, target);

        let cps = path.control_points();
        assert_eq!(cps.len(), 2);
        assert_point_approx_eq(cps[0], Point::new(50.0, 0.0));
        assert_point_approx_eq(cps[1], Point::new(50.0, 20.0));
    }

    #[test]
    fn orthogonal_path_vertical_first_when_taller() {
        let source = Point::new(0.0, 0.0);
        let target = Point::new(20.0, 100.0);
        let path = OrthogonalArrowPlacer::path(source, target);

        let cps = path.control_points();
        assert_eq!(cps.len(), 2);
        assert_point_approx_eq(cps[0], Point::new(0.0, 50.0));
        assert_point_approx_eq(cps[1], Point::new(20.0, 50.0));
    }

    #[test]
    fn orthogonal_place_produces_one_arrow_per_relation() {
        let a_node = make_node("a");
        let b_node = make_node("b");
        let a = make_component(&a_node, Point::new(0.0, 0.0));
        let b = make_component(&b_node, Point::new(100.0, 0.0));
        let r1 = make_relation(a.node_id(), b.node_id());
        let r2 = make_relation(b.node_id(), a.node_id());

        let out = OrthogonalArrowPlacer.place(&[&r1, &r2], &a, &b);
        assert_eq!(out.len(), 2);
        // Orthogonal arrows carry two control points each.
        for arrow in &out {
            assert_eq!(arrow.path().control_points().len(), 2);
        }
    }

    #[test]
    fn smart_place_returns_one_arrow_per_relation_for_mixed_styles() {
        let a_node = make_node("a");
        let b_node = make_node("b");
        let a = make_component(&a_node, Point::new(0.0, 0.0));
        let b = make_component(&b_node, Point::new(100.0, 0.0));
        let r_straight = make_relation_with_style(a.node_id(), b.node_id(), ArrowStyle::Straight);
        let r_curved = make_relation_with_style(a.node_id(), b.node_id(), ArrowStyle::Curved);
        let r_orthogonal =
            make_relation_with_style(a.node_id(), b.node_id(), ArrowStyle::Orthogonal);

        let placer = SmartArrowPlacer::new();
        let out = placer.place(&[&r_straight, &r_curved, &r_orthogonal], &a, &b);
        assert_eq!(out.len(), 3);
    }

    #[test]
    fn smart_place_routes_self_loops_through_curved_placer() {
        let a_node = make_node("a");
        let a = make_component(&a_node, Point::new(50.0, 50.0));
        // Even an orthogonal-styled self-loop must be routed as a curve.
        let r = make_relation_with_style(a.node_id(), a.node_id(), ArrowStyle::Orthogonal);

        let placer = SmartArrowPlacer::new();
        let out = placer.place(&[&r], &a, &a);
        assert_eq!(out.len(), 1);
        // Curved self-loops are cubic Béziers (two control points) and carry a
        // label position override, unlike the orthogonal placer.
        assert_eq!(out[0].path().control_points().len(), 2);
    }
}