package com.gh.common.exposure

object ExposureTraceUtils {

    fun appendTrace(event: ExposureEvent?): List<ExposureEvent> {
        val traceList = arrayListOf<ExposureEvent>()

        event?.let {
            //这里使用deepCopy，是为了防止循环引用调用hashCode方法触发StackOverflowError错误
            val deepCopy = it.deepCopy()
            if (deepCopy.eTrace == null) {
                traceList.add(deepCopy)
            } else {
                traceList.addAll(deepCopy.eTrace!!)
                traceList.add(flattenTrace(deepCopy))
            }
        }

        return traceList
    }

    private fun flattenTrace(event: ExposureEvent): ExposureEvent {
        event.eTrace = arrayListOf()
        return event
    }

}