import java.nio.file.Paths

def resolveModulePath(packageName) {
    def basePath = rootDir.toPath().normalize()

    // Node's module resolution algorithm searches up to the root directory,
    // after which the base path will be null
    while (basePath) {
        def candidatePath = Paths.get(basePath.toString(), 'node_modules', packageName)
        if (candidatePath.toFile().exists()) {
            return candidatePath.toString()
        }

        basePath = basePath.getParent()
    }

    return null
}

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

configurations {
    compileClasspath
}

buildscript {
    if (project == rootProject) {
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.6.4'
        }
    }
}

// AsyncStorage has default size of 6MB.
// This is a sane limit to protect the user from the app storing too much data in the database.
// This also protects the database from filling up the disk cache and becoming malformed.
// If you really need bigger size, please keep in mind the potential consequences.
long dbSizeInMB = 6L

def newDbSize = rootProject.properties['AsyncStorage_db_size_in_MB']

if( newDbSize != null && newDbSize.isLong()) {
    dbSizeInMB = newDbSize.toLong()
}

def useDedicatedExecutor = rootProject.hasProperty('AsyncStorage_dedicatedExecutor')
        ? rootProject.properties['AsyncStorage_dedicatedExecutor']
        : false

apply plugin: 'com.android.library'

android {
    compileSdkVersion safeExtGet('compileSdkVersion', 28)
    buildToolsVersion safeExtGet('buildToolsVersion', '28.0.3')
    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 19)
        targetSdkVersion safeExtGet('targetSdkVersion', 28)
        buildConfigField "Long", "AsyncStorage_db_size", "${dbSizeInMB}L"
        buildConfigField("boolean", "AsyncStorage_useDedicatedExecutor", "${useDedicatedExecutor}")
    }
    lintOptions {
        abortOnError false
    }
}

repositories {
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url "${resolveModulePath("react-native")}/android"
    }
    google()
    jcenter()
}

dependencies {
    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-native:+'  // From node_modules
}
