# Copyright (c) 2022-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/config/config.gni")
import("//build/ohos.gni")
import("//build_plugins/config/ios/ios_sdk.gni")
import("//foundation/arkui/ace_engine/ace_config.gni")
import("//third_party/icu/icu.gni")

# import(
#     "//foundation/arkui/ace_engine/frameworks/core/components_ng/components_plugin.gni")
import("//foundation/arkui/ace_engine/interfaces/napi/kits/napi_lib.gni")
import("//plugins/component/components.gni")
import("//plugins/plugin_lib.gni")
import("config.gni")

# combine framework and xcframework
ohos_combine_darwin_framework("libarkui_ios") {
  deps = [ ":arkui_ios" ]

  _export_framework_headers = [
    "$ace_root/adapter/ios/capability/bridge/BridgePlugin.h",
    "$ace_root/adapter/ios/capability/bridge/BridgePluginManager.h",
    "$ace_root/adapter/ios/capability/bridge/MethodData.h",
    "$ace_root/adapter/ios/capability/bridge/ResultValue.h",
    "$ace_root/adapter/ios/capability/bridge/TaskOption.h",
    "$ace_root/adapter/ios/capability/bridge/codec/BridgeArray.h",
    "$ace_root/adapter/ios/capability/platformview/IPlatformView.h",
    "$ace_root/adapter/ios/capability/platformview/PlatformViewFactory.h",
    "$ace_root/adapter/ios/stage/ability/AbilityLoader.h",
    "$ace_root/adapter/ios/stage/ability/StageViewController.h",
    "$ace_root/adapter/ios/stage/ability/StageApplication.h",
    "$ace_root/adapter/ios/entrance/plugin_lifecycle/IArkUIXPlugin.h",
    "$ace_root/adapter/ios/entrance/plugin_lifecycle/PluginContext.h",
    "$ace_root/adapter/ios/entrance/logIntercept/ILogger.h",
    "$ace_root/adapter/ios/entrance/WantParams.h",
  ]

  subsystem_name = "arkui"
  part_name = "arkui-x"
}

# libace target
# Clang runtime for iOS (provides ___chkstk_darwin used by MoltenVK).
#
# GN build config forbids calling exec_script() here (whitelist restriction),
# so we resolve the actual clang runtime directory by copying it at build
# time via an action target.
_xcode_dev = get_path_info(get_path_info(get_path_info(get_path_info(get_path_info(ios_device_sdk_path, "dir"), "dir"), "dir"), "dir"), "dir")

# Simulator links must use libclang_rt.iossim.a; device uses libclang_rt.ios.a (see collect_clang_rt_ios_a.sh).
_clang_rt_darwin_kind = "ios"
if (defined(use_ios_simulator) && use_ios_simulator) {
  _clang_rt_darwin_kind = "iossim"
}

action("clang_rt_ios_collect") {
  script = "//build_plugins/config/ios/collect_clang_rt_ios_a.sh"
  inputs = [ "//build_plugins/config/ios/collect_clang_rt_ios_a.sh" ]
  outputs = [ "${target_gen_dir}/libclang_rt.ios.a" ]
  args = [
    _xcode_dev,
    rebase_path("${target_gen_dir}/libclang_rt.ios.a", root_build_dir),
    _clang_rt_darwin_kind,
  ]
}

# Path to copied libclang_rt.ios.a for -force_load (so ___chkstk_darwin is always pulled in after MoltenVK).
_clang_rt_ios_out =
    get_label_info(":clang_rt_ios_collect", "target_gen_dir") +
    "/libclang_rt.ios.a"
_clang_rt_ios_force_load_path = rebase_path(_clang_rt_ios_out, root_build_dir)

