"""Rules/toolchains for angular_cli with Bazel."""

module(
    name = "angular_cli",
)

bazel_dep(name = "platforms", version = "1.0.0")
bazel_dep(name = "yq.bzl", version = "0.3.5")
bazel_dep(name = "rules_nodejs", version = "6.7.3")
bazel_dep(name = "aspect_rules_js", version = "3.0.3")
bazel_dep(name = "aspect_rules_ts", version = "3.8.7")
bazel_dep(name = "rules_pkg", version = "1.2.0")
bazel_dep(name = "rules_cc", version = "0.2.17")
bazel_dep(name = "jq.bzl", version = "0.6.1")
bazel_dep(name = "bazel_lib", version = "3.2.2")
bazel_dep(name = "bazel_skylib", version = "1.9.0")
bazel_dep(name = "aspect_rules_esbuild", version = "0.25.1")
bazel_dep(name = "aspect_rules_jasmine", version = "2.0.4")
bazel_dep(name = "rules_angular")
git_override(
    module_name = "rules_angular",
    commit = "19a4a8fb4d6f035b5506ca21bbbd309ab5f5e729",
    remote = "https://github.com/angular/rules_angular.git",
)

bazel_dep(name = "devinfra")
git_override(
    module_name = "devinfra",
    commit = "55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc",
    remote = "https://github.com/angular/dev-infra.git",
)

bazel_dep(name = "rules_sass")
git_override(
    module_name = "rules_sass",
    commit = "13918bec49cd183a591e3781d1d08044b4aa9f61",
    remote = "https://github.com/angular/rules_sass.git",
)

bazel_dep(name = "rules_browsers")
git_override(
    module_name = "rules_browsers",
    commit = "46029652792eac7bb51b6abd04503af296db70ea",
    remote = "https://github.com/angular/rules_browsers.git",
)

node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")
node.toolchain(
    node_repositories = {
        "22.22.2-darwin_arm64": ("node-v22.22.2-darwin-arm64.tar.gz", "node-v22.22.2-darwin-arm64", "db4b275b83736df67533529a18cc55de2549a8329ace6c7bcc68f8d22d3c9000"),
        "22.22.2-darwin_amd64": ("node-v22.22.2-darwin-x64.tar.gz", "node-v22.22.2-darwin-x64", "12a6abb9c2902cf48a21120da13f87fde1ed1b71a13330712949e8db818708ba"),
        "22.22.2-linux_arm64": ("node-v22.22.2-linux-arm64.tar.xz", "node-v22.22.2-linux-arm64", "e9e1930fd321a470e29bb68f30318bf58e3ecb4acb4f1533fb19c58328a091fe"),
        "22.22.2-linux_ppc64le": ("node-v22.22.2-linux-ppc64le.tar.xz", "node-v22.22.2-linux-ppc64le", "14045b5a5030d35ca0030fb7e870bd11a651eb9b57323ebc0021e8d78ac6bac9"),
        "22.22.2-linux_s390x": ("node-v22.22.2-linux-s390x.tar.xz", "node-v22.22.2-linux-s390x", "9e4a07c291b8949289c6ea8ee61b1d14666a4810feae776a8d1eb1f57e03a2fb"),
        "22.22.2-linux_amd64": ("node-v22.22.2-linux-x64.tar.xz", "node-v22.22.2-linux-x64", "88fd1ce767091fd8d4a99fdb2356e98c819f93f3b1f8663853a2dee9b438068a"),
        "22.22.2-windows_amd64": ("node-v22.22.2-win-x64.zip", "node-v22.22.2-win-x64", "7c93e9d92bf68c07182b471aa187e35ee6cd08ef0f24ab060dfff605fcc1c57c"),
    },
    node_version = "22.22.2",
)
use_repo(
    node,
    "nodejs_darwin_amd64",
    "nodejs_darwin_arm64",
    "nodejs_linux_amd64",
    "nodejs_linux_arm64",
    "nodejs_toolchains",
    "nodejs_windows_amd64",
)

node_dev = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node", dev_dependency = True)

# Node.js 20
node_dev.toolchain(
    name = "node20",
    node_version = "20.19.0",
)

# Node.js 22
node_dev.toolchain(
    name = "node22",
    node_version = "22.12.0",
)

