load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_java//java:defs.bzl", "java_proto_library")
load("@rules_jvm_external//:kt_defs.bzl", "kt_jvm_export")
load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//:protobuf_version.bzl", "PROTOBUF_JAVA_VERSION")
load("//:protobuf.bzl", "internal_gen_kt_protos")

exports_files([
    "src/test/kotlin/com/google/protobuf/Proto3Test.kt",
])

# Kotlin generated protos depend on this and only this.
kt_jvm_library(
    name = "shared_runtime",
    srcs = [
        "src/main/kotlin/com/google/protobuf/DslList.kt",
        "src/main/kotlin/com/google/protobuf/DslMap.kt",
        "src/main/kotlin/com/google/protobuf/DslProxy.kt",
        "src/main/kotlin/com/google/protobuf/ExtensionList.kt",
        "src/main/kotlin/com/google/protobuf/ProtoDslMarker.kt",
        "src/main/kotlin/com/google/protobuf/UnmodifiableCollections.kt",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        "//java/lite",
    ],
)

kt_jvm_library(
    name = "only_for_use_in_proto_generated_code_its_generator_and_tests",
    srcs = ["src/main/kotlin/com/google/protobuf/OnlyForUseByGeneratedProtoCode.kt"],
    visibility = ["//java:__subpackages__"],
)

kt_jvm_library(
    name = "bytestring_lib",
    srcs = ["src/main/kotlin/com/google/protobuf/ByteStrings.kt"],
    visibility = ["//java:__subpackages__"],
    deps = ["//java/lite"],
)

kt_jvm_library(
    name = "full_extensions",
    srcs = [
        "src/main/kotlin/com/google/protobuf/Anies.kt",
        "src/main/kotlin/com/google/protobuf/ExtendableMessageExtensions.kt",
    ],
    deps = ["//java/core"],
)

kt_jvm_export(
    name = "kotlin_mvn",
    deploy_env = [
        "@com_github_jetbrains_kotlin//:kotlin-stdlib",
        "//java/core",
    ],
    maven_coordinates = "com.google.protobuf:protobuf-kotlin:%s" % PROTOBUF_JAVA_VERSION,
    pom_template = "//java/kotlin:pom_template.xml",
    resources = [
        "//:built_in_runtime_protos",
        "//:well_known_type_protos",
    ],
    tags = ["manual"],
    runtime_deps = [
        ":bytestring_lib",
        ":full_extensions",
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        ":shared_runtime",
        ":well_known_protos_kotlin",
    ],
)

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

test_suite(
    name = "tests",
    tests = [
        "bytestring_test",
        "proto2_test",
        "proto3_test",
        "shared_tests",
        "test_extensions",
    ],
)

kt_jvm_library(
    name = "bytestring_test_library",
    srcs = ["src/test/kotlin/com/google/protobuf/ByteStringsTest.kt"],
    deps = [
        ":bytestring_lib",
        "//java/lite",
        "@com_github_jetbrains_kotlin//:kotlin-test",
        "@maven//:com_google_truth_truth",
        "@maven//:junit_junit",
    ],
)

java_test(
    name = "bytestring_test",
    test_class = "com.google.protobuf.kotlin.ByteStringsTest",
    runtime_deps = [":bytestring_test_library"],
)

proto_library(
    name = "example_extensible_message_proto",
    srcs = ["src/test/proto/com/google/protobuf/example_extensible_message.proto"],
    visibility = ["//java:__subpackages__"],
)

java_proto_library(
    name = "example_extensible_message_java_proto",
    deps = [":example_extensible_message_proto"],
)

kt_jvm_library(
    name = "shared_tests_library",
    srcs = [
        "src/test/kotlin/com/google/protobuf/DslListTest.kt",
        "src/test/kotlin/com/google/protobuf/DslMapTest.kt",
        "src/test/kotlin/com/google/protobuf/ExtensionListTest.kt",
    ],
    deps = [
        ":bytestring_lib",
        ":example_extensible_message_java_proto",
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        ":shared_runtime",
        "@com_github_jetbrains_kotlin//:kotlin-test",
        "@maven//:com_google_guava_guava_testlib",
        "@maven//:com_google_truth_truth",
        "@maven//:junit_junit",
    ],
)

java_test(
    name = "shared_tests",
    test_class = "com.google.protobuf.kotlin.DslListTest",
    runtime_deps = [":shared_tests_library"],
)

kt_jvm_library(
    name = "test_extensions_library",
    srcs = [
        "src/test/kotlin/com/google/protobuf/AniesTest.kt",
        "src/test/kotlin/com/google/protobuf/ExtendableMessageExtensionsTest.kt",
    ],
    deps = [
        ":example_extensible_message_java_proto",
        ":full_extensions",
        ":kotlin_unittest",
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        ":shared_runtime",
        "//java/core",
        "@com_github_jetbrains_kotlin//:kotlin-test",
        "@maven//:com_google_truth_truth",
        "@maven//:junit_junit",
    ],
)

