use minijinja::Environment;

static TEMPLATES: &[(&str, &str)] = &[
    (
        "enum_variant_constructor.rs.jinja",
        include_str!("templates/enum_variant_constructor.rs.jinja"),
    ),
    (
        "unregistration_fn.jinja",
        include_str!("templates/unregistration_fn.jinja"),
    ),
    ("clear_fn.jinja", include_str!("templates/clear_fn.jinja")),
    ("registration_fn.jinja", include_str!("templates/registration_fn.jinja")),
    (
        "sync_method_unit_return.jinja",
        include_str!("templates/sync_method_unit_return.jinja"),
    ),
    (
        "sync_method_string_return.jinja",
        include_str!("templates/sync_method_string_return.jinja"),
    ),
    (
        "sync_method_complex_return.jinja",
        include_str!("templates/sync_method_complex_return.jinja"),
    ),
    (
        "async_method_unit_return.jinja",
        include_str!("templates/async_method_unit_return.jinja"),
    ),
    (
        "async_method_string_return.jinja",
        include_str!("templates/async_method_string_return.jinja"),
    ),
    (
        "async_method_complex_return.jinja",
        include_str!("templates/async_method_complex_return.jinja"),
    ),
    (
        "bridge_constructor.jinja",
        include_str!("templates/bridge_constructor.jinja"),
    ),
    ("visitor_bridge.jinja", include_str!("templates/visitor_bridge.jinja")),
    (
        "conversion_options.jinja",
        include_str!("templates/conversion_options.jinja"),
    ),
    ("bridge_function.jinja", include_str!("templates/bridge_function.jinja")),
    (
        "sync_method_non_unit_return.jinja",
        include_str!("templates/sync_method_non_unit_return.jinja"),
    ),
    ("visitor_method.jinja", include_str!("templates/visitor_method.jinja")),
    (
        "r_free_function_wrapper.jinja",
        include_str!("templates/r_free_function_wrapper.jinja"),
    ),
    (
        "r_type_class_env.jinja",
        include_str!("templates/r_type_class_env.jinja"),
    ),
    (
        "r_method_binding.jinja",
        include_str!("templates/r_method_binding.jinja"),
    ),
    (
        "r_from_json_factory.jinja",
        include_str!("templates/r_from_json_factory.jinja"),
    ),
    (
        "r_dollar_dispatch.jinja",
        include_str!("templates/r_dollar_dispatch.jinja"),
    ),
    (
        "r_bracket_dispatch.jinja",
        include_str!("templates/r_bracket_dispatch.jinja"),
    ),
    (
        "r_namespace_export.jinja",
        include_str!("templates/r_namespace_export.jinja"),
    ),
    (
        "r_namespace_s3method.jinja",
        include_str!("templates/r_namespace_s3method.jinja"),
    ),
    ("r_s3_generic.jinja", include_str!("templates/r_s3_generic.jinja")),
    ("r_s3_method.jinja", include_str!("templates/r_s3_method.jinja")),
    (
        "r_namespace_s3method_named.jinja",
        include_str!("templates/r_namespace_s3method_named.jinja"),
    ),
    ("r_use_dyn_lib.jinja", include_str!("templates/r_use_dyn_lib.jinja")),
    (
        "r_namespace_use_dyn_lib.jinja",
        include_str!("templates/r_namespace_use_dyn_lib.jinja"),
    ),
    (
        "flat_enum_derive.jinja",
        include_str!("templates/flat_enum_derive.jinja"),
    ),
    (
        "flat_enum_struct_header.jinja",
        include_str!("templates/flat_enum_struct_header.jinja"),
    ),
    (
        "flat_enum_discriminator_field.jinja",
        include_str!("templates/flat_enum_discriminator_field.jinja"),
    ),
    (
        "flat_enum_variant_field.jinja",
        include_str!("templates/flat_enum_variant_field.jinja"),
    ),
    (
        "flat_enum_struct_footer.jinja",
        include_str!("templates/flat_enum_struct_footer.jinja"),
    ),
    (
        "flat_enum_from_core_impl.jinja",
        include_str!("templates/flat_enum_from_core_impl.jinja"),
    ),
    (
        "flat_enum_from_core_variant_unit.jinja",
        include_str!("templates/flat_enum_from_core_variant_unit.jinja"),
    ),
    (
        "flat_enum_from_core_variant_tuple.jinja",
        include_str!("templates/flat_enum_from_core_variant_tuple.jinja"),
    ),
    (
        "flat_enum_from_core_variant_struct.jinja",
        include_str!("templates/flat_enum_from_core_variant_struct.jinja"),
    ),
    (
        "flat_enum_from_core_impl_footer.jinja",
        include_str!("templates/flat_enum_from_core_impl_footer.jinja"),
    ),
    (
        "flat_enum_from_core_impl_catch_all.jinja",
        include_str!("templates/flat_enum_from_core_impl_catch_all.jinja"),
    ),
    (
        "flat_enum_from_binding_impl.jinja",
        include_str!("templates/flat_enum_from_binding_impl.jinja"),
    ),
    (
        "flat_enum_from_binding_variant.jinja",
        include_str!("templates/flat_enum_from_binding_variant.jinja"),
    ),
    (
        "flat_enum_from_binding_impl_footer.jinja",
        include_str!("templates/flat_enum_from_binding_impl_footer.jinja"),
    ),
    (
        "flat_enum_from_binding_variant_unit.jinja",
        include_str!("templates/flat_enum_from_binding_variant_unit.jinja"),
    ),
    (
        "flat_enum_from_binding_variant_tuple.jinja",
        include_str!("templates/flat_enum_from_binding_variant_tuple.jinja"),
    ),
    (
        "enum_from_core_to_binding_impl.jinja",
        include_str!("templates/enum_from_core_to_binding_impl.jinja"),
    ),
    (
        "enum_from_core_to_binding_arm.jinja",
        include_str!("templates/enum_from_core_to_binding_arm.jinja"),
    ),
    (
        "enum_from_core_to_binding_catch_all.jinja",
        include_str!("templates/enum_from_core_to_binding_catch_all.jinja"),
    ),
    (
        "enum_from_binding_to_core_impl.jinja",
        include_str!("templates/enum_from_binding_to_core_impl.jinja"),
    ),
    (
        "enum_from_binding_to_core_arm.jinja",
        include_str!("templates/enum_from_binding_to_core_arm.jinja"),
    ),
    (
        "enum_from_binding_to_core_catch_all.jinja",
        include_str!("templates/enum_from_binding_to_core_catch_all.jinja"),
    ),
    (
        "json_vec_optional_preamble.jinja",
        include_str!("templates/json_vec_optional_preamble.jinja"),
    ),
    (
        "json_vec_required_preamble.jinja",
        include_str!("templates/json_vec_required_preamble.jinja"),
    ),
    (
        "named_let_optional_binding.jinja",
        include_str!("templates/named_let_optional_binding.jinja"),
    ),
    (
        "named_let_required_binding.jinja",
        include_str!("templates/named_let_required_binding.jinja"),
    ),
    (
        "serde_named_optional_binding.jinja",
        include_str!("templates/serde_named_optional_binding.jinja"),
    ),
    (
        "serde_named_required_binding.jinja",
        include_str!("templates/serde_named_required_binding.jinja"),
    ),
    (
        "async_param_clone_bytes_ref.jinja",
        include_str!("templates/async_param_clone_bytes_ref.jinja"),
    ),
    (
        "async_param_clone_path_ref.jinja",
        include_str!("templates/async_param_clone_path_ref.jinja"),
    ),
    (
        "async_param_clone_named_ref.jinja",
        include_str!("templates/async_param_clone_named_ref.jinja"),
    ),
    (
        "async_param_clone_native_struct_ref.jinja",
        include_str!("templates/async_param_clone_native_struct_ref.jinja"),
    ),
    (
        "async_param_clone_ref.jinja",
        include_str!("templates/async_param_clone_ref.jinja"),
    ),
    (
        "async_param_clone_value.jinja",
        include_str!("templates/async_param_clone_value.jinja"),
    ),
    (
        "registration_variant.rs.jinja",
        include_str!("templates/registration_variant.rs.jinja"),
    ),
    (
        "service_r_constructor.jinja",
        include_str!("templates/service_r_constructor.jinja"),
    ),
    (
        "service_r_configurator.jinja",
        include_str!("templates/service_r_configurator.jinja"),
    ),
    (
        "service_r_entrypoint.jinja",
        include_str!("templates/service_r_entrypoint.jinja"),
    ),
    (
        "service_r_registration.jinja",
        include_str!("templates/service_r_registration.jinja"),
    ),
    (
        "service_rs_handler_bridge.jinja",
        include_str!("templates/service_rs_handler_bridge.jinja"),
    ),
    (
        "service_rs_run_function_header.jinja",
        include_str!("templates/service_rs_run_function_header.jinja"),
    ),
    (
        "service_rs_registration_match_arm.jinja",
        include_str!("templates/service_rs_registration_match_arm.jinja"),
    ),
    (
        "service_rs_run_function_footer.jinja",
        include_str!("templates/service_rs_run_function_footer.jinja"),
    ),
    (
        "bridge_field_function.jinja",
        include_str!("templates/bridge_field_function.jinja"),
    ),
    (
        "json_struct_optional_preamble.jinja",
        include_str!("templates/json_struct_optional_preamble.jinja"),
    ),
    (
        "json_struct_required_preamble.jinja",
        include_str!("templates/json_struct_required_preamble.jinja"),
    ),
    (
        "r_unit_enum_wrapper.jinja",
        include_str!("templates/r_unit_enum_wrapper.jinja"),
    ),
    (
        "r_trait_bridge_roxygen.jinja",
        include_str!("templates/r_trait_bridge_roxygen.jinja"),
    ),
];

pub(crate) fn make_env() -> Environment<'static> {
    let mut env = Environment::new();
    env.set_trim_blocks(true);
    env.set_lstrip_blocks(true);
    env.set_keep_trailing_newline(true);
    for (name, src) in TEMPLATES {
        env.add_template(name, src).expect("built-in template is valid");
    }
    env
}

pub(crate) fn render(template_name: &str, ctx: minijinja::Value) -> String {
    make_env()
        .get_template(template_name)
        .unwrap_or_else(|_| panic!("template {template_name} not found"))
        .render(ctx)
        .unwrap_or_else(|e| panic!("template {template_name} failed to render: {e}"))
}
