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
use floating_ui_utils::{
    get_alignment, get_alignment_sides, get_expanded_placements, get_opposite_axis_placements,
    get_opposite_placement, get_side, get_side_axis, Alignment, Axis, Placement,
};
use serde::{Deserialize, Serialize};

use crate::{
    detect_overflow::{detect_overflow, DetectOverflowOptions},
    middleware::arrow::{ArrowData, ARROW_NAME},
    types::{
        Derivable, DerivableFn, Middleware, MiddlewareReturn, MiddlewareState,
        MiddlewareWithOptions, Reset, ResetValue,
    },
};

/// Name of the [`Flip`] middleware.
pub const FLIP_NAME: &str = "flip";

/// Fallback strategy used by [`Flip`] middleware.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub enum FallbackStrategy {
    #[default]
    BestFit,
    InitialPlacement,
}

/// Options for [`Flip`] middleware.
#[derive(Clone, Debug, PartialEq)]
pub struct FlipOptions<Element: Clone> {
    /// Options for [`detect_overflow`].
    ///
    /// Defaults to [`DetectOverflowOptions::default`].
    pub detect_overflow: Option<DetectOverflowOptions<Element>>,

    /// The axis that runs along the side of the floating element. Determines whether overflow along this axis is checked to perform a flip.
    ///
    /// Defaults to `true`.
    pub main_axis: Option<bool>,

    /// The axis that runs along the alignment of the floating element. Determines whether overflow along this axis is checked to perform a flip.
    ///
    /// Defaults to `true`.
    pub cross_axis: Option<bool>,

    /// Placements to try sequentially if the preferred `placement` does not fit.
    ///
    /// Defaults to the opposite placement.
    pub fallback_placements: Option<Vec<Placement>>,

    /// What strategy to use when no placements fit.
    ///
    /// Defaults to [`FallbackStrategy::BestFit`].
    pub fallback_strategy: Option<FallbackStrategy>,

    /// Whether to allow fallback to the perpendicular axis of the preferred placement, and if so, which side direction along the axis to prefer.
    ///
    /// Defaults to [`Option::None`] (disallow fallback).
    pub fallback_axis_side_direction: Option<Alignment>,

    /// Whether to flip to placements with the opposite alignment if they fit better.
    ///
    /// Defaults to `true`.
    pub flip_alignment: Option<bool>,
}

impl<Element: Clone> FlipOptions<Element> {
    /// Set `detect_overflow` option.
    pub fn detect_overflow(mut self, value: DetectOverflowOptions<Element>) -> Self {
        self.detect_overflow = Some(value);
        self
    }

    /// Set `main_axis` option.
    pub fn main_axis(mut self, value: bool) -> Self {
        self.main_axis = Some(value);
        self
    }

    /// Set `cross_axis` option.
    pub fn cross_axis(mut self, value: bool) -> Self {
        self.cross_axis = Some(value);
        self
    }

    /// Set `fallback_placements` option.
    pub fn fallback_placements(mut self, value: Vec<Placement>) -> Self {
        self.fallback_placements = Some(value);
        self
    }

    /// Set `fallback_strategy` option.
    pub fn fallback_strategy(mut self, value: FallbackStrategy) -> Self {
        self.fallback_strategy = Some(value);
        self
    }

    /// Set `fallback_axis_side_direction` option.
    pub fn fallback_axis_side_direction(mut self, value: Alignment) -> Self {
        self.fallback_axis_side_direction = Some(value);
        self
    }

    /// Set `flip_alignment` option.
    pub fn flip_alignment(mut self, value: bool) -> Self {
        self.flip_alignment = Some(value);
        self
    }
}

impl<Element: Clone> Default for FlipOptions<Element> {
    fn default() -> Self {
        Self {
            detect_overflow: Default::default(),
            main_axis: Default::default(),
            cross_axis: Default::default(),
            fallback_placements: Default::default(),
            fallback_strategy: Default::default(),
            fallback_axis_side_direction: Default::default(),
            flip_alignment: Default::default(),
        }
    }
}

