# Copyright (c) 2021-2026 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/ohos.gni")
import("//foundation/arkui/ace_engine/ace_config.gni")
import("//foundation/arkui/ace_engine/build/ace_lib.gni")
import("//foundation/arkui/ace_engine/interfaces/napi/kits/napi_lib.gni")

if (is_ohos_standard_system) {
  system_resource_hap_path =
      get_label_info("//base/global/system_resources/systemres:systemres_hap",
                     "target_out_dir") + "/SystemResources.hap"
  system_resource_path =
      get_label_info("//base/global/system_resources/systemres:systemres_hap",
                     "target_out_dir") + "/resources"
  system_resource_index_path =
      get_label_info("//base/global/system_resources/systemres:systemres_hap",
                     "target_gen_dir") + "/main_res/gen/resources.index"
  system_resource_value_path =
      get_label_info("//base/global/system_resources/systemres:systemres_hap",
                     "target_out_dir") + "/resources/entry/resources.txt"
} else {
  system_resource_hap_path =
      get_label_info("//base/global/system_resources/systemres:systemres_hap",
                     "target_gen_dir") + "/SystemResources.hap"
  system_resource_path =
      get_label_info("//base/global/system_resources/systemres:systemres_hap",
                     "target_gen_dir") + "/resources"
  system_resource_index_path =
      get_label_info("//base/global/system_resources/systemres:systemres_hap",
                     "target_gen_dir") + "/main_res/gen/resources.index"
  system_resource_value_path =
      get_label_info("//base/global/system_resources/systemres:systemres_hap",
                     "target_gen_dir") + "/resources/entry/resources.txt"
}

action("get_system_resource") {
  script = "get_system_resources.sh"
  deps = [ "//base/global/system_resources/systemres:systemres_hap($current_toolchain)" ]
  args = [
    rebase_path(system_resource_hap_path),
    rebase_path(system_resource_path),
  ]
  inputs = [ system_resource_hap_path ]
  outputs = [ system_resource_path ]
}

action("extra_system_resource") {
  script = "./dump/dump.py"
  deps = [ "//base/global/system_resources/systemres:main_res" ]
  args = [
    rebase_path(system_resource_index_path),
    rebase_path(system_resource_value_path),
  ]
  outputs = [ system_resource_value_path ]
}

