# Copyright (c) 2025 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("//base/inputmethod/imf/inputmethod.gni")
import("//build/config/components/ets_frontend/ets2abc_config.gni")
import("//build/ohos.gni")
import("//build/ohos/taihe_idl/taihe.gni")

subsystem_name = "inputmethod"
part_name = "imf"
taihe_generated_file_path =
    "$taihe_file_path/out/$subsystem_name/$part_name/inputMethod"

copy_taihe_idl("copy_taihe_deps") {
  sources = [ "idl/ohos.InputMethodSubtype.taihe" ]
}

copy_taihe_idl("copy_panel_taihe") {
  sources = [ "idl/ohos.inputMethod.Panel.taihe" ]
}

copy_taihe_idl("inputMethod_taihe") {
  sources = [ "idl/ohos.inputMethod.taihe" ]
  deps = [
    ":copy_panel_taihe",
    ":copy_taihe_deps",
  ]
}

ohos_taihe("run_taihe") {
  taihe_generated_file_path = "$taihe_generated_file_path"
  deps = [ ":inputMethod_taihe" ]
  outputs = [
    "$taihe_generated_file_path/src/ohos.inputMethod.ani.cpp",
    "$taihe_generated_file_path/src/ohos.inputMethod.abi.c",
    "$taihe_generated_file_path/src/ohos.inputMethod.Panel.ani.cpp",
    "$taihe_generated_file_path/src/ohos.inputMethod.Panel.abi.c",
    "$taihe_generated_file_path/src/ohos.InputMethodSubtype.ani.cpp",
    "$taihe_generated_file_path/src/ohos.InputMethodSubtype.abi.c",
  ]
}

taihe_shared_library("inputmethod_taihe_native") {
  branch_protector_ret = "pac_ret"
  sanitize = {
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    debug = false
    integer_overflow = true
    ubsan = true
  }
  cflags_cc = [
    "-fvisibility=hidden",
    "-fvisibility-inlines-hidden",
    "-fdata-sections",
    "-ffunction-sections",
    "-Os",
  ]
  taihe_generated_file_path = "$taihe_generated_file_path"
  subsystem_name = "$subsystem_name"
  part_name = "$part_name"
  include_dirs = [
    "include",
    "${inputmethod_path}/frameworks/ets/taihe/include",
    "${inputmethod_path}/frameworks/ets/taihe/inputMethod/include",
    "${inputmethod_path}/frameworks/ets/taihe/inputMethodEngine/include",
    "${inputmethod_path}/frameworks/js/napi/inputmethodability",
    "${inputmethod_path}/frameworks/js/napi/inputmethodclient",
    "${inputmethod_path}/frameworks/native/inputmethod_controller/include",
  ]
  sources = get_target_outputs(":run_taihe")
  sources += [
    "${inputmethod_path}/frameworks/js/napi/inputmethodclient/js_utils.cpp",
    "${inputmethod_path}/frameworks/native/inputmethod_controller/src/input_method_utils.cpp",
    "src/ani_constructor.cpp",
    "src/ani_message_handler.cpp",
    "src/input_method_controller_impl.cpp",
    "src/input_method_event_listener.cpp",
    "src/input_method_impl.cpp",
    "src/input_method_setting_impl.cpp",
    "src/input_method_text_changed_listener.cpp",
    "src/ohos.inputMethod.impl.cpp",
  ]
  deps = [
    ":run_taihe",
    "${inputmethod_path}/frameworks/js/napi/common:inputmethod_js_common",
    "${inputmethod_path}/interfaces/inner_api/inputmethod_ability:inputmethod_ability",
    "${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client",
  ]
  external_deps = [
    "ability_runtime:abilitykit_native",
    "ability_runtime:napi_common",
    "ace_engine:ace_uicontent",
    "bundle_framework:bms_ani_common",
    "icu:shared_icui18n",
    "icu:shared_icuuc",
    "ffrt:libffrt",
    "ipc:ipc_single",
    "napi:ace_napi",
    "node:node_header_notice",
    "samgr:samgr_proxy",
  ]
  shlib_type = "ani"
}

generate_static_abc("inputmethod_abc") {
  base_url = "$taihe_generated_file_path"
  files = [ "$taihe_generated_file_path/@ohos.inputMethod.ets" ]
  is_boot_abc = "True"
  device_dst_file = "/system/framework/inputmethod_abc.abc"
  dependencies = [ ":run_taihe" ]
}

ohos_prebuilt_etc("inputmethod_etc") {
  source = "$target_out_dir/inputmethod_abc.abc"
  module_install_dir = "framework"
  part_name = "$part_name"
  subsystem_name = "$subsystem_name"
  deps = [ ":inputmethod_abc" ]
}

generate_static_abc("inputmethod_panel_abc") {
  base_url = "$taihe_generated_file_path"
  files = [ "$taihe_generated_file_path/@ohos.inputMethod.Panel.ets" ]
  is_boot_abc = "True"
  device_dst_file = "/system/framework/inputmethod_panel_abc.abc"
  dependencies = [ ":run_taihe" ]
}

ohos_prebuilt_etc("inputmethod_panel_etc") {
  source = "$target_out_dir/inputmethod_panel_abc.abc"
  module_install_dir = "framework"
  part_name = "$part_name"
  subsystem_name = "$subsystem_name"
  deps = [ ":inputmethod_panel_abc" ]
}

generate_static_abc("inputmethod_subtype_abc") {
  base_url = "$taihe_generated_file_path"
  files = [ "$taihe_generated_file_path/@ohos.InputMethodSubtype.ets" ]
  is_boot_abc = "True"
  device_dst_file = "/system/framework/inputmethod_subtype_abc.abc"
  dependencies = [ ":run_taihe" ]
}

ohos_prebuilt_etc("inputmethod_subtype_etc") {
  source = "$target_out_dir/inputmethod_subtype_abc.abc"
  module_install_dir = "framework"
  part_name = "$part_name"
  subsystem_name = "$subsystem_name"
  deps = [ ":inputmethod_subtype_abc" ]
}

group("inputmethod_taihe") {
  deps = [
    ":inputmethod_etc",
    ":inputmethod_panel_etc",
    ":inputmethod_subtype_etc",
    ":inputmethod_taihe_native",
  ]
}