load("@devinfra//bazel/api-golden:index_rjs.bzl", "api_golden_test_npm_package")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("//tools:defaults.bzl", "copy_to_bin", "jasmine_test", "npm_package", "ts_project")
load("//tools:ts_json_schema.bzl", "ts_json_schema")

licenses(["notice"])

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

npm_link_all_packages()

ts_json_schema(
    name = "application_schema",
    src = "src/builders/application/schema.json",
)

ts_json_schema(
    name = "dev-server_schema",
    src = "src/builders/dev-server/schema.json",
)

ts_json_schema(
    name = "extract_i18n_schema",
    src = "src/builders/extract-i18n/schema.json",
)

ts_json_schema(
    name = "ng_karma_schema",
    src = "src/builders/karma/schema.json",
)

ts_json_schema(
    name = "ng_packagr_schema",
    src = "src/builders/ng-packagr/schema.json",
)

copy_to_bin(
    name = "schemas",
    srcs = glob(["**/schema.json"]),
)

RUNTIME_ASSETS = glob(
    include = [
        "src/**/schema.json",
        "src/**/*.js",
        "src/**/*.mjs",
        "src/**/*.html",
    ],
) + [
    "builders.json",
    "package.json",
]

ts_project(
    name = "build",
    srcs = glob(
        include = [
            "src/**/*.ts",
        ],
        exclude = [
            "src/test-utils.ts",
            "src/**/*_spec.ts",
            "src/**/tests/**/*.ts",
            "src/testing/**/*.ts",
        ],
    ) + [
        "index.ts",
        "//packages/angular/build:src/builders/application/schema.ts",
        "//packages/angular/build:src/builders/dev-server/schema.ts",
        "//packages/angular/build:src/builders/extract-i18n/schema.ts",
        "//packages/angular/build:src/builders/karma/schema.ts",
        "//packages/angular/build:src/builders/ng-packagr/schema.ts",
    ],
    data = RUNTIME_ASSETS,
    # TODO: Fix strict_deps failure
    ignore_strict_deps = True,
    deps = [
        ":node_modules/@angular-devkit/architect",
        ":node_modules/@angular-devkit/core",
        ":node_modules/@angular/ssr",
        ":node_modules/@inquirer/confirm",
        ":node_modules/@vitejs/plugin-basic-ssl",
        ":node_modules/vite",
        "//:node_modules/@ampproject/remapping",
        "//:node_modules/@angular/common",
        "//:node_modules/@angular/compiler",
        "//:node_modules/@angular/compiler-cli",
        "//:node_modules/@angular/core",
        "//:node_modules/@angular/localize",
        "//:node_modules/@angular/platform-browser",
        "//:node_modules/@angular/platform-server",
        "//:node_modules/@angular/service-worker",
        "//:node_modules/@babel/core",
        "//:node_modules/@babel/helper-annotate-as-pure",
        "//:node_modules/@babel/helper-split-export-declaration",
        "//:node_modules/@babel/plugin-syntax-import-attributes",
        "//:node_modules/@types/babel__core",
        "//:node_modules/@types/karma",
        "//:node_modules/@types/less",
        "//:node_modules/@types/node",
        "//:node_modules/@types/picomatch",
        "//:node_modules/@types/semver",
        "//:node_modules/@types/watchpack",
        "//:node_modules/beasties",
        "//:node_modules/browserslist",
        "//:node_modules/esbuild",
        "//:node_modules/esbuild-wasm",
        "//:node_modules/fast-glob",
        "//:node_modules/https-proxy-agent",
        "//:node_modules/istanbul-lib-instrument",
        "//:node_modules/jsonc-parser",
        "//:node_modules/karma",
        "//:node_modules/less",
        "//:node_modules/listr2",
        "//:node_modules/lmdb",
        "//:node_modules/magic-string",
        "//:node_modules/mrmime",
        "//:node_modules/ng-packagr",
        "//:node_modules/parse5-html-rewriting-stream",
        "//:node_modules/picomatch",
        "//:node_modules/piscina",
        "//:node_modules/postcss",
        "//:node_modules/rollup",
        "//:node_modules/sass",
        "//:node_modules/semver",
        "//:node_modules/source-map-support",
        "//:node_modules/tslib",
        "//:node_modules/typescript",
        "//:node_modules/watchpack",
    ],
)

ts_project(
    name = "unit_test_lib",
    testonly = True,
    srcs = glob(
        include = ["src/**/*_spec.ts"],
        exclude = ["src/builders/**/tests/**"],
    ),
    # TODO: Fix strict_deps failure
    ignore_strict_deps = True,
    deps = [
        ":build",
        "//:node_modules/@angular/compiler-cli",
        "//:node_modules/@babel/core",
        "//:node_modules/@types/jasmine",
        "//:node_modules/prettier",
        "//:node_modules/typescript",
        "//packages/angular/build/private",
    ],
)

jasmine_test(
    name = "unit_tests",
    data = [":unit_test_lib"],
)

