// -*- mode: Swift -*-

// AUTOGENERATED BY glean_parser. 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='') %}
{{ access }}static let {{ obj.name|camelize|variable_name }}{{ suffix }} = {{ obj|type_name }}( // generated from {{ obj.identifier() }}
        {% for arg_name in extra_args if obj[arg_name] is defined %}
        {{ arg_name|camelize }}: {{ obj[arg_name]|swift }}{{ "," if not loop.last }}
        {% endfor %}
    )
{% endmacro %}

{% macro enum_decl(obj, name, suffix) %}
enum {{ obj.name|Camelize }}{{ suffix }}: Int32, ExtraKeys {
        {% for key in obj|attr(name) %}
        case {{ key|camelize|variable_name }} = {{ loop.index-1 }}
        {% endfor %}

        public func index() -> Int32 {
            return self.rawValue
        }
    }
{% endmacro %}

{% macro struct_decl(obj, name, suffix) %}
struct {{ obj.name|Camelize }}{{ suffix }}: EventExtras {
        {% for item, typ in obj|attr(name) %}
        var {{ item|camelize|variable_name }}: {{typ|extra_type_name}}?
        {% endfor %}

        func toFfiExtra() -> ([Int32], [String]) {
            var keys = [Int32]()
            var values = [String]()

            {% for item in obj|attr(name) %}
            if let {{ item[0]|camelize }} = self.{{item[0]|camelize}} {
                keys.append({{ loop.index - 1 }})
                values.append(String({{ item[0]|camelize }}))
            }
            {% endfor %}

            return (keys, values)
        }
    }
{% endmacro %}

{% if not allow_reserved %}
import {{ glean_namespace }}

{% endif %}
// swiftlint:disable superfluous_disable_command
// swiftlint:disable nesting
// swiftlint:disable line_length
// swiftlint:disable identifier_name
// swiftlint:disable force_try

extension {{ namespace }} {
    {% if build_info %}
    class GleanBuild {
        private init() {
            // Intentionally left private, no external user can instantiate a new global object.
        }

        public static let info = BuildInfo(buildDate: {{ build_info.build_date }})
    }
    {% endif %}

    {% for category in categories %}
    {% if category.contains_pings %}
    class {{ category.name|Camelize }} {
        public static let shared = {{ category.name|Camelize }}()
        private init() {
            // Intentionally left private, no external user can instantiate a new global object.
        }

        {% for obj in category.objs.values() %}
        {% if obj|attr("_generate_enums") %}
        {% for name, suffix in obj["_generate_enums"] %}
        {% if obj|attr(name)|length %}
        enum {{ obj.name|Camelize }}{{ suffix }}: Int, ReasonCodes {
            {% for key in obj|attr(name) %}
            case {{ key|camelize|variable_name }} = {{ loop.index-1 }}
            {% endfor %}

            public func index() -> Int {
                return self.rawValue
            }
        }

        {% endif %}
        {% endfor %}
        {% endif %}
        /// {{ obj.description|wordwrap() | replace('\n', '\n        /// ') }}
        let {{ obj.name|camelize|variable_name }} = {{obj|type_name}}(
            name: {{ obj.name|swift }},
            includeClientId: {{obj.include_client_id|swift}},
            sendIfEmpty: {{obj.send_if_empty|swift}},
            reasonCodes: {{obj.reason_codes|swift}}
        )

      {% endfor %}
    }

    {% else %}
    enum {{ category.name|Camelize }} {
    {% for obj in category.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 category.objs.values() %}
        {% if obj.labeled %}
        {{ obj_declaration(obj, 'Label', 'private ') | indent }}
        /// {{ obj.description|wordwrap() | replace('\n', '\n        /// ') }}
        static let {{ obj.name|camelize|variable_name }} = try! LabeledMetricType<{{ obj|type_name }}>( // generated from {{ obj.identifier() }}
            category: {{ obj.category|swift }},
            name: {{ obj.name|swift }},
            sendInPings: {{ obj.send_in_pings|swift }},
            lifetime: {{ obj.lifetime|swift }},
            disabled: {{ obj.is_disabled()|swift }},
            subMetric: {{ obj.name|camelize }}Label,
            labels: {{ obj.labels|swift }}
        )

        {% else %}
        /// {{ obj.description|wordwrap() | replace('\n', '\n        /// ') }}
        {{ obj_declaration(obj) | indent }}
        {% endif %}
    {% endfor %}
    }

    {% endif %}
    {% endfor %}
}
