load("@fbcode//quic:defs.bzl", "mvfst_cpp_library")
load("@fbsource//tools/build_defs:fb_native_wrapper.bzl", "fb_native")

oncall("traffic_protocols")

fb_native.constraint_setting(
    name = "use_alt_crypto",
    visibility = ["PUBLIC"],
)

fb_native.constraint_value(
    name = "use_alt_crypto-enabled",
    constraint_setting = ":use_alt_crypto",
    visibility = ["PUBLIC"],
)

fb_native.constraint_setting(
    name = "force_alt_crypto",
    visibility = ["PUBLIC"],
)

fb_native.constraint_value(
    name = "force_alt_crypto-enabled",
    constraint_setting = ":force_alt_crypto",
    visibility = ["PUBLIC"],
)

# Deps for the `config` target's exported_deps, split by which mvfst-config
# header the `headers` select picks. Keep these on the same select axes as
# `headers` so ARVR Android can keep the server address aliases.
_CONFIG_SERVER_DEPS = [
    "//folly:network_address",  # @manual
    "//folly:small_vector",  # @manual
    "//folly/container:f14_hash",  # @manual
    "//folly/container:heap_vector_types",  # @manual
    "//quic/common/third-party:tiny_optional",  # @manual
]

_CONFIG_SOCKET_ADDRESS_MOBILE_DEPS = [
    "//quic/common/address:quic_socket_address",  # @manual
]

mvfst_cpp_library(
    name = "enum",
    headers = select({
        "DEFAULT": {
            "QuicEnum.h": "QuicEnum.h",
        },
        # Mobile: use plain enum class without BETTER_ENUM overhead (~6KB savings)
        # ARVR Android uses BETTER_ENUM (needs TransportKnobs, etc.)
        "ovr_config//os/constraints:android": select({
            "DEFAULT": {
                "QuicEnum.h": "facebook/QuicEnumMobile.h",
            },
            "ovr_config//build_mode:arvr_mode[enabled]": {
                "QuicEnum.h": "QuicEnum.h",
            },
        }),
        "ovr_config//os/constraints:iphoneos": {
            "QuicEnum.h": "facebook/QuicEnumMobile.h",
        },
    }),
    exported_deps = select({
        "DEFAULT": [
            "//folly/lang:assume",
            "//quic/common/third-party:better_enums",
        ],
        "ovr_config//os/constraints:android": select({
            "DEFAULT": [],
            "ovr_config//build_mode:arvr_mode[enabled]": [
                "//folly/lang:assume",
                "//quic/common/third-party:better_enums",
            ],
        }),
        "ovr_config//os/constraints:iphoneos": [],
    }),
)

mvfst_cpp_library(
    name = "logging_config",
    headers = select({
        # @oss-only: "DEFAULT": {"quic-logging-config.h": "//:quic-logging-config.h"},
        "DEFAULT": {"quic-logging-config.h": "facebook/quic-logging-config-xplat.h"},  # @fb-only
        "ovr_config//runtime/constraints:fbcode": {"quic-logging-config.h": "facebook/quic-logging-config-fbcode.h"},  # @fb-only
    }),
)

