/**
 * 屏幕方向检测 Hook
 */

import { useMemo } from 'react';
import { useWindowDimensions } from './useWindowDimensions';

export type Orientation = 'portrait' | 'landscape';

export interface OrientationInfo {
  orientation: Orientation;
  isPortrait: boolean;
  isLandscape: boolean;
}

/**
 * 获取屏幕方向
 */
function getOrientation(width: number, height: number): Orientation {
  return width > height ? 'landscape' : 'portrait';
}

/**
 * 屏幕方向检测 Hook
 */
export function useOrientation(): OrientationInfo {
  const { width, height } = useWindowDimensions();

  const orientation = useMemo(() => getOrientation(width, height), [width, height]);

  return useMemo(() => ({
    orientation,
    isPortrait: orientation === 'portrait',
    isLandscape: orientation === 'landscape',
  }), [orientation]);
}
