import { withResolvers } from "../promise";

export function setPageOrientation(orientation: 'portrait' | 'landscape') {
  const { promise, resolve, reject } = withResolvers();
  (<any>wx).setPageOrientation({ orientation, success: resolve, fail: reject });
  return promise;
}

export function getPageOrientation() {
  const { windowWidth, windowHeight, safeArea } = wx.getWindowInfo();
  const { bottom } = wx.getMenuButtonBoundingClientRect();

  if (windowWidth > windowHeight) {
    return {
      orientation: 'landscape',
      left: safeArea?.left ?? 0,
      right: 0,
      width: windowWidth - (safeArea.left ?? 0),
      height: (safeArea?.height ?? windowHeight) - bottom
    } as const;
  } else {
    return {
      orientation: 'portrait',
      width: windowWidth,
      height: (safeArea?.bottom ?? windowHeight) - bottom
    } as const;
  }
}