package com.gh.common.exposure

import com.gh.gamecenter.feature.exposure.ExposureEvent

object ExposureTraceUtils {

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

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

        return traceList
    }

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

}