//! Canonical source-provenance LABELING — the single rule for how every Adom
//! EDA app names where a symbol / footprint / 3D / datasheet actually came from.
//!
//! THE RULE (John, 2026-06-23): label by the DISCOVERY SOURCE — where the file
//! was actually downloaded — NOT by content_origin (who authored the model).
//!
//!   - ONLY the maker's OWN website counts as "the manufacturer", and then we
//!     NAME the site: "Texas Instruments (ti.com)", "Analog Devices (analog.com)".
//!   - The moment the manufacturer hands you off to a 3rd-party library —
//!     UltraLibrarian, SnapMagic/SnapEDA, Component Search Engine — that
//!     AGGREGATOR is the source. NEVER label it "Manufacturer".
//!   - A fuzzy bare "Manufacturer" is misleading and banned.
//!
//! This module is mirrored (same mapping) in adom-symbol, adom-footprint and
//! adom-lbr so the label is identical everywhere. See the `chip-provenance-labeling` skill.

/// The maker's canonical website domain, for "Texas Instruments (ti.com)" labels.
/// Matched case-insensitively on a substring of the manufacturer name.
pub fn mfr_domain(manufacturer: &str) -> Option<&'static str> {
    let m = manufacturer.to_lowercase();
    let table = [
        ("texas instr", "ti.com"), ("analog dev", "analog.com"),
        ("stmicro", "st.com"), ("nxp", "nxp.com"), ("microchip", "microchip.com"),
        ("infineon", "infineon.com"), ("on semi", "onsemi.com"), ("onsemi", "onsemi.com"),
        ("nordic", "nordicsemi.com"), ("espressif", "espressif.com"),
        ("renesas", "renesas.com"), ("diodes inc", "diodes.com"), ("vishay", "vishay.com"),
        ("bosch", "bosch-sensortec.com"), ("maxim", "analog.com"), ("dialog", "renesas.com"),
        ("rohm", "rohm.com"), ("toshiba", "toshiba.com"), ("skyworks", "skyworksinc.com"),
        ("qorvo", "qorvo.com"), ("micron", "micron.com"), ("cypress", "infineon.com"),
        ("silicon lab", "silabs.com"), ("silabs", "silabs.com"), ("lattice", "latticesemi.com"),
        ("winbond", "winbond.com"), ("ti ", "ti.com"),
    ];
    table.iter().find(|(k, _)| m.contains(k)).map(|(_, v)| *v)
}

/// The human label for a source, given the discovery-source slug + the
/// manufacturer name. THIS is the function every app calls.
pub fn source_label(discovery: &str, manufacturer: &str) -> String {
    match discovery.to_lowercase().as_str() {
        // Manufacturer-DIRECT: only when downloaded from the maker's own site.
        "manufacturer" | "mfr" | "vendor" => {
            if let Some(dom) = mfr_domain(manufacturer) {
                if manufacturer.is_empty() { format!("Manufacturer · {dom}") }
                else { format!("{manufacturer} ({dom})") }
            } else if !manufacturer.is_empty() {
                format!("{manufacturer} (direct)")
            } else {
                "Manufacturer (direct site)".to_string()
            }
        }
        // 3rd-party aggregators — NEVER "the manufacturer".
        "cse" | "componentsearch" | "cse_ul" | "samacsys" => "Component Search Engine".into(),
        "snapeda" | "snapmagic" => "SnapEDA".into(),
        "ultralibrarian" | "ul" => "Ultra Librarian".into(),
        "mouser" => "Mouser".into(),
        "digikey" => "DigiKey".into(),
        "arrow" => "Arrow".into(),
        "lcsc" => "LCSC".into(),
        "kicad_community" => "KiCad community".into(),
        other => other.to_string(),
    }
}

/// A short note for the "the manufacturer linked you to it, but it's a 3rd
/// party" middle-ground — appended in tooltips so the user knows the maker
/// pointed here even though the file came off an aggregator.
pub fn linked_from_mfr_note(manufacturer: &str) -> String {
    if manufacturer.is_empty() {
        "the manufacturer's page linked to this 3rd-party library".to_string()
    } else {
        format!("{manufacturer}'s own page links here, but the file is hosted by this 3rd-party library — not downloaded from {manufacturer} directly")
    }
}