package com.base.filerecoveryrecyclebin.billing

import android.content.Context
import androidx.lifecycle.DefaultLifecycleObserver
import com.android.billingclient.api.BillingClient
import com.android.billingclient.api.Purchase
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

class BillingClientLifecycle(
    private val applicationContext: Context,
) : DefaultLifecycleObserver {

    private val _subscriptionPurchases = MutableStateFlow<List<Purchase>>(emptyList())
    val subscriptionPurchases = _subscriptionPurchases.asStateFlow()


    @Volatile
    private var INSTANCE: BillingClientLifecycle? = null

    fun getInstance(applicationContext: Context): BillingClientLifecycle =
        INSTANCE ?: synchronized(this) {
            INSTANCE ?: BillingClientLifecycle(applicationContext).also { INSTANCE = it }
        }
}