/**
 * 安全区域 Composable
 * 用于获取屏幕边界到安全区域的距离
 */
export interface SafeAreaInsets {
  top: number
  right: number
  bottom: number
  left: number
}

export function useSafeArea() {
  let safeAreaInsets: SafeAreaInsets | null = null
  let systemInfo: any

  // #ifdef MP-WEIXIN
  // 微信小程序使用新的API
  systemInfo = uni.getWindowInfo()
  safeAreaInsets = systemInfo.safeArea
    ? {
        top: systemInfo.safeArea.top,
        right: systemInfo.windowWidth - systemInfo.safeArea.right,
        bottom: systemInfo.windowHeight - systemInfo.safeArea.bottom,
        left: systemInfo.safeArea.left,
      }
    : null
  // #endif

  // #ifndef MP-WEIXIN
  // 其他平台继续使用uni API
  systemInfo = uni.getSystemInfoSync()
  safeAreaInsets = systemInfo.safeAreaInsets
  // #endif

  return {
    safeAreaInsets,
    systemInfo,
  }
}
