/**
 * 分包页视口高度：iOS 上 scroll-view 需父级固定 px 高度，避免 flex+height:0 导致空白
 */
export default {
  data() {
    return {
      pageHeightPx: 0
    }
  },
  computed: {
    pageStyle() {
      if (this.pageHeightPx > 0) {
        return { height: `${this.pageHeightPx}px` }
      }
      return {}
    }
  },
  onLoad() {
    this.applyPageHeight()
  },
  onReady() {
    this.applyPageHeight()
  },
  methods: {
    getNavigationBarHeight() {
      const sys = uni.getSystemInfoSync()
      const statusBar = sys.statusBarHeight || 0
      // #ifdef MP-WEIXIN
      try {
        const menu = uni.getMenuButtonBoundingClientRect()
        if (menu && menu.height) {
          return statusBar + (menu.top - statusBar) * 2 + menu.height
        }
      } catch (e) {
        /* noop */
      }
      // #endif
      return statusBar + 44
    },
    getViewportContentHeight() {
      const sys = uni.getSystemInfoSync()
      const navH = this.getNavigationBarHeight()
      // #ifdef H5
      if (typeof window !== 'undefined' && window.innerHeight) {
        return Math.max(320, window.innerHeight - navH)
      }
      // #endif
      const screenH = sys.screenHeight || sys.windowHeight || 600
      return Math.max(320, screenH - navH)
    },
    applyPageHeight() {
      this.pageHeightPx = this.getViewportContentHeight()
    }
  }
}