/// An overflow stored in [`FlipData`].
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct FlipDataOverflow {
    pub placement: Placement,
    pub overflows: Vec<f64>,
}

/// Data stored by [`Flip`] middleware.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct FlipData {
    pub index: usize,
    pub overflows: Vec<FlipDataOverflow>,
}

/// Flip middleware.
///
/// Optimizes the visibility of the floating element by flipping the `placement` in order to keep it in view when the preferred placement(s) will overflow the clipping boundary.
/// Alternative to [`AutoPlacement`][`crate::middleware::AutoPlacement`].
///
/// See [the Rust Floating UI book](https://floating-ui.rustforweb.org/middleware/flip.html) for more documentation.
#[derive(PartialEq)]
pub struct Flip<'a, Element: Clone + 'static, Window: Clone> {
    options: Derivable<'a, Element, Window, FlipOptions<Element>>,
}

impl<'a, Element: Clone + 'static, Window: Clone> Flip<'a, Element, Window> {
    /// Constructs a new instance of this middleware.
    pub fn new(options: FlipOptions<Element>) -> Self {
        Flip {
            options: options.into(),
        }
    }

    /// Constructs a new instance of this middleware with derivable options.
    pub fn new_derivable(options: Derivable<'a, Element, Window, FlipOptions<Element>>) -> Self {
        Flip { options }
    }

    /// Constructs a new instance of this middleware with derivable options function.
    pub fn new_derivable_fn(
        options: DerivableFn<'a, Element, Window, FlipOptions<Element>>,
    ) -> Self {
        Flip {
            options: options.into(),
        }
    }
}

impl<'a, Element: Clone + 'static, Window: Clone> Clone for Flip<'a, Element, Window> {
    fn clone(&self) -> Self {
        Self {
            options: self.options.clone(),
        }
    }
}

