import appSettings from '@/core/config/app-settings'
// #ifdef H5
import { runningInsideWeChat } from '@/platform/h5-wechat'
// #endif

const FALLBACK_CLIENT = {
	app: 'app-hl88live',
	wechat: 'wechat-hl88live',
	h5: 'h5-hl88live',
	mp: 'mp-hl88live'
}

function resolveClientId() {
	const map = appSettings.httpAppClient || {}
	// #ifdef APP-PLUS
	return map.app || FALLBACK_CLIENT.app
	// #endif
	// #ifdef H5
	return runningInsideWeChat()
		? (map.wechat || FALLBACK_CLIENT.wechat)
		: (map.h5 || FALLBACK_CLIENT.h5)
	// #endif
	// #ifdef MP
	return map.mp || FALLBACK_CLIENT.mp
	// #endif
	return FALLBACK_CLIENT.h5
}

export function applyClientHeaders(headers = {}) {
	const next = { ...headers }
	next['App-Client'] = resolveClientId()
	next['Accept-Language'] = 'zh-CN,zh;q=0.9'
	next.language = uni.getLocale()
	// #ifdef APP-PLUS
	next.uuid = plus.device.uuid
	next['App-Version'] = plus.runtime.version
	// #endif
	return next
}

export function readSessionToken() {
	const cached = uni.getStorageSync('hl88app:user')
	return cached && cached.token ? cached.token : ''
}
