import("//Meta/gn/build/compiled_action.gni")
import("//Meta/gn/build/download_cache.gni")
import("//Meta/gn/build/download_file.gni")
import("//Meta/gn/build/extract_archive_contents.gni")
import("//Userland/Libraries/LibUnicode/enable_unicode_download.gni")

locale_cache = cache_path + "CLDR/"

if (enable_unicode_database_download) {
  download_file("locale_database_download") {
    version = "43.1.0"
    url = "https://github.com/unicode-org/cldr-json/releases/download/" +
          version + "/cldr-" + version + "-json-modern.zip"
    cache = locale_cache
    output = "cldr.zip"
    version_file = "version.txt"
  }

  extract_archive_contents("locale_database_files") {
    deps = [ ":locale_database_download" ]
    archive = get_target_outputs(":locale_database_download")
    directory = locale_cache
    paths = [
      "cldr-bcp47",
      "cldr-core",
      "cldr-dates-modern",
      "cldr-localenames-modern",
      "cldr-misc-modern",
      "cldr-numbers-modern",
      "cldr-units-modern",
    ]
  }

  compiled_action("generate_datetime_format_sources") {
    tool =
        "//Meta/Lagom/Tools/CodeGenerators/LibLocale:GenerateDateTimeFormatData"
    deps = [ ":locale_database_files" ]
    outputs = [
      "$target_gen_dir/DateTimeFormatData.h",
      "$target_gen_dir/DateTimeFormatData.cpp",
    ]
    args = [
      "-h",
      rebase_path(outputs[0], root_build_dir),
      "-c",
      rebase_path(outputs[1], root_build_dir),
      "-r",
      rebase_path(locale_cache + "cldr-core", root_build_dir),
      "-d",
      rebase_path(locale_cache + "cldr-dates-modern", root_build_dir),
    ]
  }

  compiled_action("generate_locale_sources") {
    tool = "//Meta/Lagom/Tools/CodeGenerators/LibLocale:GenerateLocaleData"
    deps = [ ":locale_database_files" ]
    outputs = [
      "$target_gen_dir/LocaleData.h",
      "$target_gen_dir/LocaleData.cpp",
    ]
    args = [
      "-h",
      rebase_path(outputs[0], root_build_dir),
      "-c",
      rebase_path(outputs[1], root_build_dir),
      "-b",
      rebase_path(locale_cache + "cldr-bcp47", root_build_dir),
      "-r",
      rebase_path(locale_cache + "cldr-core", root_build_dir),
      "-l",
      rebase_path(locale_cache + "cldr-localenames-modern", root_build_dir),
      "-m",
      rebase_path(locale_cache + "cldr-misc-modern", root_build_dir),
      "-n",
      rebase_path(locale_cache + "cldr-numbers-modern", root_build_dir),
      "-d",
      rebase_path(locale_cache + "cldr-dates-modern", root_build_dir),
    ]
  }

  compiled_action("generate_number_format_sources") {
    tool =
        "//Meta/Lagom/Tools/CodeGenerators/LibLocale:GenerateNumberFormatData"
    deps = [ ":locale_database_files" ]
    outputs = [
      "$target_gen_dir/NumberFormatData.h",
      "$target_gen_dir/NumberFormatData.cpp",
    ]
    args = [
      "-h",
      rebase_path(outputs[0], root_build_dir),
      "-c",
      rebase_path(outputs[1], root_build_dir),
      "-r",
      rebase_path(locale_cache + "cldr-core", root_build_dir),
      "-n",
      rebase_path(locale_cache + "cldr-numbers-modern", root_build_dir),
      "-u",
      rebase_path(locale_cache + "cldr-units-modern", root_build_dir),
    ]
  }

  compiled_action("generate_plural_rules_sources") {
    tool = "//Meta/Lagom/Tools/CodeGenerators/LibLocale:GeneratePluralRulesData"
    deps = [ ":locale_database_files" ]
    outputs = [
      "$target_gen_dir/PluralRulesData.h",
      "$target_gen_dir/PluralRulesData.cpp",
    ]
    args = [
      "-h",
      rebase_path(outputs[0], root_build_dir),
      "-c",
      rebase_path(outputs[1], root_build_dir),
      "-r",
      rebase_path(locale_cache + "cldr-core", root_build_dir),
      "-l",
      rebase_path(locale_cache + "cldr-localenames-modern", root_build_dir),
    ]
  }

  compiled_action("generate_relative_time_format_sources") {
    tool = "//Meta/Lagom/Tools/CodeGenerators/LibLocale:GenerateRelativeTimeFormatData"
    deps = [ ":locale_database_files" ]
    outputs = [
      "$target_gen_dir/RelativeTimeFormatData.h",
      "$target_gen_dir/RelativeTimeFormatData.cpp",
    ]
    args = [
      "-h",
      rebase_path(outputs[0], root_build_dir),
      "-c",
      rebase_path(outputs[1], root_build_dir),
      "-d",
      rebase_path(locale_cache + "cldr-dates-modern", root_build_dir),
    ]
  }

  # FIXME: Add optimization for serenity to use these as a shlib
  source_set("locale_data_sources") {
    include_dirs = [
      "//Userland/Libraries",
      "$target_gen_dir/..",
    ]
    cflags_cc = [
      "-g0",
      "-Os",
      "-Wno-parentheses-equality",
    ]
    deps = [
      ":generate_datetime_format_sources",
      ":generate_locale_sources",
      ":generate_number_format_sources",
      ":generate_plural_rules_sources",
      ":generate_relative_time_format_sources",
      "//AK",
    ]
    public_deps = [ "//Userland/Libraries/LibTimeZone" ]
    sources = get_target_outputs(":generate_datetime_format_sources")
    sources += get_target_outputs(":generate_locale_sources")
    sources += get_target_outputs(":generate_number_format_sources")
    sources += get_target_outputs(":generate_plural_rules_sources")
    sources += get_target_outputs(":generate_relative_time_format_sources")
  }
}

source_set("LibLocale") {
  output_name = "locale"
  include_dirs = [
    "//Userland/Libraries",
    "$target_gen_dir/..",
  ]
  sources = [
    "DateTimeFormat.cpp",
    "Locale.cpp",
    "NumberFormat.cpp",
    "PluralRules.cpp",
    "RelativeTimeFormat.cpp",
  ]
  deps = [
    "//AK",
    "//Userland/Libraries/LibCore",
    "//Userland/Libraries/LibUnicode",
  ]
  if (enable_unicode_database_download) {
    deps += [ ":locale_data_sources" ]
    defines = [ "ENABLE_UNICODE_DATA=1" ]
  } else {
    defines = [ "ENABLE_UNICODE_DATA=0" ]
  }
}