java_test(
    name = "test_extensions",
    test_class = "com.google.protobuf.kotlin.ExtendableMessageExtensionsTest",
    runtime_deps = [":test_extensions_library"],
)

proto_library(
    name = "evil_names_proto2",
    srcs = ["src/test/proto/com/google/protobuf/evil_names_proto2.proto"],
    visibility = ["//:__subpackages__"],
)

java_proto_library(
    name = "evil_names_proto2_java_proto",
    deps = [":evil_names_proto2"],
)

internal_gen_kt_protos(
    name = "gen_evil_names_proto2",
    deps = [":evil_names_proto2"],
)

proto_library(
    name = "evil_names_proto3",
    srcs = ["src/test/proto/com/google/protobuf/evil_names_proto3.proto"],
    visibility = ["//:__subpackages__"],
)

java_proto_library(
    name = "evil_names_proto3_java_proto",
    deps = [":evil_names_proto3"],
)

internal_gen_kt_protos(
    name = "gen_evil_names_proto3",
    deps = [":evil_names_proto3"],
)

proto_library(
    name = "multiple_files_proto3",
    srcs = ["src/test/proto/com/google/protobuf/multiple_files_proto3.proto"],
    visibility = ["//:__subpackages__"],
)

java_proto_library(
    name = "multiple_files_proto3_java_proto",
    deps = [":multiple_files_proto3"],
)

internal_gen_kt_protos(
    name = "gen_kotlin_proto3_java_multiple_files",
    deps = [":multiple_files_proto3"],
)

internal_gen_kt_protos(
    name = "gen_kotlin_unittest",
    visibility = ["//java:__subpackages__"],
    deps = ["//:kt_unittest"],
)

kt_jvm_library(
    name = "kotlin_unittest",
    srcs = [
        ":gen_evil_names_proto2",
        ":gen_kotlin_unittest",
    ],
    deps = [
        ":evil_names_proto2_java_proto",
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        ":shared_runtime",
        ":well_known_protos_kotlin",
        "//java/core",
        "//java/core:generic_test_protos_java_proto",
    ],
)

internal_gen_kt_protos(
    name = "gen_kotlin_proto3_unittest",
    deps = ["//:kt_proto3_unittest"],
)

kt_jvm_library(
    name = "kotlin_proto3_unittest",
    srcs = [
        ":gen_evil_names_proto3",
        ":gen_kotlin_proto3_java_multiple_files",
        ":gen_kotlin_proto3_unittest",
    ],
    deps = [
        ":evil_names_proto3_java_proto",
        ":multiple_files_proto3_java_proto",
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        ":shared_runtime",
        "//java/core",
        "//java/core:generic_test_protos_java_proto",
    ],
)

kt_jvm_library(
    name = "proto2_test_library",
    srcs = ["src/test/kotlin/com/google/protobuf/Proto2Test.kt"],
    deps = [
        ":kotlin_unittest",
        "//java/core:test_util",
        "@maven//:com_google_truth_truth",
        "@maven//:junit_junit",
    ],
)

java_test(
    name = "proto2_test",
    test_class = "com.google.protobuf.kotlin.Proto2Test",
    runtime_deps = [":proto2_test_library"],
)

kt_jvm_library(
    name = "proto3_test_library",
    srcs = ["src/test/kotlin/com/google/protobuf/Proto3Test.kt"],
    deps = [
        ":kotlin_proto3_unittest",
        "//java/core:test_util",
        "@maven//:com_google_truth_truth",
        "@maven//:junit_junit",
    ],
)

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

internal_gen_kt_protos(
    name = "gen_well_known_protos_kotlin",
    visibility = [
        "//java:__subpackages__",
    ],
    deps = [
        "//:any_proto",
        "//:api_proto",
        "//:compiler_plugin_proto",
        "//:descriptor_proto",
        "//:duration_proto",
        "//:empty_proto",
        "//:field_mask_proto",
        "//:source_context_proto",
        "//:struct_proto",
        "//:timestamp_proto",
        "//:type_proto",
        "//:wrappers_proto",
    ],
)

kt_jvm_library(
    name = "well_known_protos_kotlin",
    srcs = [
        ":gen_well_known_protos_kotlin",
    ],
    deps = [
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        ":shared_runtime",
        "//java/core",
    ],
)

pkg_files(
    name = "dist_files",
    srcs = glob([
        "src/main/kotlin/com/google/protobuf/*.kt",
        "src/test/kotlin/**/*.kt",
        "src/test/proto/**/*.proto",
    ]) + [
        "BUILD.bazel",
        "generate-sources-build.xml",
        "generate-test-sources-build.xml",
        "pom.xml",
        "pom_template.xml",
    ],
    strip_prefix = strip_prefix.from_root(""),
    visibility = ["//java:__pkg__"],
)
