/**
 * 响应式常量定义
 */

// ==================== 断点定义 ====================
export const BREAKPOINTS = {
  mobile: 0,      // 手机
  tablet: 768,    // 平板竖屏
  desktop: 1024,  // 平板横屏/桌面
  wide: 1440,     // 宽屏桌面
} as const;

export type BreakpointKey = keyof typeof BREAKPOINTS;
export type BreakpointValue = typeof BREAKPOINTS[BreakpointKey];

// ==================== 更细粒度的断点定义 ====================
export const FINE_BREAKPOINTS = {
  xs: 0,      // 超小屏手机
  sm: 375,    // 小屏手机 (iPhone SE, 小屏安卓)
  md: 414,    // 中屏手机 (iPhone Pro Max, 大屏安卓)
  lg: 768,    // 平板竖屏 / 大折叠屏手机展开
  xl: 1024,   // 平板横屏 / 小桌面
  '2xl': 1280, // 桌面
  '3xl': 1440, // 大桌面
  '4xl': 1920, // 超大屏
} as const;

export type FineBreakpointKey = keyof typeof FINE_BREAKPOINTS;

// ==================== 响应式值类型 ====================
export type ResponsiveValue<T> = T | Partial<Record<FineBreakpointKey, T>>;

// ==================== 断点顺序（用于比较）====================
export const BREAKPOINT_ORDER: FineBreakpointKey[] = ['xs', 'sm', 'md', 'lg', 'xl', '2xl', '3xl', '4xl'];
export const REVERSE_BREAKPOINT_ORDER: FineBreakpointKey[] = ['4xl', '3xl', '2xl', 'xl', 'lg', 'md', 'sm', 'xs'];

// ==================== 获取当前断点 ====================
export function getBreakpoint(width: number): BreakpointKey {
  if (width >= BREAKPOINTS.wide) {
    return 'wide';
  }
  if (width >= BREAKPOINTS.desktop) {
    return 'desktop';
  }
  if (width >= BREAKPOINTS.tablet) {
    return 'tablet';
  }
  return 'mobile';
}

// ==================== 获取细粒度断点 ====================
export function getFineBreakpoint(width: number): FineBreakpointKey {
  if (width >= FINE_BREAKPOINTS['4xl']) return '4xl';
  if (width >= FINE_BREAKPOINTS['3xl']) return '3xl';
  if (width >= FINE_BREAKPOINTS['2xl']) return '2xl';
  if (width >= FINE_BREAKPOINTS.xl) return 'xl';
  if (width >= FINE_BREAKPOINTS.lg) return 'lg';
  if (width >= FINE_BREAKPOINTS.md) return 'md';
  if (width >= FINE_BREAKPOINTS.sm) return 'sm';
  return 'xs';
}

// ==================== 断点比较工具 ====================
/**
 * 检查当前断点是否大于等于目标断点
 */
export function isBreakpointGTE(
  current: FineBreakpointKey,
  target: FineBreakpointKey
): boolean {
  return BREAKPOINT_ORDER.indexOf(current) >= BREAKPOINT_ORDER.indexOf(target);
}

/**
 * 检查当前断点是否小于目标断点
 */
export function isBreakpointLT(
  current: FineBreakpointKey,
  target: FineBreakpointKey
): boolean {
  return !isBreakpointGTE(current, target);
}
