/**
 * 列数计算 Hook
 */

import { useMemo } from 'react';
import { useBreakpoint } from './useBreakpoint';
import { FineBreakpointKey } from './constants';

const DEFAULT_COLUMN_CONFIG: Record<FineBreakpointKey, number> = {
  xs: 1,
  sm: 1,
  md: 2,
  lg: 3,
  xl: 4,
  '2xl': 4,
  '3xl': 5,
  '4xl': 6,
};

/**
 * 根据容器宽度计算合适的列数
 * 
 * @param columnConfig - 列数配置
 * @returns 列数
 * 
 * @example
 * const columns = useColumnCount({
 *   xs: 1,
 *   sm: 2,
 *   md: 3,
 *   lg: 4
 * });
 */
export function useColumnCount(
  columnConfig: Partial<Record<FineBreakpointKey, number>> = {}
): number {
  const { fineBreakpoint } = useBreakpoint();
  
  return useMemo(() => {
    const config = { ...DEFAULT_COLUMN_CONFIG, ...columnConfig };
    const breakpointOrder: FineBreakpointKey[] = ['4xl', '3xl', '2xl', 'xl', 'lg', 'md', 'sm', 'xs'];
    const currentIndex = breakpointOrder.indexOf(fineBreakpoint);
    
    for (let i = currentIndex; i < breakpointOrder.length; i++) {
      const bp = breakpointOrder[i];
      if (config[bp] !== undefined) {
        return config[bp];
      }
    }
    
    return 1;
  }, [fineBreakpoint, columnConfig]);
}
