import type { TableColumnCtx } from 'element-plus'
import type { DefaultRow } from 'element-plus/es/components/table/src/table/defaults'

export interface SummaryMethodProps<T extends object> {
  columns: TableColumnCtx<T>[]
  data: T[]
}

export type SummariesType<T extends object> = {
  [key in keyof T]?: (data: T[]) => string
} & {
  [key: string]: (data: T[]) => string
}

export default function buildSummaryMethod<T extends DefaultRow>(summaries: SummariesType<T>) {
  return ({columns, data}: SummaryMethodProps<T>) => {
    const summary: string[] = [ '合计' ]
    for (let i = 1; i < columns.length; i++) {
      const column = columns[i]
      summary[i] = summaries[column.property]?.(data) ?? ''
    }
    return summary
  }
}
