/** Vuetify v-data-table sort-by item */
export type TableSortItem = { key: string; order: 'asc' | 'desc' }

/** 表头 key → GET /api/servers/ sort_by（白名单与 server_repo._SORT_COLUMNS 一致） */
const SERVER_COLUMN_TO_API: Record<string, string> = {
  status: 'status',
  name: 'name',
  domain: 'domain',
  address: 'domain',
  category: 'category',
  target_path: 'target_path',
  agent_version: 'agent_version',
  agent: 'agent_version',
  last_heartbeat: 'heartbeat',
  heartbeat: 'heartbeat',
}

export function toServerApiSort(sortBy: TableSortItem[]): { sort_by?: string; sort_order?: 'asc' | 'desc' } {
  if (!sortBy.length) return {}
  const apiKey = SERVER_COLUMN_TO_API[sortBy[0].key]
  if (!apiKey) return {}
  return { sort_by: apiKey, sort_order: sortBy[0].order }
}
