/** Feste KPI-Kachel-IDs (sync mit backend dashboard_widget_config). */
export const KPI_TILE_BODY_FAT = 'body_fat'
export const KPI_TILE_AVG_KCAL = 'avg_kcal'

export const REF_TILE_PREFIX = 'ref:'

/**
 * @param {Record<string, unknown> | undefined} config
 * @returns {string[] | undefined} undefined = automatische Kachelwahl (Legacy)
 */
export function kpiTileOrderFromConfig(config) {
  if (!config || !Object.prototype.hasOwnProperty.call(config, 'tiles')) return undefined
  const raw = config.tiles
  if (!Array.isArray(raw)) return undefined
  /** @type {string[]} */
  const ids = []
  for (const item of raw) {
    const id = typeof item === 'string' ? item : item && item.id
    if (typeof id === 'string' && id.trim()) ids.push(id.trim())
  }
  return ids.slice(0, 9)
}