# Node.js 24
node_dev.toolchain(
    name = "node24",
    node_version = "24.0.0",
)
use_repo(
    node_dev,
    "node20_darwin_amd64",
    "node20_darwin_arm64",
    "node20_linux_amd64",
    "node20_linux_arm64",
    "node20_toolchains",
    "node20_windows_amd64",
    "node22_darwin_amd64",
    "node22_darwin_arm64",
    "node22_linux_amd64",
    "node22_linux_arm64",
    "node22_toolchains",
    "node22_windows_amd64",
    "node24_darwin_amd64",
    "node24_darwin_arm64",
    "node24_linux_amd64",
    "node24_linux_arm64",
    "node24_toolchains",
    "node24_windows_amd64",
)

pnpm = use_extension("@aspect_rules_js//npm:extensions.bzl", "pnpm")
pnpm.pnpm(
    name = "pnpm",
    pnpm_version = "10.33.0",
    pnpm_version_integrity = "sha512-EFaLtKavtYyes2MNqQzJUWQXq+vT+rvmc58K55VyjaFJHp21pUTHatjrdXD1xLs9bGN7LLQb/c20f6gjyGSTGQ==",
)
use_repo(pnpm, "pnpm")

npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm")
npm.npm_translate_lock(
    name = "npm",
    custom_postinstalls = {
        # TODO: Standardize browser management for `rules_js`
        "webdriver-manager": "node ./bin/webdriver-manager update --standalone false --gecko false --versions.chrome 106.0.5249.21",
    },
    data = [
        "//:package.json",
        "//:pnpm-workspace.yaml",
        "//modules/testing/builder:package.json",
        "//packages/angular/build:package.json",
        "//packages/angular/cli:package.json",
        "//packages/angular/create/package.json",
        "//packages/angular/pwa:package.json",
        "//packages/angular/ssr:package.json",
        "//packages/angular_devkit/architect:package.json",
        "//packages/angular_devkit/architect_cli:package.json",
        "//packages/angular_devkit/build_angular:package.json",
        "//packages/angular_devkit/build_webpack:package.json",
        "//packages/angular_devkit/core:package.json",
        "//packages/angular_devkit/schematics:package.json",
        "//packages/angular_devkit/schematics_cli:package.json",
        "//packages/ngtools/webpack:package.json",
        "//packages/schematics/angular:package.json",
        "//tests:package.json",
    ],
    lifecycle_hooks_envs = {
        # TODO: Standardize browser management for `rules_js`
        "puppeteer": ["PUPPETEER_DOWNLOAD_PATH=./downloads"],
    },
    lifecycle_hooks_execution_requirements = {
        # Needed for downloading chromedriver.
        # Also `update-config` of webdriver manager would store an absolute path;
        # which would then break execution.
        "webdriver-manager": ["local"],
    },
    npmrc = "//:.npmrc",
    pnpm_lock = "//:pnpm-lock.yaml",
)

# This is needed as by default `.md` files are excluded from the npm package.
# But @angular/core includes best-practices.md file.
# See: https://github.com/aspect-build/rules_js/blob/786a74a158dd36ed073188b0e506c423cd05501a/npm/private/exclude_package_contents_presets.bzl#L29
npm.npm_exclude_package_contents(
    package = "@angular/core",
    presets = [],
)
use_repo(npm, "npm")

rules_ts_ext = use_extension("@aspect_rules_ts//ts:extensions.bzl", "ext")
rules_ts_ext.deps(
    name = "angular_cli_npm_typescript",
    # Obtained by: npm info typescript@5.9.3 dist.integrity
    ts_integrity = "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
    ts_version = "5.9.3",
)
use_repo(rules_ts_ext, **{"npm_typescript": "angular_cli_npm_typescript"})

rules_angular = use_extension("@rules_angular//setup:extensions.bzl", "rules_angular")
rules_angular.setup(
    name = "components_rules_angular_configurable_deps",
    angular_compiler_cli = "//:node_modules/@angular/compiler-cli",
    typescript = "//:node_modules/typescript",
)
use_repo(rules_angular, **{"rules_angular_configurable_deps": "components_rules_angular_configurable_deps"})

register_toolchains(
    "@devinfra//bazel/git-toolchain:git_linux_toolchain",
    "@devinfra//bazel/git-toolchain:git_macos_x86_toolchain",
    "@devinfra//bazel/git-toolchain:git_macos_arm64_toolchain",
    "@devinfra//bazel/git-toolchain:git_windows_toolchain",
    "//tools/toolchains:dummy_cc_windows_no_exec_toolchain",
)
