load("@aspect_rules_ts//ts:defs.bzl", rules_js_tsconfig = "ts_config")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@devinfra//bazel/validation:defs.bzl", "validate_ts_version_matching")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("//tools:defaults.bzl", "copy_to_bin")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

exports_files([
    "LICENSE",
    "tsconfig.json",
    "tsconfig-test.json",
    "tsconfig-build-ng.json",
    "tsconfig-build.json",
    "package.json",
])

npm_link_all_packages()

rules_js_tsconfig(
    name = "tsconfig",
    src = "tsconfig.json",
    visibility = [
        "//:__pkg__",
    ],
)

rules_js_tsconfig(
    name = "build-tsconfig",
    src = "tsconfig-build.json",
    deps = [
        ":tsconfig",
        "//:node_modules/@types/node",
    ],
)

rules_js_tsconfig(
    name = "test-tsconfig",
    src = "tsconfig-test.json",
    deps = [
        ":tsconfig",
        "//:node_modules/@types/jasmine",
        "//:node_modules/@types/node",
    ],
)

rules_js_tsconfig(
    name = "build-tsconfig-esm",
    src = "tsconfig-build-esm.json",
    deps = [
        ":tsconfig",
    ],
)

rules_js_tsconfig(
    name = "test-tsconfig-esm",
    src = "tsconfig-test-esm.json",
    deps = [
        ":build-tsconfig-esm",
        "//:node_modules/@types/jasmine",
        "//:node_modules/@types/node",
    ],
)

# Files required by e2e tests
copy_to_bin(
    name = "config-files",
    srcs = [
        "package.json",
    ],
)

# Detect if the build is running under --stamp
config_setting(
    name = "stamp",
    values = {"stamp": "true"},
)

# If set will replace dependency versions with tarballs for packages in this repo
bool_flag(
    name = "enable_package_json_tar_deps",
    build_setting_default = False,
)

config_setting(
    name = "package_json_use_tar_deps",
    flag_values = {
        ":enable_package_json_tar_deps": "true",
    },
)

# If set will replace dependency versions with snapshot repos for packages in this repo
bool_flag(
    name = "enable_snapshot_repo_deps",
    build_setting_default = False,
)

config_setting(
    name = "package_json_use_snapshot_repo_deps",
    flag_values = {
        ":enable_snapshot_repo_deps": "true",
    },
)

validate_ts_version_matching(
    module_lock_file = "MODULE.bazel.lock",
    package_json = "package.json",
)