impl<Element: Clone + PartialEq, Window: Clone + PartialEq> Middleware<Element, Window>
    for Flip<'static, Element, Window>
{
    fn name(&self) -> &'static str {
        FLIP_NAME
    }

    fn compute(&self, state: MiddlewareState<Element, Window>) -> MiddlewareReturn {
        let options = self.options.evaluate(state.clone());

        let MiddlewareState {
            placement,
            initial_placement,
            middleware_data,
            elements,
            rects,
            platform,
            ..
        } = state;

        let data: FlipData = middleware_data.get_as(self.name()).unwrap_or(FlipData {
            index: 0,
            overflows: vec![],
        });

        let check_main_axis = options.main_axis.unwrap_or(true);
        let check_cross_axis = options.cross_axis.unwrap_or(true);
        let specified_fallback_placements = options.fallback_placements.clone();
        let fallback_strategy = options.fallback_strategy.unwrap_or_default();
        let fallback_axis_side_direction = options.fallback_axis_side_direction;
        let flip_alignment = options.flip_alignment.unwrap_or(true);

        // If a reset by the arrow was caused due to an alignment offset being added,
        // we should skip any logic now since `flip()` has already done its work.
        let arrow_data: Option<ArrowData> = middleware_data.get_as(ARROW_NAME);
        if arrow_data.map_or(false, |arrow_data| arrow_data.alignment_offset.is_some()) {
            return MiddlewareReturn {
                x: None,
                y: None,
                data: None,
                reset: None,
            };
        }

        let side = get_side(placement);
        let initial_side_axis = get_side_axis(initial_placement);
        let is_base_placement = get_alignment(initial_placement).is_none();
        let rtl = platform.is_rtl(elements.floating);

        let has_specified_fallback_placements = specified_fallback_placements.is_some();
        let mut placements =
            specified_fallback_placements.unwrap_or(match is_base_placement || !flip_alignment {
                true => vec![get_opposite_placement(initial_placement)],
                false => get_expanded_placements(initial_placement),
            });

        let has_fallback_axis_side_direction = fallback_axis_side_direction.is_some();

        if !has_specified_fallback_placements && has_fallback_axis_side_direction {
            placements.append(&mut get_opposite_axis_placements(
                initial_placement,
                flip_alignment,
                fallback_axis_side_direction,
                rtl,
            ));
        }

        placements.insert(0, initial_placement);

        let overflow = detect_overflow(
            MiddlewareState {
                elements: elements.clone(),
                ..state
            },
            options.detect_overflow.unwrap_or_default(),
        );

        let mut overflows: Vec<f64> = Vec::new();
        let mut overflows_data = data.overflows;

        if check_main_axis {
            overflows.push(overflow.side(side));
        }
        if check_cross_axis {
            let sides = get_alignment_sides(placement, rects, rtl);
            overflows.push(overflow.side(sides.0));
            overflows.push(overflow.side(sides.1));
        }

        overflows_data.push(FlipDataOverflow {
            placement,
            overflows: overflows.clone(),
        });

        // One or more sides is overflowing.
        if !overflows.into_iter().all(|side| side <= 0.0) {
            let next_index = data.index + 1;
            let next_placement = placements.get(next_index);

            if let Some(next_placement) = next_placement {
                // Try next placement and re-run the lifecycle.
                return MiddlewareReturn {
                    x: None,
                    y: None,
                    data: Some(
                        serde_json::to_value(FlipData {
                            index: next_index,
                            overflows: overflows_data,
                        })
                        .expect("Data should be valid JSON."),
                    ),
                    reset: Some(Reset::Value(ResetValue {
                        placement: Some(*next_placement),
                        rects: None,
                    })),
                };
            }

            // First, find the candidates that fit on the main axis side of overflow, then find the placement that fits the best on the main cross axis side.
            let mut reset_placement: Vec<&FlipDataOverflow> = overflows_data
                .iter()
                .filter(|overflow| overflow.overflows[0] <= 0.0)
                .collect();
            reset_placement.sort_by(|a, b| a.overflows[1].total_cmp(&b.overflows[1]));

            let mut reset_placement = reset_placement.first().map(|overflow| overflow.placement);

            // Otherwise fallback.
            if reset_placement.is_none() {
                match fallback_strategy {
                    FallbackStrategy::BestFit => {
                        let mut placement: Vec<(Placement, f64)> = overflows_data
                            .into_iter()
                            .filter(|overflow| {
                                if has_fallback_axis_side_direction {
                                    let current_side_axis = get_side_axis(overflow.placement);

                                    // Create a bias to the `y` side axis due to horizontal reading directions favoring greater width.
                                    current_side_axis == initial_side_axis
                                        || current_side_axis == Axis::Y
                                } else {
                                    true
                                }
                            })
                            .map(|overflow| {
                                (
                                    overflow.placement,
                                    overflow
                                        .overflows
                                        .into_iter()
                                        .filter(|overflow| *overflow > 0.0)
                                        .sum::<f64>(),
                                )
                            })
                            .collect();
                        placement.sort_by(|a, b| a.1.total_cmp(&b.1));

                        let placement = placement.first().map(|v| v.0);
                        if placement.is_some() {
                            reset_placement = placement;
                        }
                    }
                    FallbackStrategy::InitialPlacement => {
                        reset_placement = Some(initial_placement);
                    }
                }
            }

            if placement != reset_placement.expect("Reset placement is not none.") {
                return MiddlewareReturn {
                    x: None,
                    y: None,
                    data: None,
                    reset: Some(Reset::Value(ResetValue {
                        placement: reset_placement,
                        rects: None,
                    })),
                };
            }
        }

        MiddlewareReturn {
            x: None,
            y: None,
            data: None,
            reset: None,
        }
    }
}

impl<'a, Element: Clone, Window: Clone> MiddlewareWithOptions<Element, Window, FlipOptions<Element>>
    for Flip<'a, Element, Window>
{
    fn options(&self) -> &Derivable<Element, Window, FlipOptions<Element>> {
        &self.options
    }
}