// -*- 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 reason_enum_decl(obj, name, suffix) -%}
@Suppress("ClassNaming", "EnumNaming")
enum class {{ obj.name|camelize }}{{ suffix }} : ReasonCode {
{% for key in obj|attr(name) %}
    {{ key|camelize }} {
        override fun code(): Int = {{ loop.index-1 }}
    }{{ "," 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 -%}

{%- macro generate_structure(name, struct) %}
{%- if struct.type == "array" -%}
    @Serializable
    data class {{ name }}(var items: MutableList<{{ name }}Item> = mutableListOf()) : ObjectSerialize {
        fun add(elem: {{ name }}Item) = items.add(elem)

        fun addAll(elements: Collection<{{ name }}Item>) = items.addAll(elements)

        fun clear() = items.clear()

        fun remove(element: {{ name }}Item) = items.remove(element)
        fun removeAll(elements: Collection<{{ name }}Item>) = items.removeAll(elements)
        fun removeAt(index: Int) = items.removeAt(index)

        fun set(index: Int, element: {{ name }}Item) = items.set(index, element)

        override fun intoSerializedObject(): String {
            return Json.encodeToString(items)
        }
    }

    {{ generate_structure(name ~ "Item", struct["items"]) }}

{%- elif struct.type == "object" -%}
    @Serializable
    data class {{ name }}(
        {% for itemname, val in struct.properties.items() %}
          {% if val.type == "object" %}
          var {{itemname|camelize}}: {{ name ~ "Item" ~ itemname|Camelize ~ "Object" }}? = null,
          {% elif val.type == "array" %}
          var {{itemname|camelize}}: {{ name ~ "Item" ~ itemname|Camelize }} = {{ name ~ "Item" ~ itemname|Camelize }}(),
          {% else %}
          var {{itemname|camelize}}: {{val.type|structure_type_name}}? = null,
          {% endif %}
        {% endfor %}
    ): ObjectSerialize {
        override fun intoSerializedObject(): String {
            return Json.encodeToString(this)
        }
    }

    {% for itemname, val in struct.properties.items() %}
        {% if val.type == "array" %}
        {% set nested_name = name ~ "Item" ~ itemname|Camelize %}
        {{ generate_structure(nested_name, val) }}
        {% elif val.type == "object" %}
        {% set nested_name = name ~ "Item" ~ itemname|Camelize ~ "Object" %}
        {{ generate_structure(nested_name, val) }}
        {% endif %}
    {% endfor %}

{% endif %}

{% 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.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.NoExtras // ktlint-disable import-ordering no-unused-imports
import {{ glean_namespace }}.private.NoReasonCodes // ktlint-disable import-ordering no-unused-imports
import {{ glean_namespace }}.private.ObjectSerialize // ktlint-disable import-ordering no-unused-imports
import {{ glean_namespace }}.private.ReasonCode // 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 %}
{% if has_object_metrics %}
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
{% endif %}

internal object {{ category_name|Camelize }} {
{% for obj in objs.values() %}
    {% if obj.type == "ping" %}
    {% if obj|attr("_generate_enums") %}
    {% for name, suffix in obj["_generate_enums"] %}
    {% if obj|attr(name)|length %}
    {{ reason_enum_decl(obj, name, suffix)|indent }}
    {% endif %}
    {% endfor %}
    {% endif %}
    {% else %}
    {% if obj|attr("_generate_structure") %}
    {{ generate_structure(obj.name|Camelize ~ "Object", obj._generate_structure) }}
    {%- endif %}
    {% if obj|attr("_generate_enums") %}
    {% for name, suffix in obj["_generate_enums"] %}
    {% if obj|attr(name)|length %}
    {{ struct_decl(obj, name, suffix)|indent }}
    {% endif %}
    {% endfor %}
    {% endif %}
    {% 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 %}
}
