/**
 * 表格高度计算工具函数
 */

/**
 * 计算表格所需的高度
 * @param options 配置选项
 * @returns 计算出的高度值（像素）
 */
export const calculateTableHeight = (options: {
  searchAndActionAreaHeight?: number // 搜索和操作区域高度，默认200px
  paginationAreaHeight?: number     // 分页区域高度，默认80px
  bufferHeight?: number             // 缓冲高度，默认70px
  minHeight?: number                // 最小高度，默认300px
  maxHeight?: number                // 最大高度，默认无限制
} = {}): number => {
  const {
    searchAndActionAreaHeight = 200, // 搜索表单和操作按钮区域高度
    paginationAreaHeight = 30,       // 分页组件高度
    bufferHeight = 70,               // 上下缓冲高度
    minHeight = 300,                 // 最小高度
    maxHeight = Infinity             // 最大高度
  } = options

  // 计算可用高度
  const calculatedHeight = window.innerHeight - searchAndActionAreaHeight - bufferHeight
  
  // 应用最小和最大高度限制
  const clampedHeight = Math.max(minHeight, Math.min(maxHeight, calculatedHeight))
  
  return clampedHeight
}

/**
 * 获取表格高度的CSS值
 * @param options 配置选项
 * @returns CSS高度值字符串
 */
export const getTableHeightCssValue = (options: {
  searchAndActionAreaHeight?: number
  paginationAreaHeight?: number
  bufferHeight?: number
  minHeight?: number
  maxHeight?: number
} = {}): string => {
  const height = calculateTableHeight(options)
  return `${height}px`
}

/**
 * 创建响应式的表格高度计算器
 * @param options 配置选项
 * @returns 包含计算方法和响应式值的对象
 */
export const useTableHeightCalculator = (options: {
  searchAndActionAreaHeight?: number
  paginationAreaHeight?: number
  bufferHeight?: number
  minHeight?: number
  maxHeight?: number
} = {}) => {
  const {
    searchAndActionAreaHeight = 200,
    paginationAreaHeight = 70,
    bufferHeight = 30,
    minHeight = 300,
    maxHeight = Infinity
  } = options

  // 计算表格高度
  const calculate = (): number => {
    const calculatedHeight = window.innerHeight - searchAndActionAreaHeight - paginationAreaHeight - bufferHeight
    return Math.max(minHeight, Math.min(maxHeight, calculatedHeight))
  }

  // 获取CSS值
  const getCssValue = (): string => {
    return `${calculate()}px`
  }

  return {
    calculate,
    getCssValue
  }
}
