import Taro, { useReady } from '@tarojs/taro'

// 可视区域定义 安全距离 减去 头部状态栏 和 导航栏的高度
async function useGetCustomClient(cb) {
  useReady(() => {
    const topSpaceHeight = getTopSafeHeight()
    const systemInfo = Taro.getSystemInfoSync()
    const { safeArea = { height: 0 }, windowHeight } = systemInfo
    let height = 0

    if (safeArea.height) {
      height = safeArea.height - topSpaceHeight
    } else {
      height = windowHeight - topSpaceHeight - 60
    }

    height *= 0.85

    const width = height * 375 / 667

    cb({ width, height})
    
  })
}

export function getTopSafeHeight() {
    // 状态栏 刘海屏
    const systemInfo = Taro.getSystemInfoSync()
    const { statusBarHeight = 0 } = systemInfo
    // 胶囊信息
    const menuInfo = Taro.getMenuButtonBoundingClientRect()
    // 导航高度固定 44px
    let navigationBarHeight = (menuInfo.top - statusBarHeight) * 2 + menuInfo.height

    navigationBarHeight = Math.min(44, navigationBarHeight)

    return navigationBarHeight + statusBarHeight
}

export default useGetCustomClient
