// -*- mode: kotlin -*-

/*
 * AUTOGENERATED BY glean_parser v{{ parser_version }}. DO NOT EDIT. DO NOT COMMIT.
 */
{# The rendered markdown 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/. */
{%- macro obj_declaration(obj, suffix='', access='', lazy=False) -%}
{% if (access != "private ") -%}
@get:JvmName("{{ obj.name|camelize }}{{ suffix }}")
{% endif -%}
{{ access }}val {{ obj.name|camelize }}{{ suffix }}: {{ obj|type_name }}{% if lazy %} by lazy { {%- else %} ={% endif %} // generated from {{ obj.identifier() }}
{% if obj.type == 'ping' %}
    {{ obj|type_name }}(
        {% for arg_name in ping_args if obj[arg_name] is defined %}
        {{ arg_name|camelize }} = {{ obj[arg_name]|kotlin }}{{ "," if not loop.last }}
        {% endfor %}
    )
{% else %}
    {{ obj|type_name }}(
        CommonMetricData(
            {% for arg_name in common_metric_args if obj[arg_name] is defined %}
            {{ arg_name|camelize }} = {{ obj[arg_name]|kotlin }}{{ "," if not loop.last }}
            {% endfor %}
        ){%- for arg_name in extra_metric_args if obj[arg_name] is defined -%}
        , {{ arg_name|camelize }} = {{ obj[arg_name]|kotlin }}
        {%- endfor -%}
    )
{% endif %}
{% if lazy %}}{% endif %}
{%- endmacro -%}

{%- macro enum_decl(obj, name, suffix) -%}
@Suppress("ClassNaming", "EnumNaming")
enum class {{ obj.name|camelize }}{{ suffix }} : EventExtraKey {
{% for key in obj|attr(name) %}
    {{ key|camelize }} {
        override fun keyName(): String = "{{ key }}"
    }{{ "," if not loop.last }}{{ ";" if loop.last }}

{% endfor %}
}
{%- endmacro %}

{%- macro struct_decl(obj, name, suffix) -%}
@Suppress("ClassNaming", "EnumNaming")
data class {{ obj.name|Camelize }}{{ suffix }}(
{% for item, typ in obj|attr(name) %}
    val {{ item|camelize }}: {{typ|extra_type_name}}? = null{{ "," if not loop.last }}
{% endfor %}
) : EventExtras {
    override fun toExtraRecord(): Map<String, String> {
        val map = mutableMapOf<String, String>()

        {% for item in obj|attr(name) %}
        this.{{ item[0]|camelize }}?.let {
            map.put("{{item[0]}}", it.toString())
        }
        {% endfor %}
        return map
    }
}
{%- endmacro -%}

/* ktlint-disable no-blank-line-before-rbrace */
@file:Suppress("PackageNaming", "MaxLineLength")
package {{ namespace }}

import {{ glean_namespace }}.private.CommonMetricData // ktlint-disable import-ordering no-unused-imports
import {{ glean_namespace }}.private.EventExtraKey // ktlint-disable import-ordering no-unused-imports
import {{ glean_namespace }}.private.EventExtras // ktlint-disable import-ordering no-unused-imports
import {{ glean_namespace }}.private.HistogramType // ktlint-disable import-ordering no-unused-imports
import {{ glean_namespace }}.private.Lifetime // ktlint-disable import-ordering no-unused-imports
import {{ glean_namespace }}.private.MemoryUnit // ktlint-disable import-ordering no-unused-imports
import {{ glean_namespace }}.private.NoExtraKeys // ktlint-disable import-ordering no-unused-imports
import {{ glean_namespace }}.private.NoExtras // ktlint-disable import-ordering no-unused-imports
import {{ glean_namespace }}.private.NoReasonCodes // ktlint-disable import-ordering no-unused-imports
import {{ glean_namespace }}.private.TimeUnit // ktlint-disable import-ordering no-unused-imports
{% for obj_type in obj_types %}
import {{ glean_namespace }}.private.{{ obj_type }} // ktlint-disable import-ordering
{% endfor %}
{% if has_labeled_metrics %}
import {{ glean_namespace }}.private.LabeledMetricType // ktlint-disable import-ordering
{% endif %}

internal object {{ category_name|Camelize }} {
{% for obj in objs.values() %}
    {% if obj|attr("_generate_enums") %}
    {% for name, suffix in obj["_generate_enums"] %}
    {% if obj|attr(name)|length %}
    {% if obj.has_extra_types %}
    {{ struct_decl(obj, name, suffix)|indent }}
    {% else %}
    {{ enum_decl(obj, name, suffix)|indent }}
    {% endif %}
    {% endif %}
    {% endfor %}
    {% endif %}
{% endfor %}
{% for obj in objs.values() %}
    {% if obj.labeled %}
    {{ obj_declaration(obj, 'Label', 'private ') | indent }}
    /**
     * {{ obj.description|wordwrap() | replace('\n', '\n        * ') }}
     */
    val {{ obj.name|camelize }}: LabeledMetricType<{{ obj|type_name }}> by lazy { // generated from {{ obj.identifier() }}
        LabeledMetricType(
            category = {{ obj.category|kotlin }},
            name = {{ obj.name|kotlin }},
            subMetric = {{ obj.name|camelize }}Label,
            disabled = {{ obj.is_disabled()|kotlin }},
            lifetime = {{ obj.lifetime|kotlin }},
            sendInPings = {{ obj.send_in_pings|kotlin }},
            labels = {{ obj.labels|kotlin }}
        )
    }
    {% else %}
    /**
     * {{ obj.description|wordwrap() | replace('\n', '\n     * ') }}
     */
    {{ obj_declaration(obj, lazy=obj.type != 'ping') | indent }}
    {% endif %}
{%- endfor %}
}
