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

buildscript {
  if (project == rootProject) {
    repositories {
      google()
      mavenCentral()
    }

    dependencies {
      classpath 'com.android.tools.build:gradle:4.1.2'
    }
  }
}

apply plugin: 'com.android.library'

def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger()
def shouldUseNameSpace = agpVersion >= 7
def PACKAGE_PROP = "package=\"org.reactnative.maskedview\""
def manifestOutFile = file("${projectDir}/src/main/AndroidManifest.xml")
def manifestContent = manifestOutFile.getText()
if(shouldUseNameSpace){
      manifestContent = manifestContent.replaceAll(
        PACKAGE_PROP,
        ''
    )  
} else {
    if(!manifestContent.contains("$PACKAGE_PROP")){
        manifestContent = manifestContent.replace(
            '<manifest',
            "<manifest $PACKAGE_PROP "
        )
    }
}
manifestContent.replaceAll("  ", " ")
manifestOutFile.write(manifestContent)

android {
  compileSdkVersion safeExtGet('compileSdkVersion', 28)
  
  if(shouldUseNameSpace){
    namespace = "org.reactnative.maskedview"
  }

  defaultConfig {
    minSdkVersion safeExtGet('minSdkVersion', 16)
    targetSdkVersion safeExtGet('targetSdkVersion', 28)
  }

  sourceSets {
    main {
      java.srcDirs = ['src/main/java']
    }
  }

  lintOptions {
    abortOnError false
    warning 'InvalidPackage'
  }
    buildTypes {
        release {
        }
    }
}

repositories {
  google()
  maven { url "https://jitpack.io" }
  mavenCentral()
  maven {
    // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
    url "$rootDir/../node_modules/react-native/android"
  }
}

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