ohos_shared_library("arkui_ios") {
  ldflags = [
    "-Wl",
    "-install_name",
    "@rpath/libarkui_ios.framework/libarkui_ios",
    "-Wl,-force_load," + _clang_rt_ios_force_load_path,
  ]

  deps = [
    "$ace_root/build:libace_static_ios",
    "$ace_root/interfaces/inner_api/ace_kit:ace_kit",
    "$ace_root/interfaces/native:ace_static_ndk",
    "//foundation/appframework:appframework_napis",
    "//foundation/arkui/ace_engine/frameworks/core/components_ng/pattern/menu:ace_core_components_menu_pattern_ng_ios",
    "//plugins/interfaces/native:ace_plugin_util_inner_ios",
  ]
  if (defined(ark_engine)) {
    deps += [
      "//arkcompiler/runtime_core/libpandabase:libarkbase_static",
      "//arkcompiler/runtime_core/libpandafile:libarkfile_static",
    ]

    if (runtime_mode != "release") {
      deps += [
        # for enable debugger
        "//arkcompiler/toolchain/inspector:ark_debugger_static",
        "//arkcompiler/toolchain/inspector:connectserver_debugger_static",
        "//arkcompiler/toolchain/tooling:libark_ecma_debugger_set",
      ]
    }
  }

  deps +=
      [ "//foundation/appframework/arkui/uicontent:ace_uicontent_interface" ]

  # Link after MoltenVK so ___chkstk_darwin is resolved (static lib order matters).
  deps += [ ":clang_rt_ios_collect" ]

  cflags_cc += [
    "-Wall",
    "-frtti",
  ]

  exported_symbols_list = "libarkui_ios.txt"
  part_name = "arkui-x"
  subsystem_name = "arkui"

  assert(icu_dat_name == "icudt74l",
      "arkui_ios exports are aligned to ICU 74; update libarkui_ios.txt when ICU version changes.")
}

# napi target
common_napi_modules = []
foreach(module, common_napi_libs) {
  module_path_names = []
  module_path_names = string_split(module, "/")
  prefix_dir = module_path_names[0]

  module_path = prefix_dir
  if (prefix_dir != module) {
    module_path = module_path_names[1]
  }

  module_name_ext = string_replace(module_path, "_", "")

  module_name = string_replace(module, "_", "")
  module_name = string_replace(module_name, "/", "_")
  lib_name = "lib" + module_name
  ohos_shared_library(module_name) {
    ldflags = [
      "-Wl",
      "-install_name",
      "@rpath/${lib_name}.framework/$lib_name",
    ]
    deps = [ "$ace_root/interfaces/napi/kits/${module_path}:${module_name_ext}_static_ios" ]
    deps += [ ":arkui_ios" ]
    exported_symbols_list = "empty_ios_symbol.txt"
    subsystem_name = "arkui"
    part_name = "arkui-x"
  }

  ohos_combine_darwin_framework(lib_name) {
    deps = [ ":$module_name" ]

    subsystem_name = "arkui"
    part_name = "arkui-x"
  }
  common_napi_modules += [ ":${lib_name}" ]
}

# plugin target
common_plugin_modules = []
foreach(module, common_plugin_libs) {
  plugin_name = module
  target_name_ext = string_replace(module, "_", "")
  target_name_ext = string_replace(target_name_ext, "/", "_")
  plugin_name_ext = string_replace(plugin_name, "/", "_")
  lib_name = "lib" + target_name_ext
  ohos_shared_library(target_name_ext) {
    ldflags = [
      "-Wl",
      "-install_name",
      "@rpath/${lib_name}.framework/$lib_name",
      "-Wl,-force_load," + _clang_rt_ios_force_load_path,
    ]
    deps = [ "//plugins/$module:${plugin_name_ext}_static_ios" ]
    deps += [ ":arkui_ios" ]
    deps += [ ":clang_rt_ios_collect" ]

    frameworks = [
      "AVFoundation.framework",
      "Foundation.framework",
      "UIKit.framework",
      "WebKit.framework",
    ]
    if (module == "i18n") {
      deps += [ ":intl" ]
    }
    if (module == "data/datasharepredicates") {
      exported_symbols_list = "data_datasharepredicates.txt"
    } else if (module == "intl") {
      exported_symbols_list = "intl.txt"
    } else {
      exported_symbols_list = "empty_ios_symbol.txt"
    }

    # relative_install_dir = "module"
    subsystem_name = "arkui"
    part_name = "plugins"
  }
  ohos_combine_darwin_framework(lib_name) {
    deps = [ ":$target_name_ext" ]

    subsystem_name = "arkui"
    part_name = "plugins"
  }
  common_plugin_modules += [ ":${lib_name}" ]
}