ts_project(
    name = "application_integration_test_lib",
    testonly = True,
    srcs = glob(include = ["src/builders/application/tests/**/*.ts"]),
    # TODO: Fix strict_deps failure
    ignore_strict_deps = True,
    deps = [
        ":build",
        "//packages/angular/build/private",
        "//modules/testing/builder",
        ":node_modules/@angular-devkit/architect",

        # Base dependencies for the application in hello-world-app.
        "//:node_modules/@angular/common",
        "//:node_modules/@angular/compiler",
        "//:node_modules/@angular/compiler-cli",
        "//:node_modules/@angular/core",
        "//:node_modules/@angular/platform-browser",
        "//:node_modules/@angular/platform-browser-dynamic",
        "//:node_modules/@angular/router",
        "//:node_modules/rxjs",
        "//:node_modules/tslib",
        "//:node_modules/typescript",
        "//:node_modules/zone.js",
        "//:node_modules/buffer",
    ],
)

ts_project(
    name = "dev-server_integration_test_lib",
    testonly = True,
    srcs = glob(include = ["src/builders/dev-server/tests/**/*.ts"]),
    # TODO: Fix strict_deps failure
    ignore_strict_deps = True,
    deps = [
        ":build",
        "//packages/angular/build/private",
        "//modules/testing/builder",
        ":node_modules/@angular-devkit/architect",

        # dev server only test deps
        "//:node_modules/@types/http-proxy",
        "//:node_modules/http-proxy",
        "//:node_modules/puppeteer",

        # Base dependencies for the application in hello-world-app.
        "//:node_modules/@angular/common",
        "//:node_modules/@angular/compiler",
        "//:node_modules/@angular/compiler-cli",
        "//:node_modules/@angular/core",
        "//:node_modules/@angular/platform-browser",
        "//:node_modules/@angular/platform-browser-dynamic",
        "//:node_modules/@angular/router",
        "//:node_modules/ng-packagr",
        "//:node_modules/rxjs",
        "//:node_modules/tslib",
        "//:node_modules/typescript",
        "//:node_modules/zone.js",
        "//:node_modules/buffer",
    ],
)

ts_project(
    name = "karma_integration_test_lib",
    testonly = True,
    srcs = glob(include = ["src/builders/karma/tests/**/*.ts"]),
    # TODO: Fix strict_deps failure
    ignore_strict_deps = True,
    deps = [
        ":build",
        "//packages/angular/build/private",
        "//modules/testing/builder",
        ":node_modules/@angular-devkit/architect",

        # karma specific test deps
        "//:node_modules/karma-chrome-launcher",
        "//:node_modules/karma-coverage",
        "//:node_modules/karma-jasmine",
        "//:node_modules/karma-jasmine-html-reporter",
        "//:node_modules/puppeteer",

        # Base dependencies for the karma in hello-world-app.
        "//:node_modules/@angular/common",
        "//:node_modules/@angular/compiler",
        "//:node_modules/@angular/compiler-cli",
        "//:node_modules/@angular/core",
        "//:node_modules/@angular/platform-browser",
        "//:node_modules/@angular/platform-browser-dynamic",
        "//:node_modules/@angular/router",
        "//:node_modules/rxjs",
        "//:node_modules/tslib",
        "//:node_modules/typescript",
        "//:node_modules/zone.js",
        "//:node_modules/buffer",
    ],
)

jasmine_test(
    name = "application_integration_tests",
    size = "large",
    data = [":application_integration_test_lib"],
    flaky = True,
    shard_count = 10,
)

jasmine_test(
    name = "dev-server_integration_tests",
    size = "large",
    data = [":dev-server_integration_test_lib"],
    flaky = True,
    shard_count = 10,
)

jasmine_test(
    name = "karma_integration_tests",
    size = "large",
    data = [":karma_integration_test_lib"],
    env = {
        # TODO: Replace Puppeteer downloaded browsers with Bazel-managed browsers,
        # or standardize to avoid complex configuration like this!
        "PUPPETEER_DOWNLOAD_PATH": "../../../node_modules/puppeteer/downloads",
    },
    flaky = True,
    shard_count = 10,
)

genrule(
    name = "license",
    srcs = ["//:LICENSE"],
    outs = ["LICENSE"],
    cmd = "cp $(execpath //:LICENSE) $@",
)

npm_package(
    name = "pkg",
    pkg_deps = [
        "//packages/angular_devkit/architect:package.json",
    ],
    stamp_files = [
        "src/utils/version.js",
        "src/tools/esbuild/utils.js",
        "src/utils/normalize-cache.js",
    ],
    tags = ["release-package"],
    deps = RUNTIME_ASSETS + [
        ":README.md",
        ":build",
        ":license",
        "//packages/angular/build/private",
    ],
)

api_golden_test_npm_package(
    name = "api",
    data = [
        ":npm_package",
        "//goldens:public-api",
    ],
    golden_dir = "goldens/public-api/angular/build",
    npm_package = "packages/angular/build/npm_package",
)
