import java.nio.file.Paths

buildscript {
    ext.findFile = { fileName ->
        def currentDirPath = rootDir == null ? null : rootDir.toString()

        while (currentDirPath != null) {
            def currentDir = file(currentDirPath);
            def requestedFile = Paths.get(currentDirPath, fileName).toFile()

            if (requestedFile.exists()) {
                return requestedFile
            }

            currentDirPath = currentDir.getParent()
        }

        return null
    }

    ext.findNodeModulesPath = { packageName ->
        return findFile(Paths.get("node_modules", packageName).toString())
    }

    ext.getExtProp = { prop, defaultValue ->
        return rootProject.ext.has(prop) ? rootProject.ext.get(prop) : defaultValue
    }

    ext.kotlinVersion = getExtProp("kotlinVersion", "1.5.31")

    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath("com.android.tools.build:gradle:${getExtProp('androidPluginVersion', '7.2.1')}")
    }
}

plugins {
    id("com.android.library")
    id("org.jetbrains.kotlin.android") version "${kotlinVersion}"
}

repositories {
    maven {
        url("${findNodeModulesPath('react-native')}/android")
    }

    google()
    mavenCentral()
}

android {
    compileSdkVersion getExtProp("compileSdkVersion", 31)
    defaultConfig {
        minSdkVersion getExtProp("minSdkVersion", 23)
        targetSdkVersion getExtProp("targetSdkVersion", 29)
    }
    lintOptions {
        abortOnError false
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"

    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"
}
