1
use web_sys::Element;
2

            
3
use super::account::AccountAutocomplete;
4
use super::commodity::CommodityAutocomplete;
5
use super::tag::{TagNameAutocomplete, TagValueAutocomplete};
6
use crate::data_attrs::{AutocompleteAttr, autocomplete_type};
7

            
8
/// Creates and attaches an autocomplete component based on the type attribute.
9
///
10
/// Reads `data-autocomplete` from the wrapper element and dispatches to the
11
/// appropriate type-specific handler.
12
#[must_use]
13
pub fn create_autocomplete(wrapper: &Element) -> Option<()> {
14
    let ac_type = wrapper.get_attribute(AutocompleteAttr::Type.as_str())?;
15

            
16
    match ac_type.as_str() {
17
        autocomplete_type::ACCOUNT => AccountAutocomplete::attach(wrapper.clone()),
18
        autocomplete_type::COMMODITY => CommodityAutocomplete::attach(wrapper.clone()),
19
        autocomplete_type::TAG_NAME => TagNameAutocomplete::attach(wrapper.clone()),
20
        autocomplete_type::TAG_VALUE => TagValueAutocomplete::attach(wrapper.clone()),
21
        _ => {
22
            web_sys::console::warn_1(&format!("Unknown autocomplete type: {ac_type}").into());
23
            None
24
        }
25
    }
26
}