load("@rules_java//java:java_test.bzl", "java_test")
load("@rules_jvm_external//:kt_defs.bzl", "kt_jvm_export")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
load("//:protobuf.bzl", "internal_gen_kt_protos")
load("//:protobuf_version.bzl", "PROTOBUF_JAVA_VERSION")
load("//bazel:java_lite_proto_library.bzl", "java_lite_proto_library")
load("//build_defs:kotlin_opts.bzl", "protobuf_versioned_kt_jvm_library")

java_lite_proto_library(
    name = "example_extensible_message_java_proto_lite",
    deps = ["//java/kotlin:example_extensible_message_proto"],
)

kt_jvm_library(
    name = "lite_extensions",
    srcs = ["src/main/kotlin/com/google/protobuf/ExtendableMessageLiteExtensions.kt"],
    deps = ["//java/lite"],
)

internal_gen_kt_protos(
    name = "gen_well_known_protos_kotlinlite",
    lite = True,
    deps = [
        "//:any_proto",
        "//:api_proto",
        "//:descriptor_proto",
        "//:duration_proto",
        "//:empty_proto",
        "//:field_mask_proto",
        "//:java_features_proto",
        "//:source_context_proto",
        "//:struct_proto",
        "//:timestamp_proto",
        "//:type_proto",
        "//:wrappers_proto",
    ],
)

kt_jvm_library(
    name = "well_known_protos_kotlin_lite",
    srcs = [
        ":gen_well_known_protos_kotlinlite",
    ],
    deps = [
        "//java/kotlin:only_for_use_in_proto_generated_code_its_generator_and_tests",
        "//java/kotlin:shared_runtime",
        "//java/lite",
    ],
)

protobuf_versioned_kt_jvm_library(
    name = "kotlin-lite_bundle",
    automatic_module_name = "com.google.protobuf",
    bundle_description = "Kotlin lite Protocol Buffers library. Protocol " +
                         "Buffers are a way of encoding structured data in " +
                         "an efficient yet extensible format.",
    bundle_name = "Protocol Buffers [Kotlin-Lite]",
    bundle_symbolic_name = "com.google.protobuf",
    visibility = ["//visibility:public"],
    exports = [
        ":lite_extensions",
        ":well_known_protos_kotlin_lite",
        "//java/kotlin:bytestring_lib",
        "//java/kotlin:only_for_use_in_proto_generated_code_its_generator_and_tests",
        "//java/kotlin:shared_runtime",
    ],
)

kt_jvm_export(
    name = "kotlin-lite_mvn",
    deploy_env = [
        "//java/lite",
    ],
    maven_coordinates = "com.google.protobuf:protobuf-kotlin-lite:%s" % PROTOBUF_JAVA_VERSION,
    pom_template = "//java/kotlin-lite:pom_template.xml",
    resources = [
        "//:java_features_proto",
        "//:well_known_type_protos",
        "//src/google/protobuf:descriptor_proto_srcs",
    ],
    tags = ["manual"],
    runtime_deps = [
        ":kotlin-lite_bundle",
    ],
)

filegroup(
    name = "release",
    srcs = [
        ":kotlin-lite_mvn-docs",
        ":kotlin-lite_mvn-maven-source",
        ":kotlin-lite_mvn-pom",
        ":kotlin-lite_mvn-project",
    ],
    visibility = ["//java:__pkg__"],
)

test_suite(
    name = "tests",
    tests = [
        "proto2_test_lite",
        "proto3_test_lite",
        "test_lite_extensions",
    ],
)

kt_jvm_library(
    name = "test_lite_extensions_library",
    srcs = ["src/test/kotlin/com/google/protobuf/ExtendableMessageLiteExtensionsTest.kt"],
    deps = [
        ":example_extensible_message_java_proto_lite",
        ":lite_extensions",
        "//java/kotlin:only_for_use_in_proto_generated_code_its_generator_and_tests",
        "//java/kotlin:shared_runtime",
        "//java/lite",
        "@protobuf_maven_dev//:com_google_truth_truth",
        "@protobuf_maven_dev//:junit_junit",
        "@rules_kotlin//kotlin/compiler:kotlin-test",
    ],
)

