apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'

def ideaActive = System.getProperty("idea.active") == "true"

kotlin {
    iosArm64("iosArm64") {
        binaries {
            framework {
                freeCompilerArgs += "-Xobjc-generics"
            }
        }
    }
    iosX64("iosX64") {
        binaries {
            framework {
                freeCompilerArgs += "-Xobjc-generics"
            }
        }
    }
    if (ideaActive) {
        iosX64("ios") {
            binaries {
                framework {
                    freeCompilerArgs += "-Xobjc-generics"
                }
            }
        }
    }
    sourceSets {
        final List<String> projectsList = [
                ":model",
                ":data:api",
                ":data:repository",
        ]
        commonMain {
            projectsList.forEach {
                kotlin.srcDirs += "${project(it).projectDir}/src/commonMain/kotlin"
            }
            languageSettings {
                languageVersion = '1.3'
                apiVersion = '1.3'
                progressiveMode = true
            }
            dependencies {
                projectsList.forEach {
                    project(it).configurations.all { c ->
                        if (c.name == "commonMainApi") {
                            c.dependencies.all { dep ->
                                // note: Avoid dependencies for this project's modules like a `api project(":model")`.
                                //       Already these classes are included via `srcDir`,
                                //       so adding dependencies for projects causes compile error by double declaration.
                                if (!(dep instanceof ProjectDependency)) {
                                    api dep
                                }
                            }
                        } else if (c.name == "commonMainImplementation") {
                            c.dependencies.all { dep ->
                                if (!(dep instanceof ProjectDependency)) {
                                    implementation dep
                                }
                            }
                        }
                    }
                }

            }
        }
        iOSMain {
            final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'
            projectsList.forEach {
                kotlin.srcDirs += "${project(it).projectDir}/src/iosMain/kotlin"
                if (mode == "RELEASE") {
                    kotlin.srcDirs += "${project(it).projectDir}/src/iosMain/kotlinRelease"
                } else {
                    kotlin.srcDirs += "${project(it).projectDir}/src/iosMain/kotlinDebug"
                }
            }
            dependencies {
                projectsList.forEach {
                    project(it).configurations.all { c ->
                        if (c.name == "iOSMainApi") {
                            c.dependencies.all { dep ->
                                if (!(dep instanceof ProjectDependency)) {
                                    api dep
                                }
                            }
                        } else if (c.name == "iOSMainImplementation") {
                            c.dependencies.all { dep ->
                                if (!(dep instanceof ProjectDependency)) {
                                    implementation dep
                                }
                            }
                        }
                    }
                }
            }
        }
        configure(iosX64Main) {
            dependsOn iOSMain
            dependencies {
                projectsList.forEach {
                    project(it).configurations.all { c ->
                        if (c.name == "iosX64MainApi") {
                            c.dependencies.all { dep ->
                                if (!(dep instanceof ProjectDependency)) {
                                    api dep
                                }
                            }
                        } else if (c.name == "iosX64MainImplementation") {
                            c.dependencies.all { dep ->
                                if (!(dep instanceof ProjectDependency)) {
                                    implementation dep
                                }
                            }
                        }
                    }
                }
            }
        }
        configure(iosArm64Main) {
            dependsOn iOSMain
            dependencies {
                projectsList.forEach {
                    project(it).configurations.all { c ->
                        if (c.name == "iosArm64MainApi") {
                            c.dependencies.all { dep ->
                                if (!(dep instanceof ProjectDependency)) {
                                    api dep
                                }
                            }
                        } else if (c.name == "iosArm64MainImplementation") {
                            c.dependencies.all { dep ->
                                if (!(dep instanceof ProjectDependency)) {
                                    implementation dep
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

configurations {
    compileClasspath
}