/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

// To run intltests, cd hermes/android and run:
//   ./gradlew intl:connectedAndroidTest

plugins {
  id("com.android.library")
  id("de.undercouch.download")
  id("com.facebook.hermes.plugins.internal.hermesUtils")
}

import de.undercouch.gradle.tasks.download.Download

buildDir = "${hermesUtils.hermesWs}/build_android/intltest"
buildDir.mkdirs()

def testDestination = "java/com/facebook/hermes/test/assets"

task prepareTests() {
  doLast {
    def test262Dir = file(hermesUtils.fbsource).exists() ?
                 "${hermesUtils.fbsource}/xplat/third-party/javascript-test-suites/test262/" :
                 "${hermesUtils.hermesWs}/test262"
    assert file(test262Dir).exists() : "Test262 directory not found"
    copy{
      from(test262Dir) {
        include("test/intl402/Collator/**/*.js")
        include("test/intl402/DateTimeFormat/**/*.js")
        include("test/intl402/NumberFormat/**/*.js")
        include("test/intl402/String/**/*.js")
        include("test/intl402/Number/**/*.js")
        include("test/intl402/Array/**/*.js")
        include("test/intl402/TypedArray/**/*.js")
        include("test/intl402/Date/**/*.js")
        include("test/intl402/Intl/**/*.js")
        include("test/built-ins/String/prototype/toLocale**/*.js")
        include("test/built-ins/String/prototype/localeCompare/**/*.js")
        include("test/built-ins/Date/prototype/toLocale**/*.js")
        include("test/built-ins/Number/prototype/toLocaleString/**/*.js")
        include("test/built-ins/Array/prototype/toLocaleString/**/*.js")
        include("harness/*.js")
        includeEmptyDirs = false
      }
      into("${testDestination}/test262")
    }
    copy {
      from("../../test/hermes/intl")
      into("${testDestination}")
    }
  }
}

// TODO: Figure out how to deduplicate this file and intl/build.gradle
android {
  namespace = "com.facebook.hermes.intltest"
  compileSdk = hermesUtils.compileSdk
  ndkVersion = hermesUtils.ndkVersion

  defaultConfig {
    minSdk = hermesUtils.minSdk
    ndk {
      abiFilters (*hermesUtils.abis)
    }
    testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

    externalNativeBuild {
      cmake {
        arguments "-DHERMES_IS_ANDROID=True"
        arguments "-DHERMES_FACEBOOK_BUILD=${hermesUtils.facebookBuild}"
        arguments "-DANDROID_STL=c++_shared"
        arguments "-DANDROID_PIE=True"
        arguments "-DIMPORT_HOST_COMPILERS=${hermesUtils.hermesC}"
        arguments "-DHERMES_SLOW_DEBUG=False"
        arguments "-DHERMES_ENABLE_INTL=True"
        targets "jsijni", "jsijniepi", "hermesvm"
      }
    }
    ndk {
      abiFilters (*hermesUtils.abis)
    }
  }

  externalNativeBuild {
    cmake {
      version hermesUtils.cmakeVersion
      path "../../CMakeLists.txt"
      buildStagingDirectory = "${hermesUtils.hermesWs}/staging/intl"
      buildStagingDirectory.mkdirs()
    }
  }

  // Allow using prefab so that we can import libfbjni.so.
  buildFeatures {
    prefab true
  }

  dependencies {
    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'org.assertj:assertj-core:2.9.1'
    androidTestImplementation 'com.facebook.soloader:soloader:0.10.4'
    androidTestImplementation 'com.facebook.yoga:proguard-annotations:1.19.0'

    implementation 'com.facebook.fbjni:fbjni:0.3.0'
    implementation "androidx.annotation:annotation:1.3.0"
    implementation "androidx.annotation:annotation-experimental:1.0.0"
  }

  // TODO: Revisit this if there is a better solution for deduplicating native
  // libraries.
  packagingOptions {
    pickFirst "**/libfbjni.so"
    pickFirst "**/libc++_shared.so"
  }

  sourceSets {
    main {
      java {
        srcDirs = []
      }
    }
    androidTest {
      java {
        srcDirs = [
          "java",
          // Include AndroidUnicodeUtils, which would normally come from RN
          "../../lib/Platform/Unicode/java",
          "../../lib/Platform/Intl/java",
        ]
      }
      resources {
        srcDir "java/com/facebook/hermes/test"
      }
    }
  }
}

repositories {
  google()
  mavenCentral()
}
