import { ref, nextTick, computed } from 'vue'
import { onReady } from "@dcloudio/uni-app"
import { query, systemInfo } from "@/utils"


function useListHeight() {
	const navbarNodeInfo = ref(null)
	const footerHeightInfo = ref({
		height: 0,
		keyboardHeight: 0,
		bottomSafeDistance: 0
	})
	const sysInfo = ref(systemInfo())


	// 计算列表高度
	const listHeight = computed(() => {
		const { windowHeight } = sysInfo.value
		const navbarHeight = navbarNodeInfo.value ? navbarNodeInfo.value.height : 0
		let footerHeight = 0
		if (footerHeightInfo.value) {
			const { height = 0, keyboardHeight = 0, bottomSafeDistance = 0 } = footerHeightInfo.value
			footerHeight = height + keyboardHeight
			const isIOS = uni.getSystemInfoSync().platform === 'ios';
			if (isIOS) {
				footerHeight += bottomSafeDistance;
			}
		}

		return windowHeight - navbarHeight - footerHeight
	})


	onReady(() => {
		nextTick(() => {
			query(".user-chat__navbar")
				.then(result => {
					navbarNodeInfo.value = result
				})
		})
	})

	return {
		navbarNodeInfo,
		footerHeightInfo,
		sysInfo,
		listHeight
	}
}


export default useListHeight