/**
 * 列定义工具：消除手动分配 idx，支持 _actions 约定
 *
 * @param {Array<Object>} rawColumns 列配置数组
 * @param {Object} options
 * @param {number} options.selectionWidth 复选框列宽度，默认 55，传 0 不显示
 * @param {number} options.indexWidth 序号列宽度，默认 60，传 0 不显示
 * @returns {Array<Object>} 补齐 idx 后的列数组
 *
 * @example
 * const columns = useTableColumns([
 *   { prop: 'code', label: '编码', minWidth: 120 },
 *   { prop: 'name', label: '名称' },
 *   { prop: '_actions', label: '操作', width: 180, fixed: 'right' },
 * ])
 */
export function useTableColumns (rawColumns, options = {}) {
  const { selectionWidth = 55, indexWidth = 0 } = options

  const result = []

  if (selectionWidth > 0) {
    result.push({ idx: 0, type: 'selection', width: selectionWidth })
  }
  if (indexWidth > 0) {
    result.push({ idx: result.length, type: 'index', width: indexWidth, label: '#' })
  }

  let slotIdx = 100
  rawColumns.forEach((col, i) => {
    const clean = { ...col }
    if (clean.prop === '_actions') {
      clean.slot = '_actions'
      clean.idx = slotIdx++
    } else if (clean.slot || clean.headerSlot) {
      clean.idx = slotIdx++
    } else {
      clean.idx = result.length
    }
    result.push(clean)
  })

  return result
}

export default useTableColumns
