import { sliceByCells, textCellWidth } from '@simon_he/vue-tui'

export function ellipsisByCells(text: unknown, maxCells: number): string {
  maxCells = Math.max(0, Math.floor(maxCells))
  const s = String(text ?? '')
    .replace(/\s+/g, ' ')
    .trim()
  if (!s || maxCells <= 0) return ''
  if (textCellWidth(s) <= maxCells) return s
  if (maxCells <= 1) return sliceByCells(s, maxCells)
  return `${sliceByCells(s, maxCells - 1)}…`
}
