// AUTOGENERATED BY glean_parser.  DO NOT EDIT.
{# The rendered source is autogenerated, but this
Jinja2 template is not. Please file bugs! #}

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::private::Ping;
use once_cell::sync::Lazy;

{% for obj in all_objs['pings'].values() %}
#[allow(non_upper_case_globals)]
/// {{ obj.description|trim | replace('\n', '\n/// ') }}
pub static {{ obj.name|snake_case }}: Lazy<Ping> = Lazy::new(|| {
    Ping::new(
        "{{ obj.name }}",
        {{ obj.include_client_id|rust }},
        {{ obj.send_if_empty|rust }},
        {{ obj.precise_timestamps|rust }},
        {{ obj.include_info_sections|rust }},
        {{ obj.enabled|rust }},
        {{ obj.schedules_pings|rust }},
        {{ obj.reason_codes|rust }},
        {{ obj.follows_collection_enabled|rust }},
        {{ obj.uploader_capabilities|rust }},
    )
});

{% endfor %}

/// Instantiate custom pings once to trigger registration.
///
/// # Arguments
///
/// application_id: If present, limit to only registering custom pings
///                 assigned to the identified application.
#[doc(hidden)]
pub fn register_pings(application_id: Option<&str>) {
    match application_id {
        {% for id, ping_names in ping_names_by_app_id.items() %}
        Some("{{id}}") => {
            log::info!("Registering pings {{ ping_names|join(', ') }} for {{id}}");
            {% for ping_name in ping_names %}
            let _ = &*{{ ping_name|snake_case }};
            {% endfor %}
        },
        {% endfor %}
        _ => {
            {% for obj in all_objs['pings'].values() %}
            let _ = &*{{ obj.name|snake_case }};
            {% endfor %}
        }
    }
    extern "C" {
        fn JOG_MaybeReload();
    }
    unsafe {
      JOG_MaybeReload();
    }
}

/// Map from pings to the pings scheduled along with them.
///
#[doc(hidden)]
pub fn ping_schedule() -> std::collections::HashMap<String, Vec<String>> {
  std::collections::HashMap::from([
      {% for ping, schedule in ping_schedule_reverse_map.items() %}
      (
        {{ ping|rust }},
        {{ schedule|rust }}
      ),
      {% endfor %}
  ])
}

#[cfg(feature = "with_gecko")]
pub(crate) fn submit_ping_by_id(id: u32, reason: Option<&str>) {
    if id & (1 << crate::factory::DYNAMIC_PING_BIT) > 0 {
        let map = crate::factory::__jog_metric_maps::PING_MAP
            .read()
            .expect("Read lock for dynamic ping map was poisoned!");
        if let Some(ping) = map.get(&id) {
            ping.submit(reason);
        } else {
            // TODO: instrument this error.
            log::error!("Cannot submit unknown dynamic ping {} by id.", id);
        }
        return;
    }
    match id {
{% for obj in all_objs['pings'].values() %}
        {{ obj.name|ping_id }} => {{ obj.name | snake_case }}.submit(reason),
{% endfor %}
        _ => {
            // TODO: instrument this error.
            log::error!("Cannot submit unknown ping {} by id.", id);
        }
    }
}

#[cfg(feature = "with_gecko")]
pub(crate) fn set_ping_enabled_by_id(id: u32, enabled: bool) {
    if id & (1 << crate::factory::DYNAMIC_PING_BIT) > 0 {
        let map = crate::factory::__jog_metric_maps::PING_MAP
            .read()
            .expect("Read lock for dynamic ping map was poisoned!");
        if let Some(ping) = map.get(&id) {
            ping.set_enabled(enabled);
        } else {
            // TODO: instrument this error.
            log::error!("Cannot set_enabled on unknown dynamic ping {} by id.", id);
        }
        return;
    }
    match id {
{% for obj in all_objs['pings'].values() %}
        {{ obj.name|ping_id }} => {{ obj.name | snake_case }}.set_enabled(enabled),
{% endfor %}
        _ => {
            // TODO: instrument this error.
            log::error!("Cannot set_enabled on unknown ping {} by id.", id);
        }
    }
}

/// Measure the allocation size of all known pings.
#[doc(hidden)]
pub fn fog_ping_alloc_size(ops: &mut malloc_size_of::MallocSizeOfOps) -> usize {
  use malloc_size_of::MallocSizeOf;

  let mut n = 0;
  {% for obj in all_objs['pings'].values() %}
  n += {{ obj.name|snake_case }}.size_of(ops);
  {% endfor %}
  n
}
