use minijinja::Environment;

static TEMPLATES: &[(&str, &str)] = &[
    ("doc_comment.jinja", include_str!("templates/doc_comment.jinja")),
    ("enum_header.jinja", include_str!("templates/enum_header.jinja")),
    (
        "error_enum_header.jinja",
        include_str!("templates/error_enum_header.jinja"),
    ),
    ("error_case.jinja", include_str!("templates/error_case.jinja")),
    (
        "error_case_with_data.jinja",
        include_str!("templates/error_case_with_data.jinja"),
    ),
    ("typealias.jinja", include_str!("templates/typealias.jinja")),
    ("struct_newtype.jinja", include_str!("templates/struct_newtype.jinja")),
    ("impl_header.jinja", include_str!("templates/impl_header.jinja")),
    (
        "fn_new_signature.jinja",
        include_str!("templates/fn_new_signature.jinja"),
    ),
    ("fn_getter.jinja", include_str!("templates/fn_getter.jinja")),
    (
        "getter_json_bridge.jinja",
        include_str!("templates/getter_json_bridge.jinja"),
    ),
    (
        "getter_optional_bytes.jinja",
        include_str!("templates/getter_optional_bytes.jinja"),
    ),
    ("getter_bytes.jinja", include_str!("templates/getter_bytes.jinja")),
    (
        "getter_serde_optional.jinja",
        include_str!("templates/getter_serde_optional.jinja"),
    ),
    ("getter_serde.jinja", include_str!("templates/getter_serde.jinja")),
    (
        "getter_simple_clone.jinja",
        include_str!("templates/getter_simple_clone.jinja"),
    ),
    ("getter_duration.jinja", include_str!("templates/getter_duration.jinja")),
    (
        "getter_optional_duration.jinja",
        include_str!("templates/getter_optional_duration.jinja"),
    ),
    (
        "getter_optional_named.jinja",
        include_str!("templates/getter_optional_named.jinja"),
    ),
    ("getter_named.jinja", include_str!("templates/getter_named.jinja")),
    (
        "getter_vec_named_optional.jinja",
        include_str!("templates/getter_vec_named_optional.jinja"),
    ),
    (
        "getter_vec_named.jinja",
        include_str!("templates/getter_vec_named.jinja"),
    ),
    (
        "getter_vec_complex_serde_optional.jinja",
        include_str!("templates/getter_vec_complex_serde_optional.jinja"),
    ),
    (
        "getter_vec_complex_serde.jinja",
        include_str!("templates/getter_vec_complex_serde.jinja"),
    ),
    (
        "getter_vec_complex_skip.jinja",
        include_str!("templates/getter_vec_complex_skip.jinja"),
    ),
    (
        "getter_vec_primitive_serde_optional.jinja",
        include_str!("templates/getter_vec_primitive_serde_optional.jinja"),
    ),
    (
        "getter_vec_primitive_serde.jinja",
        include_str!("templates/getter_vec_primitive_serde.jinja"),
    ),
    (
        "getter_vec_primitive_clone.jinja",
        include_str!("templates/getter_vec_primitive_clone.jinja"),
    ),
    (
        "getter_string_like_debug_optional.jinja",
        include_str!("templates/getter_string_like_debug_optional.jinja"),
    ),
    (
        "getter_string_like_debug.jinja",
        include_str!("templates/getter_string_like_debug.jinja"),
    ),
    (
        "getter_string_like_serde_optional.jinja",
        include_str!("templates/getter_string_like_serde_optional.jinja"),
    ),
    (
        "getter_string_like_serde.jinja",
        include_str!("templates/getter_string_like_serde.jinja"),
    ),
    (
        "getter_string_cow.jinja",
        include_str!("templates/getter_string_cow.jinja"),
    ),
    (
        "getter_string_cow_optional.jinja",
        include_str!("templates/getter_string_cow_optional.jinja"),
    ),
    (
        "getter_skip_comment.jinja",
        include_str!("templates/getter_skip_comment.jinja"),
    ),
    (
        "default_construction_var.jinja",
        include_str!("templates/default_construction_var.jinja"),
    ),
    (
        "default_field_excluded.jinja",
        include_str!("templates/default_field_excluded.jinja"),
    ),
    (
        "default_field_inner_excluded.jinja",
        include_str!("templates/default_field_inner_excluded.jinja"),
    ),
    (
        "default_field_json_bridge.jinja",
        include_str!("templates/default_field_json_bridge.jinja"),
    ),
    (
        "default_field_enum_skip.jinja",
        include_str!("templates/default_field_enum_skip.jinja"),
    ),
    (
        "default_field_named_optional_boxed.jinja",
        include_str!("templates/default_field_named_optional_boxed.jinja"),
    ),
    (
        "default_field_named_optional_arc.jinja",
        include_str!("templates/default_field_named_optional_arc.jinja"),
    ),
    (
        "default_field_named_optional_plain.jinja",
        include_str!("templates/default_field_named_optional_plain.jinja"),
    ),
    (
        "default_field_named_boxed.jinja",
        include_str!("templates/default_field_named_boxed.jinja"),
    ),
    (
        "default_field_named_arc.jinja",
        include_str!("templates/default_field_named_arc.jinja"),
    ),
    (
        "default_field_named_plain.jinja",
        include_str!("templates/default_field_named_plain.jinja"),
    ),
    (
        "default_field_vec_named_enum_skip.jinja",
        include_str!("templates/default_field_vec_named_enum_skip.jinja"),
    ),
    (
        "default_field_vec_named_arc_optional.jinja",
        include_str!("templates/default_field_vec_named_arc_optional.jinja"),
    ),
    (
        "default_field_vec_named_plain_optional.jinja",
        include_str!("templates/default_field_vec_named_plain_optional.jinja"),
    ),
    (
        "default_field_vec_named_arc_plain.jinja",
        include_str!("templates/default_field_vec_named_arc_plain.jinja"),
    ),
    (
        "default_field_vec_named_plain.jinja",
        include_str!("templates/default_field_vec_named_plain.jinja"),
    ),
    (
        "default_field_vec_serde.jinja",
        include_str!("templates/default_field_vec_serde.jinja"),
    ),
    (
        "default_field_string_like.jinja",
        include_str!("templates/default_field_string_like.jinja"),
    ),
    (
        "default_field_string_like_non_serde_comment.jinja",
        include_str!("templates/default_field_string_like_non_serde_comment.jinja"),
    ),
    (
        "default_field_string_like_optional_serde.jinja",
        include_str!("templates/default_field_string_like_optional_serde.jinja"),
    ),
    (
        "default_field_string_like_serde.jinja",
        include_str!("templates/default_field_string_like_serde.jinja"),
    ),
    (
        "default_field_bytes_assign.jinja",
        include_str!("templates/default_field_bytes_assign.jinja"),
    ),
    (
        "default_field_duration_assign.jinja",
        include_str!("templates/default_field_duration_assign.jinja"),
    ),
    (
        "default_field_optional_duration_assign.jinja",
        include_str!("templates/default_field_optional_duration_assign.jinja"),
    ),
    (
        "default_field_generic_assign.jinja",
        include_str!("templates/default_field_generic_assign.jinja"),
    ),
    (
        "dc_construct_target.jinja",
        include_str!("templates/dc_construct_target.jinja"),
    ),
    (
        "extern_type_decl.jinja",
        include_str!("templates/extern_type_decl.jinja"),
    ),
    (
        "extern_init_attr.jinja",
        include_str!("templates/extern_init_attr.jinja"),
    ),
    ("extern_fn_new.jinja", include_str!("templates/extern_fn_new.jinja")),
    (
        "extern_fn_getter.jinja",
        include_str!("templates/extern_fn_getter.jinja"),
    ),
    (
        "extern_enum_type.jinja",
        include_str!("templates/extern_enum_type.jinja"),
    ),
    (
        "extern_functions_header.jinja",
        include_str!("templates/extern_functions_header.jinja"),
    ),
    (
        "extern_swift_name_attr.jinja",
        include_str!("templates/extern_swift_name_attr.jinja"),
    ),
    ("extern_fn_decl.jinja", include_str!("templates/extern_fn_decl.jinja")),
    (
        "trait_extern_type.jinja",
        include_str!("templates/trait_extern_type.jinja"),
    ),
    (
        "trait_phantom_fn.jinja",
        include_str!("templates/trait_phantom_fn.jinja"),
    ),
    ("trait_method_fn.jinja", include_str!("templates/trait_method_fn.jinja")),
    ("trait_struct.jinja", include_str!("templates/trait_struct.jinja")),
    (
        "trait_phantom_impl.jinja",
        include_str!("templates/trait_phantom_impl.jinja"),
    ),
    (
        "trait_method_impl.jinja",
        include_str!("templates/trait_method_impl.jinja"),
    ),
    (
        "enum_unit_header.jinja",
        include_str!("templates/enum_unit_header.jinja"),
    ),
    (
        "enum_unit_variant.jinja",
        include_str!("templates/enum_unit_variant.jinja"),
    ),
    (
        "swift_enum_header.jinja",
        include_str!("templates/swift_enum_header.jinja"),
    ),
    ("enum_case_unit.jinja", include_str!("templates/enum_case_unit.jinja")),
    (
        "enum_case_with_data.jinja",
        include_str!("templates/enum_case_with_data.jinja"),
    ),
    (
        "enum_from_impl_header.jinja",
        include_str!("templates/enum_from_impl_header.jinja"),
    ),
    (
        "enum_from_variant.jinja",
        include_str!("templates/enum_from_variant.jinja"),
    ),
    (
        "enum_from_wildcard.jinja",
        include_str!("templates/enum_from_wildcard.jinja"),
    ),
    (
        "struct_literal_open.jinja",
        include_str!("templates/struct_literal_open.jinja"),
    ),
    (
        "default_construction_let_mut.jinja",
        include_str!("templates/default_construction_let_mut.jinja"),
    ),
    (
        "default_construction_return.jinja",
        include_str!("templates/default_construction_return.jinja"),
    ),
    (
        "default_field_arc_assign.jinja",
        include_str!("templates/default_field_arc_assign.jinja"),
    ),
    (
        "default_field_boxed_assign.jinja",
        include_str!("templates/default_field_boxed_assign.jinja"),
    ),
    (
        "default_field_enum_assign.jinja",
        include_str!("templates/default_field_enum_assign.jinja"),
    ),
    (
        "default_field_excluded_comment.jinja",
        include_str!("templates/default_field_excluded_comment.jinja"),
    ),
    (
        "default_field_json_bridge_read.jinja",
        include_str!("templates/default_field_json_bridge_read.jinja"),
    ),
    (
        "default_field_optional_arc_assign.jinja",
        include_str!("templates/default_field_optional_arc_assign.jinja"),
    ),
    (
        "default_field_optional_boxed_assign.jinja",
        include_str!("templates/default_field_optional_boxed_assign.jinja"),
    ),
    (
        "default_field_optional_plain_assign.jinja",
        include_str!("templates/default_field_optional_plain_assign.jinja"),
    ),
    (
        "default_field_plain_assign.jinja",
        include_str!("templates/default_field_plain_assign.jinja"),
    ),
    (
        "default_field_vec_named_unwrap_plain.jinja",
        include_str!("templates/default_field_vec_named_unwrap_plain.jinja"),
    ),
    (
        "default_field_vec_named_unwrap.jinja",
        include_str!("templates/default_field_vec_named_unwrap.jinja"),
    ),
    (
        "default_field_vec_non_primitive_comment.jinja",
        include_str!("templates/default_field_vec_non_primitive_comment.jinja"),
    ),
    (
        "default_field_vec_primitive_assign.jinja",
        include_str!("templates/default_field_vec_primitive_assign.jinja"),
    ),
    (
        "default_field_vec_serde_round_trip.jinja",
        include_str!("templates/default_field_vec_serde_round_trip.jinja"),
    ),
    (
        "swift_bytes_string_overload.jinja",
        include_str!("templates/swift_bytes_string_overload.jinja"),
    ),
    (
        "swift_bytes_array_overload.jinja",
        include_str!("templates/swift_bytes_array_overload.jinja"),
    ),
    (
        "swift_path_overload.jinja",
        include_str!("templates/swift_path_overload.jinja"),
    ),
    (
        "swift_trailing_param.jinja",
        include_str!("templates/swift_trailing_param.jinja"),
    ),
    (
        "swift_trailing_param_optional_default.jinja",
        include_str!("templates/swift_trailing_param_optional_default.jinja"),
    ),
    (
        "swift_trailing_arg.jinja",
        include_str!("templates/swift_trailing_arg.jinja"),
    ),
    (
        "rust_from_json_extern_decl.rs.jinja",
        include_str!("templates/rust_from_json_extern_decl.rs.jinja"),
    ),
    (
        "rust_from_json_shim.rs.jinja",
        include_str!("templates/rust_from_json_shim.rs.jinja"),
    ),
    (
        "inbound_registration_fn.rs.jinja",
        include_str!("templates/inbound_registration_fn.rs.jinja"),
    ),
    (
        "inbound_swift_type.rs.jinja",
        include_str!("templates/inbound_swift_type.rs.jinja"),
    ),
    (
        "inbound_swift_method.rs.jinja",
        include_str!("templates/inbound_swift_method.rs.jinja"),
    ),
    (
        "inbound_wrapper_struct.rs.jinja",
        include_str!("templates/inbound_wrapper_struct.rs.jinja"),
    ),
    (
        "inbound_plugin_impl.rs.jinja",
        include_str!("templates/inbound_plugin_impl.rs.jinja"),
    ),
    (
        "inbound_trait_impl_open.rs.jinja",
        include_str!("templates/inbound_trait_impl_open.rs.jinja"),
    ),
    (
        "inbound_register_fn.rs.jinja",
        include_str!("templates/inbound_register_fn.rs.jinja"),
    ),
    (
        "plugin_error_helper.rs.jinja",
        include_str!("templates/plugin_error_helper.rs.jinja"),
    ),
    (
        "inbound_method_open.rs.jinja",
        include_str!("templates/inbound_method_open.rs.jinja"),
    ),
    (
        "inbound_method_binding.rs.jinja",
        include_str!("templates/inbound_method_binding.rs.jinja"),
    ),
    (
        "inbound_method_result_unit.rs.jinja",
        include_str!("templates/inbound_method_result_unit.rs.jinja"),
    ),
    (
        "inbound_method_result_value.rs.jinja",
        include_str!("templates/inbound_method_result_value.rs.jinja"),
    ),
    (
        "inbound_method_mime_types.rs.jinja",
        include_str!("templates/inbound_method_mime_types.rs.jinja"),
    ),
    (
        "inbound_method_json_return.rs.jinja",
        include_str!("templates/inbound_method_json_return.rs.jinja"),
    ),
    (
        "inbound_method_unit_call.rs.jinja",
        include_str!("templates/inbound_method_unit_call.rs.jinja"),
    ),
    (
        "inbound_method_value_call.rs.jinja",
        include_str!("templates/inbound_method_value_call.rs.jinja"),
    ),
    (
        "rust_extern_opaque_type.rs.jinja",
        include_str!("templates/rust_extern_opaque_type.rs.jinja"),
    ),
    (
        "rust_extern_init.rs.jinja",
        include_str!("templates/rust_extern_init.rs.jinja"),
    ),
    (
        "rust_extern_configurator.rs.jinja",
        include_str!("templates/rust_extern_configurator.rs.jinja"),
    ),
    (
        "rust_extern_c_register_via_callback.rs.jinja",
        include_str!("templates/rust_extern_c_register_via_callback.rs.jinja"),
    ),
    (
        "rust_extern_entrypoint.rs.jinja",
        include_str!("templates/rust_extern_entrypoint.rs.jinja"),
    ),
    (
        "rust_extern_service_consolidated.rs.jinja",
        include_str!("templates/rust_extern_service_consolidated.rs.jinja"),
    ),
    (
        "rust_extern_service_type_and_constructor.rs.jinja",
        include_str!("templates/rust_extern_service_type_and_constructor.rs.jinja"),
    ),
    (
        "rust_extern_service_methods.rs.jinja",
        include_str!("templates/rust_extern_service_methods.rs.jinja"),
    ),
    (
        "swift_file_header.swift.jinja",
        include_str!("templates/swift_file_header.swift.jinja"),
    ),
    (
        "swift_class_header.swift.jinja",
        include_str!("templates/swift_class_header.swift.jinja"),
    ),
    (
        "swift_init.swift.jinja",
        include_str!("templates/swift_init.swift.jinja"),
    ),
    (
        "swift_deinit.swift.jinja",
        include_str!("templates/swift_deinit.swift.jinja"),
    ),
    (
        "swift_configurator.swift.jinja",
        include_str!("templates/swift_configurator.swift.jinja"),
    ),
    (
        "swift_registration.swift.jinja",
        include_str!("templates/swift_registration.swift.jinja"),
    ),
    (
        "swift_registration_variant.swift.jinja",
        include_str!("templates/swift_registration_variant.swift.jinja"),
    ),
    (
        "swift_silgen_callback.swift.jinja",
        include_str!("templates/swift_silgen_callback.swift.jinja"),
    ),
    (
        "swift_entrypoint.swift.jinja",
        include_str!("templates/swift_entrypoint.swift.jinja"),
    ),
    (
        "swift_class_footer.swift.jinja",
        include_str!("templates/swift_class_footer.swift.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}"))
}
