package ru.fincode.tsudesk.core.common.log

import android.util.Log

private fun Any.moduleTag(): String {
    val pkg = this::class.java.`package`?.name.orEmpty()
    val parts = pkg.split('.')
    val module =
        if (parts.size >= 5 && parts[0] == "ru" && parts[1] == "fincode" && parts[2] == "tsudesk") {
            when (parts[3]) {
                "core", "feature" -> parts[4]
                else -> parts[3] // fallback
            }
        } else {
            parts.lastOrNull() ?: "unknown"
        }
    return "${Constants.TAG}:${module.uppercase()}"
}

fun Any.logD(message: String) = Log.d(moduleTag(), message)
fun Any.logD(message: String, throwable: Throwable) = Log.d(moduleTag(), message, throwable)
fun Any.logE(message: String) = Log.e(moduleTag(), message)
fun Any.logE(message: String, throwable: Throwable) = Log.e(moduleTag(), message, throwable)