java_test(
    name = "test_lite_extensions",
    test_class = "com.google.protobuf.kotlin.ExtendableMessageLiteExtensionsTest",
    runtime_deps = [":test_lite_extensions_library"],
)

java_lite_proto_library(
    name = "evil_names_proto2_java_proto_lite",
    deps = ["//java/kotlin:evil_names_proto2"],
)

internal_gen_kt_protos(
    name = "gen_evil_names_proto2_lite",
    lite = True,
    deps = ["//java/kotlin:evil_names_proto2"],
)

java_lite_proto_library(
    name = "evil_names_proto3_java_proto_lite",
    deps = ["//java/kotlin:evil_names_proto3"],
)

internal_gen_kt_protos(
    name = "gen_evil_names_proto3_lite",
    lite = True,
    deps = ["//java/kotlin:evil_names_proto3"],
)

java_lite_proto_library(
    name = "multiple_files_proto3_java_proto_lite",
    deps = ["//java/kotlin:multiple_files_proto3"],
)

internal_gen_kt_protos(
    name = "gen_kotlin_proto3_java_multiple_files_lite",
    deps = ["//java/kotlin:multiple_files_proto3"],
)

internal_gen_kt_protos(
    name = "gen_kotlin_unittest_lite",
    lite = True,
    deps = ["//src/google/protobuf:lite_test_protos"],
)

kt_jvm_library(
    name = "kotlin_unittest_lite",
    srcs = [
        ":gen_evil_names_proto2_lite",
        ":gen_kotlin_unittest_lite",
    ],
    deps = [
        ":evil_names_proto2_java_proto_lite",
        "//java/core:generic_test_protos_java_proto_lite",
        "//java/core:lite_test_protos_java_proto_lite",
        "//java/kotlin:only_for_use_in_proto_generated_code_its_generator_and_tests",
        "//java/kotlin:shared_runtime",
        "//java/lite",
    ],
)

internal_gen_kt_protos(
    name = "gen_kotlin_proto3_unittest_lite",
    lite = True,
    deps = ["//src/google/protobuf:kt_proto3_unittest_protos"],
)

kt_jvm_library(
    name = "kotlin_proto3_unittest_lite",
    srcs = [
        ":gen_evil_names_proto3_lite",
        ":gen_kotlin_proto3_java_multiple_files_lite",
        ":gen_kotlin_proto3_unittest_lite",
    ],
    deps = [
        ":evil_names_proto3_java_proto_lite",
        ":multiple_files_proto3_java_proto_lite",
        "//java/core:generic_test_protos_java_proto_lite",
        "//java/core:lite_test_protos_java_proto_lite",
        "//java/kotlin:only_for_use_in_proto_generated_code_its_generator_and_tests",
        "//java/kotlin:shared_runtime",
        "//java/lite",
    ],
)

kt_jvm_library(
    name = "proto2_test_lite_library",
    srcs = ["src/test/kotlin/com/google/protobuf/Proto2LiteTest.kt"],
    deps = [
        ":kotlin_unittest_lite",
        "//java/core:test_util_lite",
        "@protobuf_maven_dev//:com_google_truth_truth",
        "@protobuf_maven_dev//:junit_junit",
    ],
)

java_test(
    name = "proto2_test_lite",
    test_class = "com.google.protobuf.kotlin.Proto2LiteTest",
    runtime_deps = [":proto2_test_lite_library"],
)

kt_jvm_library(
    name = "proto3_test_lite_library",
    srcs = ["//java/kotlin:src/test/kotlin/com/google/protobuf/Proto3Test.kt"],
    deps = [
        ":kotlin_proto3_unittest_lite",
        "//java/core:test_util_lite",
        "@protobuf_maven_dev//:com_google_truth_truth",
        "@protobuf_maven_dev//:junit_junit",
    ],
)

java_test(
    name = "proto3_test_lite",
    test_class = "com.google.protobuf.kotlin.Proto3Test",
    runtime_deps = [":proto3_test_lite_library"],
)
