# Copyright (c) 2024 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("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni")
import("//foundation/arkui/ace_engine/ace_config.gni")
import("//foundation/arkui/ace_engine/build/ace_gen_obj.gni")

arkui_x_components = [ "platformview" ]

template("build_arkui_x_component") {
  forward_variables_from(invoker, "*")

  component_name = target_name

  template("build_arkui_xcomponent_impl") {
    forward_variables_from(invoker, "*")

    platform = target_name

    es2abc_gen_abc("gen_${js_source}_abc_$platform") {
      src_js = rebase_path("${js_source}.js")
      dst_file = rebase_path(target_out_dir + "/${platform}/${js_source}.abc")
      in_puts = [ "${js_source}.js" ]
      out_puts = [ target_out_dir + "/${platform}/${js_source}.abc" ]
      extra_args = [ "--module" ]
    }

    gen_obj("${js_source}_js_$platform") {
      input = "${js_source}.js"
      if (target_os == "ios") {
        output = target_out_dir + "/${platform}/${js_source}.c"
      } else {
        output = target_out_dir + "/${platform}/${js_source}.o"
      }
      snapshot_dep = []
    }

    gen_obj("${js_source}_abc_$platform") {
      input = get_label_info(":gen_${js_source}_abc", "target_out_dir") +
              "/${platform}/${js_source}.abc"
      if (target_os == "ios") {
        output = target_out_dir + "/${platform}/${js_source}_abc.c"
      } else {
        output = target_out_dir + "/${platform}/${js_source}_abc.o"
      }
      snapshot_dep = [ ":gen_${js_source}_abc_$platform" ]
    }

    ohos_source_set("arkui_x_components_${component_name}_${platform}") {
      defines += invoker.defines

      sources = []
      sources += invoker.sources

      configs = []
      configs += [ "$ace_root:ace_config" ]

      if (defined(invoker.configs)) {
        configs += invoker.configs
      }

      deps = []
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }

      if (defined(invoker.public_configs)) {
        public_configs = []
        public_configs += invoker.public_configs
      }

      if (defined(invoker.cflags_cc)) {
        cflags_cc = []
        cflags_cc += invoker.cflags_cc
      }

      deps += [ "//plugins/libs/napi:napi_$platform" ]

      deps += [
        ":gen_obj_src_${js_source}_abc_$platform",
        ":gen_obj_src_${js_source}_js_$platform",
      ]

      if (defined(config.libace_target)) {
        deps += [ config.libace_target ]
      }
    }
  }

  foreach(item, ace_platforms) {
    build_arkui_xcomponent_impl(item.name) {
      platform = item.name
      defines = []

      if (defined(item.config)) {
        config = item.config
      } else {
        config = {
        }
      }

      if (defined(config.defines)) {
        defines += config.defines
      }

      if (defined(config.cflags_cc)) {
        cflags_cc = config.cflags_cc
      } else {
        cflags_cc = []
      }
    }
  }
}