// Substitute the jitpack SDK dependency for your local instance of the project
// Enabling here allows you to build the example app against a local android SDK
def localPropertiesFile = file("local.properties")

if (localPropertiesFile.exists() && localPropertiesFile.canRead()) {
  def localProperties = new Properties()
  localProperties.load(new FileInputStream(localPropertiesFile))
  def localCompositeBuild = localProperties["localCompositeBuild"] == "true"
  def localSdkPath = localProperties["localSdkPath"] as String

  if (localCompositeBuild) {
    def sdkDirectory = new File(localSdkPath)

    if (!sdkDirectory.exists()) {
      throw new GradleException("Local Klaviyo Android SDK directory does not exist: " + sdkDirectory.getAbsolutePath())
    } else {
      print("Configuring composite project with local Klaviyo Android SDK for library.")
    }

    includeBuild(sdkDirectory) {
      dependencySubstitution {
        // substitute remote dependency with local module
        substitute module("com.github.klaviyo.klaviyo-android-sdk:core") using project(":sdk:core")
        substitute module("com.github.klaviyo.klaviyo-android-sdk:analytics") using project(":sdk:analytics")
        substitute module("com.github.klaviyo.klaviyo-android-sdk:forms") using project(":sdk:forms")
        substitute module("com.github.klaviyo.klaviyo-android-sdk:forms-core") using project(":sdk:forms-core")
        substitute module("com.github.klaviyo.klaviyo-android-sdk:location") using project(":sdk:location")
        substitute module("com.github.klaviyo.klaviyo-android-sdk:location-core") using project(":sdk:location-core")
        substitute module("com.github.klaviyo.klaviyo-android-sdk:push-fcm") using project(":sdk:push-fcm")
      }
    }
  }
}