# Triggered only when the SDK is compiled
if (is_ohos_standard_system) {
  import("sharedlib_config.gni")

  foreach(module, napi_modules) {
    ohos_copy("copy_napi_${module.prefix}libs") {
      deps = []
      sources = []
      foreach(shard_lib, module.shard_libs) {
        subsystem_name = shard_lib.subsystem_name
        part_name = shard_lib.part_name
        deps += [ shard_lib.label ]
        out_path = get_label_info(shard_lib.label, "root_out_dir")
        lib_names = []
        if (defined(shard_lib.lib_names)) {
          lib_names += shard_lib.lib_names
        } else {
          cur_lib = get_label_info(shard_lib.label, "name")
          lib_names += [ string_replace(cur_lib, "lib", "", 1) ]
        }
        foreach(lib_name, lib_names) {
          sources += [ "${out_path}/${subsystem_name}/${part_name}/lib${lib_name}${dylib_suffix}" ]
        }
      }

      # Set the output directory for all dynamic libraries.
      if (module.prefix == "") {
        module_source_dir = "${target_out_dir}/previewer/common/bin/module/"
      } else {
        module_source_dir =
            "${target_out_dir}/previewer/common/bin/module/${module.prefix}/"
      }
      outputs = [ "${module_source_dir}{{source_file_part}}" ]
      module_install_name = ""
    }
  }

  ohos_copy("copy_abc_library") {
    deps = [
      "$ace_root/frameworks/bridge/declarative_frontend:ark_modifier",
      "$ace_root/frameworks/bridge/declarative_frontend:ark_theme_control",
      "$ace_root/frameworks/bridge/declarative_frontend:shape",
      "$ace_root/frameworks/bridge/declarative_frontend:statemanagement",
      "$ace_root/frameworks/bridge/declarative_frontend:uicontext",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_node:x_node",
      ":copy_component_model_abc_library",
    ]
    out_path = get_label_info(
            "$ace_root/frameworks/bridge/declarative_frontend:ark_modifier",
            "root_out_dir")
    sources = [
      "${out_path}/arkui/ace_engine/modifier.abc",
      "${out_path}/arkui/ace_engine/node.abc",
      "${out_path}/arkui/ace_engine/shape.abc",
      "${out_path}/arkui/ace_engine/statemanagement.abc",
      "${out_path}/arkui/ace_engine/theme.abc",
      "${out_path}/arkui/ace_engine/uicontext.abc",
    ]

    outputs = [ target_out_dir +
                "/previewer/common/bin/module/arkui/{{source_file_part}}" ]
    module_source_dir = target_out_dir + "/previewer/common/bin/module/arkui"
    module_install_name = ""
  }

  ohos_copy("copy_component_model_abc_library") {
    deps = [
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:flowitem",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:counter",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:calendarpicker",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:gauge",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:checkbox",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:checkboxgroup",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:qrcode",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:rating",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:waterflow",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:columnsplit",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:rowsplit",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:menu",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:menuitem",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:menuitemgroup",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:sidebarcontainer",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:marquee",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:stepper",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:stepperitem",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:radio",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:slider",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:timepicker",
      "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:alphabetindexer",
    ]
    if (!ace_engine_feature_wearable) { 
      deps += [
        "$ace_root/frameworks/bridge/declarative_frontend/ark_component/components:folderstack",
      ]
    }
    out_path = get_label_info(
            "$ace_root/frameworks/bridge/declarative_frontend:ark_modifier",
            "root_out_dir")
    sources = [
      "${out_path}/arkui/ace_engine/arkflowitem.abc",
      "${out_path}/arkui/ace_engine/arkcounter.abc",
      "${out_path}/arkui/ace_engine/arkcalendarpicker.abc",
      "${out_path}/arkui/ace_engine/arkgauge.abc",
      "${out_path}/arkui/ace_engine/arkcheckbox.abc",
      "${out_path}/arkui/ace_engine/arkcheckboxgroup.abc",
      "${out_path}/arkui/ace_engine/arkqrcode.abc",
      "${out_path}/arkui/ace_engine/arkrating.abc",
      "${out_path}/arkui/ace_engine/arkwaterflow.abc",
      "${out_path}/arkui/ace_engine/arkcolumnsplit.abc",
      "${out_path}/arkui/ace_engine/arkrowsplit.abc",
      "${out_path}/arkui/ace_engine/arkmenu.abc",
      "${out_path}/arkui/ace_engine/arkmenuitem.abc",
      "${out_path}/arkui/ace_engine/arkmenuitemgroup.abc",
      "${out_path}/arkui/ace_engine/arksidebarcontainer.abc",
      "${out_path}/arkui/ace_engine/arkmarquee.abc",
      "${out_path}/arkui/ace_engine/arkstepper.abc",
      "${out_path}/arkui/ace_engine/arkstepperitem.abc",
      "${out_path}/arkui/ace_engine/arkradio.abc",
      "${out_path}/arkui/ace_engine/arkslider.abc",
      "${out_path}/arkui/ace_engine/arktimepicker.abc",
      "${out_path}/arkui/ace_engine/arkalphabetindexer.abc",
    ]
    if (!ace_engine_feature_wearable) { 
      sources += [
        "${out_path}/arkui/ace_engine/arkfolderstack.abc",
      ]
    }

    # Output path description: 
    # "previewer/common/bin/module" is equivalent to the basic path of globalThis.requireNapi().
    # globalThis.requireNapi("arkui.components").
    outputs = [ target_out_dir +
                "/previewer/common/bin/module/arkui/components/{{source_file_part}}" ]
    module_source_dir = target_out_dir + "/previewer/common/bin/module/arkui/components"
    module_install_name = ""
  }

  # All dynamic libraries to be copied to the path "/previewer/common/bin/",
  # which are related to the previewer, can be added to this script.
  ohos_copy("copy_preview_shared_library") {
    deps = []
    foreach(module, napi_modules) {
      deps += [ ":copy_napi_${module.prefix}libs" ]
    }

    # The dynamic library here is used for local testing of the previewer.
    if (enable_glfw_window) {
      deps += [ "$ace_root/adapter/preview/entrance/samples:previewer_demo" ]
    }

    sources = [ "//third_party/icu/ohos_icu4j/data/icudt74l.dat" ]
    if (ace_engine_feature_enable_upgrade_skia) {
      sources += [
        "//third_party/skia/m133/src/ports/skia_ohos/config/fontconfig.json",
        "//third_party/skia/m133/src/ports/skia_ohos/config/fontconfig_ohos.json",
      ]
    } else {
      sources += [
        "//third_party/skia/src/ports/skia_ohos/config/fontconfig.json",
        "//third_party/skia/src/ports/skia_ohos/config/fontconfig_ohos.json",
      ]
    }

    foreach(item, common_bin) {
      subsystem_name = item.subsystem_name
      part_name = item.part_name
      deps += [ item.label ]
      out_path = get_label_info(item.label, "root_out_dir")
      lib_names = []
      if (defined(item.lib_names)) {
        lib_names += item.lib_names
      } else {
        lib_names +=
            [ string_replace(get_label_info(item.label, "name"), "lib", "", 1) ]
      }
      foreach(lib_name, lib_names) {
        sources += [ "${out_path}/${subsystem_name}/${part_name}/lib${lib_name}${dylib_suffix}" ]
      }
    }

    # Set the output directory for all dynamic libraries.
    outputs = [ target_out_dir + "/previewer/common/bin/{{source_file_part}}" ]
    module_source_dir = target_out_dir + "/previewer/common/bin/"
    module_install_name = ""
  }

  if (use_mingw_win) {
    ohos_copy("copy_preview_curl_cacert") {
      sources = [ "//third_party/curl/cacert.pem" ]
      outputs = [ target_out_dir + "/previewer/resources/cacert.pem" ]
      module_source_dir = target_out_dir + "/previewer/resources"
      module_install_name = ""
    }
  }

  ohos_copy("copy_system_resource_standard") {
    deps = [
      ":extra_system_resource",
      ":get_system_resource",
    ]

    sources = [ system_resource_path ]

    outputs = [ target_out_dir + "/previewer/common/{{source_file_part}}" ]
    module_source_dir = target_out_dir + "/previewer/common"
    module_install_name = ""
  }
} else {
  ohos_copy("copy_system_resource") {
    if (use_mac) {
      sources = [ "//prebuilts/ace-toolkit/preview/rich/resources" ]
      outputs = [ target_out_dir + "/tv_resources/{{source_file_part}}" ]
      module_source_dir = target_out_dir + "/tv_resources/"
      module_install_name = ""
    } else {
      deps = [
        ":extra_system_resource",
        ":get_system_resource",
      ]

      sources = [ system_resource_path ]
      outputs = [ target_out_dir + "/tv_resources/{{source_file_part}}" ]
      module_source_dir = target_out_dir + "/tv_resources/"
      module_install_name = ""
    }
  }

  ohos_copy("copy_system_resource_wearable") {
    if (use_mac) {
      sources = [ "//prebuilts/ace-toolkit/preview/rich/resources" ]
      outputs = [ target_out_dir + "/wearable_resources/{{source_file_part}}" ]
      module_source_dir = target_out_dir + "/wearable_resources/"
      module_install_name = ""
    } else {
      deps = [
        ":extra_system_resource",
        ":get_system_resource",
      ]

      sources = [ system_resource_path ]
      outputs = [ target_out_dir + "/wearable_resources/{{source_file_part}}" ]
      module_source_dir = target_out_dir + "/wearable_resources/"
      module_install_name = ""
    }
  }
}