load("@rules_cc//cc:cc_test.bzl", "cc_test")
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_python//python:proto.bzl", "py_proto_library")
load("//bazel:cc_proto_library.bzl", "cc_proto_library")
load("//bazel:proto_library.bzl", "proto_library")
load("//build_defs:cpp_opts.bzl", "COPTS")

package(
    default_visibility = [
        "//pkg:__pkg__",
        "//src/google/protobuf/json:__pkg__",
    ],
)

licenses(["notice"])

cc_library(
    name = "json",
    srcs = ["json.cc"],
    hdrs = ["json.h"],
    copts = COPTS,
    strip_include_prefix = "/src",
    visibility = ["//visibility:public"],
    deps = [
        ":lexer",
        ":parser",
        ":unparser",
        ":writer",
        "//src/google/protobuf",
        "//src/google/protobuf:port",
        "//src/google/protobuf/io",
        "//src/google/protobuf/util:type_resolver",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/strings",
    ],
)

cc_test(
    name = "json_test",
    srcs = ["json_test.cc"],
    copts = COPTS,
    deps = [
        ":json",
        "//:duration_cc_proto",
        "//:field_mask_cc_proto",
        "//:struct_cc_proto",
        "//:timestamp_cc_proto",
        "//:wrappers_cc_proto",
        "//src/google/protobuf",
        "//src/google/protobuf:cc_test_protos",
        "//src/google/protobuf:port",
        "//src/google/protobuf:test_textproto",
        "//src/google/protobuf/io",
        "//src/google/protobuf/io:test_zero_copy_stream",
        "//src/google/protobuf/stubs",
        "//src/google/protobuf/util:json_format_cc_proto",
        "//src/google/protobuf/util:json_format_proto3_cc_proto",
        "//src/google/protobuf/util:type_resolver",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/status:statusor",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:cord",
        "@googletest//:gtest",
        "@googletest//:gtest_main",
    ],
)

cc_library(
    name = "message_path",
    srcs = ["internal/message_path.cc"],
    hdrs = ["internal/message_path.h"],
    copts = COPTS,
    strip_include_prefix = "/src",
    deps = [
        "//src/google/protobuf",
        "//src/google/protobuf:port",
        "@abseil-cpp//absl/cleanup",
        "@abseil-cpp//absl/strings",
    ],
)

cc_library(
    name = "writer",
    srcs = ["internal/writer.cc"],
    hdrs = ["internal/writer.h"],
    copts = COPTS,
    strip_include_prefix = "/src",
    deps = [
        "//src/google/protobuf:port",
        "//src/google/protobuf/io",
        "//src/google/protobuf/io:tokenizer",
        "//src/google/protobuf/io:zero_copy_sink",
        "//src/google/protobuf/stubs",
        "@abseil-cpp//absl/algorithm:container",
        "@abseil-cpp//absl/log:absl_check",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:charset",
        "@abseil-cpp//absl/strings:str_format",
    ],
)

cc_library(
    name = "zero_copy_buffered_stream",
    srcs = ["internal/zero_copy_buffered_stream.cc"],
    hdrs = ["internal/zero_copy_buffered_stream.h"],
    copts = COPTS,
    strip_include_prefix = "/src",
    deps = [
        "//src/google/protobuf:port",
        "//src/google/protobuf/io",
        "//src/google/protobuf/stubs",
        "@abseil-cpp//absl/algorithm:container",
        "@abseil-cpp//absl/log:absl_check",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/status:statusor",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:str_format",
    ],
)

cc_test(
    name = "zero_copy_buffered_stream_test",
    srcs = ["internal/zero_copy_buffered_stream_test.cc"],
    copts = COPTS,
    deps = [
        ":zero_copy_buffered_stream",
        "//src/google/protobuf/io:test_zero_copy_stream",
        "//src/google/protobuf/stubs",
        "@abseil-cpp//absl/strings",
        "@googletest//:gtest",
        "@googletest//:gtest_main",
    ],
)

cc_library(
    name = "lexer",
    srcs = ["internal/lexer.cc"],
    hdrs = ["internal/lexer.h"],
    copts = COPTS,
    strip_include_prefix = "/src",
    deps = [
        ":message_path",
        ":zero_copy_buffered_stream",
        "//src/google/protobuf",
        "//src/google/protobuf:port",
        "//src/google/protobuf/io",
        "//src/google/protobuf/stubs",
        "//third_party/utf8_range:utf8_validity",
        "@abseil-cpp//absl/algorithm:container",
        "@abseil-cpp//absl/base",
        "@abseil-cpp//absl/base:core_headers",
        "@abseil-cpp//absl/log:absl_check",
        "@abseil-cpp//absl/numeric:bits",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/status:statusor",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:str_format",
        "@abseil-cpp//absl/types:optional",
    ],
)

cc_test(
    name = "lexer_test",
    timeout = "long",
    srcs = ["internal/lexer_test.cc"],
    copts = COPTS,
    deps = [
        ":lexer",
        "//src/google/protobuf:port",
        "//src/google/protobuf/io",
        "//src/google/protobuf/io:test_zero_copy_stream",
        "//src/google/protobuf/stubs",
        "@abseil-cpp//absl/algorithm:container",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/status:status_matchers",
        "@abseil-cpp//absl/status:statusor",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:str_format",
        "@googletest//:gtest",
        "@googletest//:gtest_main",
    ],
)

