plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.android.library) apply false
    alias(libs.plugins.kotlin.jvm) apply false
    alias(libs.plugins.kotlin.multiplatform) apply false
    alias(libs.plugins.jetbrains.compose) apply false
    alias(libs.plugins.compose.compiler) apply false
    alias(libs.plugins.metalava) apply false
    alias(libs.plugins.maven.publish) apply false
    alias(libs.plugins.jetbrains.dokka)
}

tasks.withType(org.jetbrains.dokka.gradle.DokkaMultiModuleTask).configureEach {
    outputDirectory = rootProject.file('docs/api')
    failOnWarning = true
}

subprojects {
    // Read in the signing.properties file if it is exists
    def signingPropsFile = rootProject.file('release/signing.properties')
    if (signingPropsFile.exists()) {
        def localProperties = new Properties()
        signingPropsFile.withInputStream { is -> localProperties.load(is) }
        localProperties.each { prop ->
            if (prop.key == "signing.secretKeyRingFile") {
                // If this is the key ring, treat it as a relative path
                project.ext.set(prop.key, rootProject.file(prop.value).absolutePath)
            } else {
                project.ext.set(prop.key, prop.value)
            }
        }
    }

    // Must be afterEvaluate or else com.vanniktech.maven.publish will overwrite our
    // dokka and version configuration.
    afterEvaluate {
        if (tasks.findByName('dokkaHtmlPartial') == null) {
            // If dokka isn't enabled on this module, skip
            return
        }

        tasks.named('dokkaHtmlPartial') {
            dokkaSourceSets.configureEach {
                reportUndocumented.set(true)
                skipEmptyPackages.set(true)
                skipDeprecated.set(true)
                jdkVersion.set(8)

                // Add Android SDK packages
                noAndroidSdkLink.set(false)

                // Add samples from :sample module
                samples.from(rootProject.file("sample/shared/src/commonMain/kotlin/"))

                // AndroidX + Compose docs
                externalDocumentationLink {
                    url.set(new URL("https://developer.android.com/reference/"))
                    packageListUrl.set(new URL("https://developer.android.com/reference/androidx/package-list"))
                }
                externalDocumentationLink {
                    url.set(new URL("https://developer.android.com/reference/kotlin/"))
                    packageListUrl.set(new URL("https://developer.android.com/reference/kotlin/androidx/package-list"))
                }

                sourceLink {
                    localDirectory.set(project.file("src/commonMain/kotlin"))
                    // URL showing where the source code can be accessed through the web browser
                    remoteUrl.set(new URL("https://github.com/fornewid/material-motion-compose/blob/main/${project.name}/src/commonMain/kotlin"))
                    // Suffix which is used to append the line number to the URL. Use #L for GitHub
                    remoteLineSuffix.set("#L")
                }
            }
        }
    }
}
