import { getWindowSize } from '../../../utils/helper-dom'
import { WideMinWidth, NarrowMinWidth, TableMinWidth } from './const'


export const gRowFields = function (rowFields: any[]){
  return rowFields.reduce((mo: any, v: any) => {
    mo[v.name]= v;
    return mo
  }, {})
}

export const getRealWidth = function (widnowWidth: number, width: string | number): number{
  if(typeof width === 'number'){
    return width
  }
  if(/%$/.test(width)){
    return ~~(widnowWidth * parseInt(width) / 100)
  }
  return parseInt(width)
}

export const getWConfig = function (rowInfo: any, paddingLeft: number, paddingRight: number){
  let winWidth = getWindowSize().width
  let winUsableWidth = winWidth - (paddingLeft + paddingRight)

  let minTableWidth = getRealWidth(winUsableWidth, rowInfo.minTableWidth || TableMinWidth)
  let minWideWidth = getRealWidth(winUsableWidth, rowInfo.minWideWidth || WideMinWidth)
  let minNarroWidth = getRealWidth(winUsableWidth, rowInfo.minNarroWidth || NarrowMinWidth)

  let wideRowInfoWidth = getRealWidth(winUsableWidth, rowInfo.wideRowInfoWidth || minWideWidth)
  let narrowRowInfoWidth = getRealWidth(winUsableWidth, rowInfo.narrowRowInfoWidth || minNarroWidth)
  wideRowInfoWidth = wideRowInfoWidth < minWideWidth? minWideWidth: wideRowInfoWidth
  narrowRowInfoWidth = narrowRowInfoWidth < minNarroWidth? minNarroWidth: narrowRowInfoWidth

  return {
    winWidth,// body 宽度
    winUsableWidth,// 实际可用宽度
    minTableWidth,// 表格最小宽度
    minWideWidth,// 宽详情最小宽度
    minNarroWidth,// 窄详情最小宽度
    wideRowInfoWidth,// 宽详情默认宽度
    narrowRowInfoWidth,// 窄详情默认宽度
  }
}