// NOTE:This file was generated from packages/generator-react-native-vector-icons/src/app/templates
// If you're contributing to react-native-vector-icons, make the change there, otherwise it'll be lost

def isNewArchitectureEnabled() {
  return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}

def safeExtGet(prop, fallback) {
  rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

apply plugin: "com.android.library"
apply plugin: "kotlin-android"

if (isNewArchitectureEnabled()) {
  apply plugin: "com.facebook.react"
}

def supportsNamespace() {
  def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
  def major = parsed[0].toInteger()
  def minor = parsed[1].toInteger()

  // Namespace support was added in 7.3.0
  return (major == 7 && minor >= 3) || major >= 8
}

android {
  if (supportsNamespace()) {
    namespace "com.reactnativevectoricons.fontawesome6_pro"

    sourceSets {
      main {
        manifest.srcFile "src/main/AndroidManifestNew.xml"
      }
    }
  }

  compileSdkVersion safeExtGet('compileSdkVersion', 31)

  defaultConfig {
    targetSdkVersion safeExtGet('targetSdkVersion', 31)
    minSdkVersion safeExtGet('minSdkVersion', 24)
  }
}

dependencies {
  implementation "com.facebook.react:react-native:+"
}

if (isNewArchitectureEnabled()) {
  react {
    jsRootDir = file("../src/")
    libraryName = "VectorIconsFontAwesome6Pro"
    codegenJavaPackageName = "com.reactnativevectoricons.fontawesome6_pro"
  }
}

android.sourceSets.main.assets.srcDirs += file("$buildDir/intermediates/RNVI-fontawesome6-pro")

task copyFonts(type: Copy) {
  from "../fonts"
  include "*.ttf"

  into "${buildDir}/intermediates/RNVI-fontawesome6-pro/fonts"
  eachFile { println "(RNVI:fontawesome6-pro) Copying font ${it.file}" }
}

preBuild.dependsOn(copyFonts)

// Find the app's project root: walk up to the nearest package.json.
// Works for both regular apps (app/android/) and monorepos.
def rnviProjectRoot = rootDir.parentFile
while (rnviProjectRoot != null && !new File(rnviProjectRoot, 'package.json').exists()) {
  rnviProjectRoot = rnviProjectRoot.parentFile
}
if (rnviProjectRoot == null) {
  throw new GradleException("(RNVI-fontawesome6-pro) Could not find package.json in any parent of ${rootDir.parentFile}")
}

// Read fontDir config from the app's package.json (default: "rnvi-fonts")
def rnviAppPkg = new groovy.json.JsonSlurper().parse(new File(rnviProjectRoot, 'package.json'))
def rnviFontDir = rnviAppPkg?.reactNativeVectorIcons?.fontDir ?: 'rnvi-fonts'
def rnviCustomFontsSrc = new File(rnviProjectRoot, "${rnviFontDir}/fontawesome6-pro")

if (!rnviCustomFontsSrc.exists()) {
  throw new GradleException("(RNVI-fontawesome6-pro) Custom fonts directory not found: ${rnviCustomFontsSrc}")
}

def rnviFontFiles = rnviCustomFontsSrc.listFiles({ File f -> f.name.endsWith('.ttf') } as FileFilter)
if (rnviFontFiles == null || rnviFontFiles.length == 0) {
  throw new GradleException("(RNVI-fontawesome6-pro) No font files found in ${rnviCustomFontsSrc}")
}

task copyExtraFonts(type: Copy) {
  from rnviCustomFontsSrc
  include "*.ttf"

  into "${buildDir}/intermediates/RNVI-fontawesome6-pro/fonts"
}

preBuild.dependsOn(copyExtraFonts)
