# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/config/components/ets_frontend/es2abc_config.gni")
import("//build/ohos.gni")
import("//foundation/arkui/ace_engine/ace_config.gni")
import("//foundation/arkui/ace_engine/build/ace_gen_obj.gni")

prebuilt_js_api_mock_path =
    get_label_info(":gen_snapshot_jsmock", "target_out_dir") +
    "/dist/jsMockSystemPlugin.js"

mock_root = "."

action("gen_node_modules") {
  script = "./prebuild_env.sh"
  args = []
  inputs = [
    "package.json",
    "package-lock.json",
  ]
  outputs = [ "$root_out_dir/automock/node_modules" ]
}

action("gen_snapshot_jsmock") {
  script = "$mock_root/js_framework_build.sh"
  js_framework_mock_generate = "$mock_root/mock-generate"
  is_mac = "false"
  is_preview_engine = "false"
  if (use_mingw_win || use_mac || use_linux) {
    is_preview_engine = "true"
  }
  use_mac = host_os == "mac"

  node_modules = "//prebuilts/build-tools/common/js-framework/node_modules"
  nodejs_path = "//prebuilts/build-tools/common/nodejs/current"
  if (use_mac) {
    is_mac = "true"
  } else {
    is_mac = "false"
  }

  buildfile_jsmock = "$mock_root/build_jsmock_system_plugin.js"

  package_file = "$mock_root/mock-generate/package.json"
  tsconfig = "$mock_root/mock-generate/tsconfig.json"
  eslint = "$mock_root/mock-generate/.eslintrc"
  babel = "$mock_root/mock-generate/.babelrc"
  api_input_path = "//interface/sdk-js/api"

  args = [
    rebase_path(buildfile_jsmock, root_build_dir),
    rebase_path(nodejs_path, root_build_dir),
    rebase_path(node_modules, root_build_dir),
    rebase_path(package_file, root_build_dir),
    rebase_path(tsconfig, root_build_dir),
    rebase_path(eslint, root_build_dir),
    rebase_path(target_out_dir, root_build_dir),
    is_mac,
    rebase_path("//prebuilts", root_build_dir),
    rebase_path(api_input_path),
    rebase_path(js_framework_mock_generate, root_build_dir),
    is_preview_engine,
    rebase_path(babel, root_build_dir),
  ]

  inputs = [
    # config of compiler
    "./mock-generate/.eslintrc",
    "build_jsmock_system_plugin.js",
    "./mock-generate/tsconfig.json",
  ]
  outputs = [ prebuilt_js_api_mock_path ]
}

es2abc_gen_abc("gen_abc_js_mock") {
  js_mock_path = get_label_info(":gen_snapshot_jsmock", "target_out_dir") +
                 "/dist/jsMockSystemPlugin.js"
  src_js = rebase_path(js_mock_path)
  dst_file = rebase_path(target_out_dir + "/jsMockSystemPlugin.abc")
  in_puts = [ js_mock_path ]
  out_puts = [ target_out_dir + "/jsMockSystemPlugin.abc" ]
  extra_dependencies = [ ":gen_snapshot_jsmock" ]
}

gen_obj("js_mock_abc") {
  input = get_label_info(":gen_abc_js_mock", "target_out_dir") +
          "/jsMockSystemPlugin.abc"

  if (is_mac || is_mingw || is_linux) {
    output = target_out_dir + "/js_mock_abc.c"
  } else {
    output = target_out_dir + "/js_mock_abc.o"
  }

  snapshot_dep = [ ":gen_abc_js_mock" ]
}