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

plugins {
  id 'com.android.library'
  // Resolve the RN Gradle plugin via the consuming app's settings.gradle pluginManagement/includeBuild.
  // We only APPLY it when New Architecture is enabled (codegen gated below).
  id 'com.facebook.react' apply false
}

android {
  namespace "com.feedbackreactnativesdk"
  // Prefer rootProject ext provided by RN apps; fall back to reasonable defaults.
  compileSdk (rootProject.ext.has('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 34)

  defaultConfig {
    minSdk (rootProject.ext.has('minSdkVersion') ? rootProject.ext.minSdkVersion : 21)
    targetSdk (rootProject.ext.has('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 34)
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    consumerProguardFiles 'consumer-rules.pro'
  }
  buildFeatures {
    buildConfig true
  }
  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable 'GradleCompatible'
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  sourceSets {
    main {
      if (isNewArchitectureEnabled()) {
          java.srcDirs += ['src/turbo']
      } else {
          java.srcDirs += ['src/legacy']
      }
    }
  }
}

repositories {
  mavenCentral()
  google()

  def found = false
  def defaultDir = null
  def androidSourcesName = 'React Native sources'

  if (rootProject.ext.has('reactNativeAndroidRoot')) {
    defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
  } else {
    defaultDir = new File(
      projectDir,
      '/../../../node_modules/react-native/android'
    )
  }

  if (defaultDir.exists()) {
    maven {
      url defaultDir.toString()
      name androidSourcesName
    }

    logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
    found = true
  } else {
    def parentDir = rootProject.projectDir

    1.upto(5, {
      if (found) return true
      parentDir = parentDir.parentFile

      def androidSourcesDir = new File(
        parentDir,
        'node_modules/react-native'
      )

      def androidPrebuiltBinaryDir = new File(
        parentDir,
        'node_modules/react-native/android'
      )

      if (androidPrebuiltBinaryDir.exists()) {
        maven {
          url androidPrebuiltBinaryDir.toString()
          name androidSourcesName
        }

        logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
        found = true
      } else if (androidSourcesDir.exists()) {
        maven {
          url androidSourcesDir.toString()
          name androidSourcesName
        }

        logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
        found = true
      }
    })
  }

  if (!found) {
    throw new RuntimeException(
      "${project.name}: unable to locate React Native android sources. " +
      "Ensure you have you installed React Native as a dependency in your project and try again."
    )
  }
}


def resolveReactNativeVersion() {
  def versionOverride = rootProject.ext.has("ReactNativeVersion") ? rootProject.ext.get("ReactNativeVersion") : null
  if (versionOverride) {
    return versionOverride
  }

  def packageJsonFile = new File(projectDir, "../package.json")
  if (packageJsonFile.exists()) {
    def packageJsonText = packageJsonFile.text
    def findReactNativeVersion = { pattern ->
      def matcher = (packageJsonText =~ pattern)
      return matcher.find() ? matcher.group(1).replace("^", "").replace("~", "") : null
    }

    def depsVersion = findReactNativeVersion(/"dependencies"\s*:\s*\{[^}]*"react-native"\s*:\s*"([^"]+)"/)
    if (depsVersion) {
      return depsVersion
    }

    def devDepsVersion = findReactNativeVersion(/"devDependencies"\s*:\s*\{[^}]*"react-native"\s*:\s*"([^"]+)"/)
    if (devDepsVersion) {
      return devDepsVersion
    }
  }

  return "+"
}

dependencies {
  def reactNativeAarVersion = resolveReactNativeVersion()
  implementation "com.facebook.react:react-android:${reactNativeAarVersion}"
  implementation 'co.pisano:feedback:1.3.30'
  implementation 'androidx.cardview:cardview:1.0.0'
  implementation 'com.google.android.material:material:1.12.0'
  api 'com.squareup.retrofit2:retrofit:2.9.0'
  api 'com.squareup.retrofit2:converter-gson:2.9.0'
}

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

  react {
    jsRootDir = file("../src/")
    libraryName = "FeedbackReactNativeSdk"
    codegenJavaPackageName = "com.feedbackreactnativesdk"
  }
}
