1
//! One lookup flow for palette args naming accounts or commodities by label:
2
//! fetch the option lists the typed values need, resolve them against the
3
//! form descriptor, then hand the resolved values to the feature that asked.
4

            
5
use cli_core::eval::escape_str;
6
use cli_core::forms::{Fetched, map_values, parse_form_options, sources_used};
7
use forms::{Form, FormValues, Source};
8

            
9
use super::App;
10
use crate::route::RouteCtx;
11
use crate::tabs::list::ListTab;
12
use crate::tabs::reports::ReportKind;
13
use crate::tabs::seq::Seq;
14
use crate::window::{Origin, WindowId};
15

            
16
/// The feature a lookup serves; each counts its own lookups to drop stale replies.
17
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18
pub enum Goal {
19
    /// Filters the Transactions window.
20
    Filter(WindowId),
21
    /// Opens the merge form over the Accounts window.
22
    Merge(Origin),
23
    /// Prints the balances in the Console window.
24
    Balance(Origin),
25
    Report {
26
        kind: ReportKind,
27
        interactive: bool,
28
        origin: Origin,
29
    },
30
}
31

            
32
/// Typed values waiting for the options of their select fields.
33
pub struct Lookup {
34
    seq: u64,
35
    goal: Goal,
36
    form: &'static Form,
37
    values: FormValues,
38
    pending: Vec<Source>,
39
    fetched: Fetched,
40
}
41

            
42
impl App {
43
    /// Resolve the select values of `values` over `form`, then complete `goal`.
44
105
    pub(super) fn start_lookup(&mut self, goal: Goal, form: &'static Form, values: FormValues) {
45
105
        let Some(seq) = self.lookup_seq(goal).map(Seq::advance) else {
46
            return;
47
        };
48
105
        let pending = sources_used(form, &values);
49
105
        self.advance_lookup(Lookup {
50
105
            seq,
51
105
            goal,
52
105
            form,
53
105
            values,
54
105
            pending,
55
105
            fetched: Fetched::new(),
56
105
        });
57
105
    }
58

            
59
165
    fn lookup_seq(&mut self, goal: Goal) -> Option<&mut Seq> {
60
165
        match goal {
61
33
            Goal::Filter(window) => self
62
33
                .win_mut::<ListTab>(window)
63
33
                .map(|list| &mut list.filter_seq),
64
56
            Goal::Merge(_) => Some(&mut self.merge_seq),
65
10
            Goal::Balance(_) => Some(&mut self.balance_seq),
66
66
            Goal::Report { origin, .. } => self.report_seq_of(origin.window),
67
        }
68
165
    }
69

            
70
60
    fn lookup_awaited(&mut self, lookup: &Lookup) -> bool {
71
60
        let awaits = |seq: &mut Seq| seq.awaits(lookup.seq);
72
60
        self.lookup_seq(lookup.goal).is_some_and(awaits)
73
60
    }
74

            
75
156
    fn advance_lookup(&mut self, mut lookup: Lookup) {
76
156
        let Some(source) = lookup.pending.pop() else {
77
87
            return self.finish_lookup(lookup);
78
        };
79
69
        let call = format!("(form-options {})", escape_str(source.name()));
80
69
        let ctx = RouteCtx::Lookup { lookup, source };
81
69
        if self.dispatch_app_eval(ctx, call).is_none() {
82
8
            self.set_status("console not connected");
83
61
        }
84
156
    }
85

            
86
60
    pub(super) fn deliver_lookup(&mut self, mut lookup: Lookup, source: Source, wire: &str) {
87
60
        if !self.lookup_awaited(&lookup) {
88
8
            return;
89
52
        }
90
52
        if let Err(e) = cli_core::render::parse_wire(wire) {
91
1
            self.set_status(format!("command error: {e}"));
92
1
            return;
93
51
        }
94
51
        lookup.fetched.push((source, parse_form_options(wire)));
95
51
        self.advance_lookup(lookup);
96
60
    }
97

            
98
87
    fn finish_lookup(&mut self, lookup: Lookup) {
99
        let Lookup {
100
            goal,
101
            form,
102
            values,
103
            fetched,
104
            ..
105
87
        } = lookup;
106
87
        match map_values(form, &values, &fetched, "") {
107
78
            Ok(resolved) => self.complete_lookup(goal, resolved, &fetched),
108
9
            Err(e) => self.set_status(format!("command error: {e}")),
109
        }
110
87
    }
111

            
112
78
    fn complete_lookup(&mut self, goal: Goal, resolved: FormValues, fetched: &Fetched) {
113
78
        match goal {
114
13
            Goal::Filter(window) => self.apply_resolved_filter(window, &resolved, fetched),
115
24
            Goal::Merge(origin) => self.open_resolved_merge(origin, &resolved, fetched),
116
4
            Goal::Balance(origin) => self.print_resolved_balance(origin, &resolved),
117
            Goal::Report {
118
                kind,
119
                interactive,
120
                origin,
121
37
            } => self.launch_report(origin, kind, &resolved, interactive),
122
        }
123
78
    }
124
}