mvfst_cpp_library(
    name = "config",
    headers = select({
        # @oss-only: "DEFAULT": {"mvfst-config.h": "//:mvfst-config.h"},
        "DEFAULT": select({
            "DEFAULT": select({
                "DEFAULT": {"mvfst-config.h": "mvfst-config.h"},  # @fb-only
                "ovr_config//os/constraints:android": {"mvfst-config.h": "facebook/mvfst-config-mobile.h"},  # @fb-only
                "ovr_config//os/constraints:iphoneos": {"mvfst-config.h": "facebook/mvfst-config-mobile.h"},  # @fb-only
            }),
            "ovr_config//build_mode:arvr_mode[enabled]": {"mvfst-config.h": "mvfst-config.h"},  # @fb-only
        }),
        "ovr_config//runtime/constraints:fbcode": select({
            "DEFAULT": {"mvfst-config.h": "facebook/mvfst-config-fbcode.h"},  # @fb-only
            "fbcode//quic:use_alt_crypto-enabled": {"mvfst-config.h": "facebook/mvfst-config-alt-crypto.h"},  # @fb-only
            "ovr_config//build_mode:arvr_mode[enabled]": {"mvfst-config.h": "mvfst-config.h"},  # @fb-only
            "ovr_config//runtime/constraints:android-host-test": {"mvfst-config.h": "mvfst-config.h"},  # @fb-only
        }),  # @fb-only
    }),
    exported_deps = select({
        # @oss-only: "DEFAULT": _CONFIG_SERVER_DEPS,
        "DEFAULT": select({
            "DEFAULT": select({
                "DEFAULT": _CONFIG_SERVER_DEPS,  # @fb-only
                "ovr_config//os/constraints:android": _CONFIG_SOCKET_ADDRESS_MOBILE_DEPS,  # @fb-only
                "ovr_config//os/constraints:iphoneos": _CONFIG_SOCKET_ADDRESS_MOBILE_DEPS,  # @fb-only
            }),
            "ovr_config//build_mode:arvr_mode[enabled]": _CONFIG_SERVER_DEPS,  # @fb-only
        }),
        "ovr_config//runtime/constraints:fbcode": select({
            "DEFAULT": _CONFIG_SERVER_DEPS,  # @fb-only
            "fbcode//quic:use_alt_crypto-enabled": _CONFIG_SERVER_DEPS,  # @fb-only
            "ovr_config//build_mode:arvr_mode[enabled]": _CONFIG_SERVER_DEPS,  # @fb-only
            "ovr_config//runtime/constraints:android-host-test": _CONFIG_SERVER_DEPS,  # @fb-only
        }),  # @fb-only
    }),
)

_CONSTANTS_SERVER_HEADERS = {
    "QuicConstants.h": "QuicConstants.h",
    "QuicTypealiases.h": "QuicTypealiases.h",
}

_CONSTANTS_SOCKET_ADDRESS_MOBILE_HEADERS = {
    "QuicConstants.h": "QuicConstants.h",
    "QuicTypealiases.h": "QuicTypealiasesSocketAddressMobile.h",
}

_CONSTANTS_TYPEALIASES_MOBILE_HEADERS = {
    "QuicConstants.h": "QuicConstants.h",
    "QuicTypealiases.h": "QuicTypealiasesMobile.h",
}

_CONSTANTS_SERVER_DEPS = [
    ":enum",
    "//folly/chrono:clock",
    "//folly/io:iobuf",
]

_CONSTANTS_SOCKET_ADDRESS_MOBILE_DEPS = [
    ":config",
    ":enum",
    "//folly/chrono:clock",
    "//folly/io:iobuf",
]

_CONSTANTS_TYPEALIASES_MOBILE_DEPS = [
    ":config",
    ":enum",
    "//folly/chrono:clock",
    "//quic/common:quic_buffer",
    "//quic/common:quic_iobuf_queue",
]

