package com.quyunshuo.module.home.pay

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn

/**
 * 支付 接口 策略模式 提供默认实现
 */
abstract class DefultPayMentStrategy : PaymentStrategy {

    override suspend fun pay(params: Map<String, String>):Flow<String> {
        return request {  }
    }

    override suspend fun queryPayState(params: Map<String, String>): Flow<String> {
        return request {  }
    }



    /**
     * 发起请求封装
     * 该方法将flow的执行切换至IO线程
     *
     * @param requestBlock 请求的整体逻辑
     * @return Flow<T> @BuilderInference block: suspend FlowCollector<T>.() -> Unit
     */
    protected fun <T> request(
        requestBlock: suspend FlowCollector<T>.() -> Unit
    ): Flow<T> {
        return flow(block = requestBlock).flowOn(Dispatchers.IO)
    }
}