# Copyright (c) 2022-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/ohos.gni")
import("//foundation/arkui/ace_engine/ace_config.gni")

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

  ohos_source_set(target_name) {
    defines += invoker.defines
    cflags_cc += invoker.cflags_cc
    use_exceptions = true

    configs = [ "//foundation/appframework/ability/ability_runtime/cross_platform/interfaces/kits/native/appkit:appkit_native_config" ]

    include_dirs = [
      "include",
      "./entity_recognition/include",
      "./entity_recognition/date_time_recognition/include",
      "./entity_recognition/phone_number_recognition/include",
      "$ace_root",
      "$ace_root/frameworks",
      "$root_out_dir/arkui/framework",
      "//third_party/icu/icu4c/source",
      "//third_party/icu/icu4c/source/common",
      "//third_party/icu/icu4c/source/i18n",
      "//third_party/icu/icu4c/source/common/unicode",
      "//base/hiviewdfx/hilog/interfaces/native/kits/include",
      "//plugins/interfaces",
    ]

    sources = [
      "framework/character.cpp",
      "framework/error_util.cpp",
      "framework/i18n_break_iterator.cpp",
      "framework/i18n_calendar.cpp",
      "framework/i18n_normalizer.cpp",
      "framework/i18n_timezone.cpp",
      "framework/icu_data.cpp",
      "framework/index_util.cpp",
      "framework/locale_config.cpp",
      "framework/locale_data.cpp",
      "framework/locale_info.cpp",
      "framework/locale_matcher.cpp",
      "framework/locale_util.cpp",
      "framework/napi_utils.cpp",
      "framework/phone_number_format.cpp",
      "framework/plural_rules.cpp",
      "framework/utils.cpp",
      "i18n_system_addon.cpp",
      "i18n_addon.cpp",
    ]

    sources += [
      "framework/holiday_manager.cpp",
      "holiday_manager_addon.cpp",
    ]

    sources += [
      "framework/zone_offset_transition.cpp",
      "framework/zone_rules.cpp",
      "i18n_timezone_addon.cpp",
      "zone_offset_transition_addon.cpp",
      "zone_rules_addon.cpp",
    ]

    sources += [
      "entity_recognition/date_time_recognition/src/date_rule_init.cpp",
      "entity_recognition/date_time_recognition/src/date_time_filter.cpp",
      "entity_recognition/date_time_recognition/src/date_time_matched.cpp",
      "entity_recognition/date_time_recognition/src/date_time_rule.cpp",
      "entity_recognition/date_time_recognition/src/date_time_sequence.cpp",
      "entity_recognition/date_time_recognition/src/rules_engine.cpp",
      "entity_recognition/phone_number_recognition/src/border_rule.cpp",
      "entity_recognition/phone_number_recognition/src/code_rule.cpp",
      "entity_recognition/phone_number_recognition/src/find_rule.cpp",
      "entity_recognition/phone_number_recognition/src/negative_rule.cpp",
      "entity_recognition/phone_number_recognition/src/phone_number_matched.cpp",
      "entity_recognition/phone_number_recognition/src/phone_number_rule.cpp",
      "entity_recognition/phone_number_recognition/src/positive_rule.cpp",
      "entity_recognition/phone_number_recognition/src/regex_rule.cpp",
      "entity_recognition/src/entity_recognizer.cpp",
      "entity_recognizer_addon.cpp",
    ]

    # libphonnumber
    LIBPHONE_DIR = "//third_party/libphonenumber/cpp"

    sources += [
      "${LIBPHONE_DIR}/src/phonenumbers/alternate_format.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/asyoutypeformatter.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/base/strings/string_piece.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/default_logger.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/logger.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/metadata.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/phonemetadata.pb.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/phonenumber.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/phonenumber.pb.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/phonenumbermatch.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/phonenumbermatcher.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/phonenumberutil.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/regex_based_matcher.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/regexp_adapter_icu.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/regexp_cache.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/short_metadata.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/shortnumberinfo.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/string_byte_sink.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/stringutil.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/unicodestring.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/utf/rune.c",
      "${LIBPHONE_DIR}/src/phonenumbers/utf/unicodetext.cc",
      "${LIBPHONE_DIR}/src/phonenumbers/utf/unilib.cc",
    ]

    include_dirs += [
      "//third_party/libphonenumber/",
      "//third_party/libphonenumber/cpp/src",
      "//third_party/protobuf/src",
      "//third_party/protobuf/src/google",
      "//third_party/protobuf/src/google/protobuf",
    ]

    defines += [
      "I18N_PHONENUMBERS_USE_ALTERNATE_FORMATS",
      "I18N_PHONENUMBERS_USE_ICU_REGEXP",
      "HAVE_PTHREAD",
      "ARK_UI_X",
    ]

    deps = [
      "//plugins/interfaces/native:ace_plugin_util_${platform}",
      "//plugins/interfaces/native:plugin_log",
      "//plugins/libs/icu:icu_${target_os}",
      "//plugins/libs/napi:napi_${target_os}",
      "//third_party/protobuf:protobuf_lite_static",
      "//third_party/libxml2:libxml2",
    ]

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

    if (platform == "android") {
      deps += [ "android/java:i18n_plugin_android_jni" ]
      deps += [ "//foundation/arkui/ace_engine/adapter/android/build:intl" ]
      libs = [
        "android",
        "log",
      ]
    } else if (platform == "ios") {
      deps += [ "ios:i18n_plugin_ios" ]
      deps += [ "//foundation/arkui/ace_engine/adapter/ios/build:intl" ]
      cflags_cc += [ "-faligned-allocation" ]
      defines += [ "ARK_UI_X" ]
    }

    subsystem_name = "plugins"
    part_name = "i18n"
  }
}

foreach(item, ace_platforms) {
  plugin_i18n_static("i18n_static_" + item.name) {
    platform = item.name

    defines = []
    cflags_cc = []
    config = {
    }

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

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

    if (defined(config.cflags_cc)) {
      cflags_cc = config.cflags_cc
    }
  }
}