mvfst_cpp_library(
    name = "constants",
    srcs = select({
        "DEFAULT": ["QuicConstants.cpp"],
        "ovr_config//os/constraints:android": ["facebook/QuicConstantsMobile.cpp"],
        "ovr_config//os/constraints:iphoneos": ["facebook/QuicConstantsMobile.cpp"],
    }),
    headers = select({
        "DEFAULT": select({
            "DEFAULT": select({
                "DEFAULT": select({
                    "DEFAULT": _CONSTANTS_SERVER_HEADERS,
                    "ovr_config//os/constraints:android": _CONSTANTS_SOCKET_ADDRESS_MOBILE_HEADERS,
                    "ovr_config//os/constraints:iphoneos": _CONSTANTS_SOCKET_ADDRESS_MOBILE_HEADERS,
                }),
                "ovr_config//build_mode:arvr_mode[enabled]": _CONSTANTS_SERVER_HEADERS,
            }),
            "ovr_config//runtime/constraints:fbcode": select({
                "DEFAULT": _CONSTANTS_SERVER_HEADERS,
                "fbcode//quic:use_alt_crypto-enabled": _CONSTANTS_SERVER_HEADERS,
                "ovr_config//build_mode:arvr_mode[enabled]": _CONSTANTS_SERVER_HEADERS,
                "ovr_config//runtime/constraints:android-host-test": _CONSTANTS_SERVER_HEADERS,
            }),
        }),
        "fbsource//tools/build_defs/config/apps/shared:constraint-value-quic-typealiases-mobile": _CONSTANTS_TYPEALIASES_MOBILE_HEADERS,
    }),
    deps = [
        "//folly/lang:assume",
    ],
    exported_deps = select({
        "DEFAULT": select({
            "DEFAULT": select({
                "DEFAULT": select({
                    "DEFAULT": _CONSTANTS_SERVER_DEPS,
                    "ovr_config//os/constraints:android": _CONSTANTS_SOCKET_ADDRESS_MOBILE_DEPS,
                    "ovr_config//os/constraints:iphoneos": _CONSTANTS_SOCKET_ADDRESS_MOBILE_DEPS,
                }),
                "ovr_config//build_mode:arvr_mode[enabled]": _CONSTANTS_SERVER_DEPS,
            }),
            "ovr_config//runtime/constraints:fbcode": select({
                "DEFAULT": _CONSTANTS_SERVER_DEPS,
                "fbcode//quic:use_alt_crypto-enabled": _CONSTANTS_SERVER_DEPS,
                "ovr_config//build_mode:arvr_mode[enabled]": _CONSTANTS_SERVER_DEPS,
                "ovr_config//runtime/constraints:android-host-test": _CONSTANTS_SERVER_DEPS,
            }),
        }),
        "fbsource//tools/build_defs/config/apps/shared:constraint-value-quic-typealiases-mobile": _CONSTANTS_TYPEALIASES_MOBILE_DEPS,
    }),
)

mvfst_cpp_library(
    name = "exception",
    srcs = select({
        "DEFAULT": ["QuicException.cpp"],
        "ovr_config//os/constraints:android": ["facebook/QuicExceptionMobile.cpp"],
        "ovr_config//os/constraints:iphoneos": ["facebook/QuicExceptionMobile.cpp"],
    }),
    headers = [
        "QuicException.h",
    ],
    deps = select({
        "DEFAULT": ["//quic/common:mvfst_logging"],
        "ovr_config//os/constraints:android": [],
        "ovr_config//os/constraints:iphoneos": [],
    })
    + [],
    exported_deps = [
        "fbsource//third-party/fmt:fmt",
        ":constants",
        ":tls_exception",
        "//folly:range",
        "//quic/common:optional",
        "//quic/common:variant",
    ],
)

mvfst_cpp_library(
    # @autodeps-skip
    name = "tls_exception",
    srcs = select({
        "DEFAULT": ["QuicTLSExceptionFizz.cpp"],
        "ovr_config//os/constraints:android": select({
            "DEFAULT": ["facebook/mbed/QuicTLSExceptionMbed.cpp"],  # @fb-only
            "ovr_config//build_mode:arvr_mode[enabled]": ["QuicTLSExceptionFizz.cpp"],
        }),
        "ovr_config//os/constraints:iphoneos": ["facebook/mbed/QuicTLSExceptionMbed.cpp"],  # @fb-only
    }),
    headers = [
        "QuicTLSException.h",
    ],
    deps = select({
        "DEFAULT": [
            "//fizz/record:record",
        ],
        "ovr_config//os/constraints:android": select({
            "DEFAULT": [
                "fbsource//xplat/mobilenetwork:tls_common",  # @manual
            ],
            "ovr_config//build_mode:arvr_mode[enabled]": [
                "//fizz/record:record",
            ],
        }),
        "ovr_config//os/constraints:iphoneos": [
            "fbsource//xplat/mobilenetwork:tls_common",  # @manual
        ],
    }),
    exported_deps = [
        ":constants",
    ],
)

fb_native.export_file(
    name = "src-tree",
    src = ".",
    mode = "reference",
    visibility = ["PUBLIC"],
)