cc_library(
    name = "untyped_message",
    srcs = ["internal/untyped_message.cc"],
    hdrs = ["internal/untyped_message.h"],
    copts = COPTS,
    strip_include_prefix = "/src",
    visibility = ["//visibility:private"],
    deps = [
        "//:type_cc_proto",
        "//src/google/protobuf",
        "//src/google/protobuf:port",
        "//src/google/protobuf:protobuf_lite",
        "//src/google/protobuf/io",
        "//src/google/protobuf/stubs",
        "//src/google/protobuf/util:type_resolver",
        "//third_party/utf8_range:utf8_validity",
        "@abseil-cpp//absl/container:flat_hash_map",
        "@abseil-cpp//absl/log:absl_check",
        "@abseil-cpp//absl/log:absl_log",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:str_format",
        "@abseil-cpp//absl/types:optional",
        "@abseil-cpp//absl/types:span",
    ],
)

cc_library(
    name = "descriptor_traits",
    hdrs = ["internal/descriptor_traits.h"],
    copts = COPTS,
    strip_include_prefix = "/src",
    deps = [
        ":lexer",
        ":untyped_message",
        "//:type_cc_proto",
        "//src/google/protobuf",
        "//src/google/protobuf:json_enumvalue_options_cc_proto",
        "//src/google/protobuf:port",
        "//src/google/protobuf/stubs",
        "@abseil-cpp//absl/algorithm:container",
        "@abseil-cpp//absl/container:flat_hash_map",
        "@abseil-cpp//absl/log:absl_log",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/status:statusor",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:str_format",
        "@abseil-cpp//absl/types:optional",
    ],
)

cc_library(
    name = "parser",
    srcs = [
        "internal/parser.cc",
    ],
    hdrs = [
        "internal/parser.h",
        "internal/parser_traits.h",
    ],
    copts = COPTS,
    strip_include_prefix = "/src",
    deps = [
        ":descriptor_traits",
        ":lexer",
        ":zero_copy_buffered_stream",
        "//:type_cc_proto",
        "//src/google/protobuf",
        "//src/google/protobuf:port",
        "//src/google/protobuf:protobuf_lite",
        "//src/google/protobuf/io",
        "//src/google/protobuf/io:zero_copy_sink",
        "//src/google/protobuf/stubs",
        "//src/google/protobuf/util:type_resolver",
        "@abseil-cpp//absl/base",
        "@abseil-cpp//absl/base:core_headers",
        "@abseil-cpp//absl/container:flat_hash_map",
        "@abseil-cpp//absl/container:flat_hash_set",
        "@abseil-cpp//absl/log:absl_check",
        "@abseil-cpp//absl/log:absl_log",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/status:statusor",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:str_format",
        "@abseil-cpp//absl/types:optional",
        "@abseil-cpp//absl/types:span",
    ],
)

cc_library(
    name = "unparser",
    srcs = [
        "internal/unparser.cc",
    ],
    hdrs = [
        "internal/unparser.h",
        "internal/unparser_traits.h",
    ],
    copts = COPTS,
    strip_include_prefix = "/src",
    deps = [
        ":descriptor_traits",
        ":untyped_message",
        ":writer",
        "//:type_cc_proto",
        "//src/google/protobuf",
        "//src/google/protobuf:port",
        "//src/google/protobuf/io",
        "//src/google/protobuf/io:zero_copy_sink",
        "//src/google/protobuf/stubs",
        "//src/google/protobuf/util:type_resolver",
        "@abseil-cpp//absl/log:absl_check",
        "@abseil-cpp//absl/log:absl_log",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/status:statusor",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:str_format",
        "@abseil-cpp//absl/types:optional",
    ],
)

filegroup(
    name = "json_enumval_custom_string_proto_srcs",
    testonly = True,
    srcs = ["json_enumval_custom_string.proto"],
    visibility = ["//:__subpackages__"],
)

proto_library(
    name = "json_enumval_custom_string_proto",
    testonly = True,
    srcs = ["json_enumval_custom_string.proto"],
    strip_import_prefix = "/src",
    visibility = [
        "//:__subpackages__",
        "//src/google/protobuf:__subpackages__",
        "//upb:__subpackages__",
    ],
    deps = ["//src/google/protobuf:json_options_proto"],
)

cc_proto_library(
    name = "json_enumval_custom_string_cc_proto",
    testonly = True,
    deps = [":json_enumval_custom_string_proto"],
)

py_proto_library(
    name = "json_enumval_custom_string_py_proto",
    testonly = True,
    visibility = ["//third_party/py/google/protobuf/internal:__pkg__"],
    deps = [":json_enumval_custom_string_proto"],
)

cc_test(
    name = "json_enumval_custom_string_test",
    srcs = ["json_enumval_custom_string_test.cc"],
    deps = [
        ":json",
        ":json_enumval_custom_string_cc_proto",
        "//src/google/protobuf:arena",
        "@abseil-cpp//absl/log:absl_check",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/status:status_matchers",
        "@abseil-cpp//absl/strings:string_view",
        "@googletest//:gtest",
        "@googletest//:gtest_main",
    ],
)
