package mg.dot.feufaro

object DeepLinkHandler {
    var hasConsumedDeepLink = false
    private var pendingData: Pair<String, String>? = null

    var onSongReceived: ((fileContent: String, filePath: String) -> Unit)? = null
        set(value) {
            field = value
            if (value != null) {
                consumePending()
            }
        }

    fun handleSongContent(fileContent: String, filePath: String) {
        var hasConsumedDeepLink = true
        val callback = onSongReceived
        if (callback != null) {
            println("DeepLink: callback dispo → appel immédiat")
            callback(fileContent, filePath)
        } else {
            println("DeepLink: callback null → mise en attente de $filePath")
            pendingData = Pair(fileContent, filePath)
        }
    }

    private fun consumePending() {
        pendingData?.let { (content, path) ->
            println("DeepLink: consommation du contenu en attente pour $path")
            onSongReceived?.invoke(content, path)
            pendingData = null
        }
    }
}