1use std::collections::BTreeSet;
4
5use cli_core::forms::{FormOption, commodity_fill};
6use forms::{Kind, LINK_CELL, Row, Source};
7
8use super::{
9 COL_FROM, COL_FROM_COMM, COL_TO, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE, columns, tag_fields,
10};
11use crate::widgets::{AmountWidget, RowsWidget, SelectOption, SelectWidget, WidgetKind, with_none};
12
13#[derive(Debug)]
15pub struct SplitRow {
16 pub from: SelectWidget,
17 pub to: SelectWidget,
18 pub from_commodity: SelectWidget,
19 pub to_commodity: SelectWidget,
20 pub value: AmountWidget,
21 pub to_amount: AmountWidget,
22 pub(super) tags: RowsWidget,
24 key: String,
27 hidden: String,
28 loaded: [String; 4],
30 auto: BTreeSet<usize>,
32 from_load: bool,
34}
35
36impl SplitRow {
37 pub(super) fn new(accounts: &[SelectOption], commodities: &[SelectOption]) -> Self {
40 let mut row = Self {
41 from: SelectWidget::new(),
42 to: SelectWidget::new(),
43 from_commodity: SelectWidget::new(),
44 to_commodity: SelectWidget::new(),
45 value: AmountWidget::default(),
46 to_amount: AmountWidget::default(),
47 tags: RowsWidget::new(tag_fields()),
48 key: String::new(),
49 hidden: String::new(),
50 loaded: Default::default(),
51 auto: BTreeSet::new(),
52 from_load: false,
53 };
54 row.from.set_options(accounts.to_vec());
55 row.to.set_options(accounts.to_vec());
56 row.from_commodity
57 .set_options(with_none(commodities.to_vec()));
58 row.to_commodity
59 .set_options(with_empty_meaning(commodities));
60 row
61 }
62
63 pub(super) fn loaded(row: &Row) -> Self {
66 let text = |key: &str| row.get(key).map_or("", String::as_str);
67 let mut loaded = Self::new(&[], &[]);
68 loaded.value = AmountWidget::with_value(text("value"));
69 loaded.to_amount = AmountWidget::with_value(text("to-amount"));
70 loaded.from_load = true;
71 loaded.key = text("key").to_string();
72 loaded.hidden = text("hidden").to_string();
73 loaded.loaded =
74 ["from", "to", "from-commodity", "to-commodity"].map(|k| text(k).to_string());
75 let [from, to, from_commodity, to_commodity] = &loaded.loaded;
76 loaded.from.set_options(seed(from));
77 loaded.to.set_options(seed(to));
78 loaded.from_commodity.set_options(seed(from_commodity));
79 loaded.to_commodity.set_options(seed(to_commodity));
80 loaded
81 }
82
83 pub(super) fn key(&self) -> &str {
84 &self.key
85 }
86
87 pub(super) fn set_key(&mut self, key: String) {
88 self.key = key;
89 }
90
91 pub(super) fn tag_rows(&self) -> impl Iterator<Item = Row> + '_ {
93 let rows = self.tags.rows().into_iter().filter(|tag| !tag.is_empty());
94 rows.map(|mut tag| {
95 tag.insert(LINK_CELL.to_string(), self.key.clone());
96 tag
97 })
98 }
99
100 pub(super) fn bind_accounts(&mut self, options: &[SelectOption]) {
101 let [from, to, ..] = &self.loaded;
102 self.from.set_options_preserving_id(bound(options, from));
103 self.to.set_options_preserving_id(bound(options, to));
104 }
105
106 pub(super) fn bind_commodities(&mut self, options: &[SelectOption]) {
107 let [.., from, to] = &self.loaded;
108 self.from_commodity
109 .set_options_preserving_id(bound(&with_none(options.to_vec()), from));
110 self.to_commodity
111 .set_options_preserving_id(bound(&with_empty_meaning(options), to));
112 }
113
114 fn fill_of(&self, source: usize, fills: &[FormOption]) -> Option<String> {
117 let text = self.cell_text(source);
118 match columns().nth(source).map(|field| field.kind) {
119 Some(Kind::Select {
120 source: Source::Commodities,
121 ..
122 }) => Some(text.to_string()).filter(|text| !text.is_empty()),
123 _ => commodity_fill(fills, text),
124 }
125 }
126
127 pub(super) fn refill(&mut self, (source, target): (usize, usize), fills: &[FormOption]) {
130 if self.from_load {
131 return;
132 }
133 let fill = self.fill_of(source, fills);
134 let auto = self.auto.contains(&target);
135 let Some(select) = self.select_mut(target) else {
136 return;
137 };
138 if !auto && !select.value().is_empty() {
139 return;
140 }
141 select.select_id(fill.as_deref().unwrap_or(""));
142 match fill {
143 Some(_) => self.auto.insert(target),
144 None => self.auto.remove(&target),
145 };
146 }
147
148 pub(super) fn commodity_state(&mut self, target: usize) -> Option<(bool, String)> {
149 let value = self.select_mut(target)?.value().to_string();
150 Some((self.auto.contains(&target), value))
151 }
152
153 pub(super) fn restore_commodity(&mut self, target: usize, (auto, value): (bool, String)) {
154 if let Some(select) = self.select_mut(target) {
155 select.select_id(&value);
156 }
157 if auto {
158 self.auto.insert(target);
159 } else {
160 self.auto.remove(&target);
161 }
162 }
163
164 pub(super) fn picked(&mut self, col: usize) {
165 self.auto.remove(&col);
166 }
167
168 #[must_use]
170 pub fn col_kind(col: usize) -> WidgetKind {
171 match columns().nth(col).map(|field| field.kind) {
172 Some(Kind::Select { .. }) => WidgetKind::Select,
173 _ => WidgetKind::Amount,
174 }
175 }
176
177 #[must_use]
179 pub fn text(&self, key: &str) -> &str {
180 match key {
181 "key" => &self.key,
182 "hidden" => &self.hidden,
183 _ => columns()
184 .position(|field| field.key == key)
185 .map_or("", |col| self.cell_text(col)),
186 }
187 }
188
189 fn cell_text(&self, col: usize) -> &str {
190 match col {
191 COL_FROM => self.from.value(),
192 COL_TO => self.to.value(),
193 COL_FROM_COMM => self.from_commodity.value(),
194 COL_TO_COMM => self.to_commodity.value(),
195 COL_VALUE => self.value.value(),
196 COL_TO_AMOUNT => self.to_amount.value(),
197 _ => "",
198 }
199 }
200
201 pub(in crate::widgets) fn select(&self, col: usize) -> Option<&SelectWidget> {
202 match col {
203 COL_FROM => Some(&self.from),
204 COL_TO => Some(&self.to),
205 COL_FROM_COMM => Some(&self.from_commodity),
206 COL_TO_COMM => Some(&self.to_commodity),
207 _ => None,
208 }
209 }
210
211 pub(in crate::widgets) fn select_mut(&mut self, col: usize) -> Option<&mut SelectWidget> {
212 match col {
213 COL_FROM => Some(&mut self.from),
214 COL_TO => Some(&mut self.to),
215 COL_FROM_COMM => Some(&mut self.from_commodity),
216 COL_TO_COMM => Some(&mut self.to_commodity),
217 _ => None,
218 }
219 }
220
221 pub(in crate::widgets) fn amount_mut(&mut self, col: usize) -> Option<&mut AmountWidget> {
222 match col {
223 COL_VALUE => Some(&mut self.value),
224 COL_TO_AMOUNT => Some(&mut self.to_amount),
225 _ => None,
226 }
227 }
228}
229
230fn with_empty_meaning(commodities: &[SelectOption]) -> Vec<SelectOption> {
233 let meaning = match columns().nth(COL_TO_COMM).map(|field| field.kind) {
234 Some(Kind::Select {
235 empty: Some(meaning),
236 ..
237 }) => meaning,
238 _ => "",
239 };
240 let same = SelectOption {
241 id: String::new(),
242 label: meaning.to_string(),
243 };
244 std::iter::once(same)
245 .chain(commodities.iter().cloned())
246 .collect()
247}
248
249pub(super) const PENDING_LABEL: &str = "…";
252
253fn seed(cell: &str) -> Vec<SelectOption> {
254 match cell {
255 "" => Vec::new(),
256 cell => vec![SelectOption {
257 id: cell.to_string(),
258 label: PENDING_LABEL.to_string(),
259 }],
260 }
261}
262
263fn bound(options: &[SelectOption], cell: &str) -> Vec<SelectOption> {
268 if cell.is_empty() || options.iter().any(|option| option.id == cell) {
269 return options.to_vec();
270 }
271 let seed = SelectOption {
272 id: cell.to_string(),
273 label: format!("{cell} (unchanged)"),
274 };
275 std::iter::once(seed)
276 .chain(options.iter().cloned())
277 .collect()
278}