# components target
# component_plugin_modules = []
# foreach(module, component_plugins) {
#   sub_name = string_replace(module, "_", "")
#   module_name = "arkui_$sub_name"
#   lib_name = "lib" + module_name
#   ohos_shared_library(module_name) {
#     ldflags = [
#       "-Wl",
#       "-install_name",
#       "@rpath/${lib_name}.framework/$lib_name",
#     ]
#     deps = [ "$ace_root/frameworks/core/components_ng/pattern/${module}:components_plugins_${module}_source_ios" ]
#     deps += [ ":arkui_ios" ]
#     frameworks = [
#       "AVFoundation.framework",
#       "Foundation.framework",
#       "UIKit.framework",
#       "WebKit.framework",
#     ]

#     # relative_install_dir = "module"
#     subsystem_name = "arkui"
#     part_name = "arkui_components"
#   }
#   ohos_combine_darwin_framework(lib_name) {
#     deps = [ ":$module_name" ]

#     subsystem_name = "arkui"
#     part_name = "arkui_components"
#   }
#   component_plugin_modules += [ ":${lib_name}" ]
# }

components_modules = []

foreach(module, arkui_x_components) {
  sub_name = string_replace(module, "_", "")
  module_name = "$sub_name"
  lib_name = "lib" + module_name
  ohos_shared_library(module_name) {
    ldflags = [
      "-Wl",
      "-install_name",
      "@rpath/${lib_name}.framework/$lib_name",
    ]
    deps = [
      "//plugins/component/${module}:arkui_x_components_${module}_source_ios",
    ]
    deps += [ ":arkui_ios" ]
    exported_symbols_list = "empty_ios_symbol.txt"
    frameworks = [
      "AVFoundation.framework",
      "Foundation.framework",
      "UIKit.framework",
      "WebKit.framework",
    ]

    # relative_install_dir = "module"
    subsystem_name = "arkui"
    part_name = "arkui_components"
  }
  ohos_combine_darwin_framework(lib_name) {
    deps = [ ":$module_name" ]

    subsystem_name = "arkui"
    part_name = "arkui_components"
  }
  components_modules += [ ":${lib_name}" ]
}

# for arkui component modules
component_modules = [
  "dynamiclayout",
  "marquee",
  "stepper",
  "slider",
  "gauge",
  "checkbox",
  "rating",
  "waterflow",
  "counter",
  "linear_split",
  "side_bar",
  "radio",
  "data_panel",
  "hyperlink",
  "patternlock",
  "qrcode",
  "time_picker",
  "indexer",
  "calendar_picker",
  "symbol",
  "rich_editor",
  "search",
  "text_clock",
]
component_modules_libs = []

foreach(module, component_modules) {
  sub_name = string_replace(module, "_", "")
  module_name = "arkui_$sub_name"
  lib_name = "lib" + module_name
  ohos_shared_library(module_name) {
    ldflags = [
      "-Wl",
      "-install_name",
      "@rpath/${lib_name}.framework/$lib_name",
    ]
    deps = [ "$ace_root/frameworks/core/components_ng/pattern/${module}:ace_core_components_${module}_pattern_ng_ios" ]

    deps += [ libace_target ]

    part_name = ace_engine_part
    subsystem_name = ace_engine_subsystem
  }

  ohos_combine_darwin_framework(lib_name) {
    deps = [ ":$module_name" ]

    subsystem_name = ace_engine_subsystem
    part_name = ace_engine_part
  }
  component_modules_libs += [ ":${lib_name}" ]
}

# Install packages
group("ace_packages") {
  deps = [ ":libarkui_ios" ]
  deps += common_napi_modules
  deps += common_plugin_modules
  deps += components_modules
  if (defined(use_components_lib) && use_components_lib) {
    deps += component_plugin_modules
  }
  deps += component_modules_libs
}