export const vibrationMetricOptions = [
  // 位移专属看板本轮只保留一个业务指标，避免前端继续暴露无效的多指标切换入口。
  { field: 'vibrationDisplacement', label: '位移', unit: 'um', color: '#5b8ff9' }
] as const;

export const vibrationMetricMap = Object.fromEntries(vibrationMetricOptions.map((item) => [item.field, item]));

export const vibrationHeatmapColors = ['#0f766e', '#0ea5a4', '#38bdf8', '#fde68a', '#fb923c', '#ef4444'];

export const getVibrationMetricOption = (field?: string) => vibrationMetricMap[field || ''] || vibrationMetricOptions[0];

export const toNumber = (value: number | string | undefined, fallback = 0) => {
  const numberValue = Number(value);
  return Number.isFinite(numberValue) ? numberValue : fallback;
};

export const formatMetricValue = (value: number | string | undefined, digits = 2) => {
  if (value === null || value === undefined || value === '') {
    return '--';
  }
  return toNumber(value).toFixed(digits);
};

export const formatPercentValue = (value: number | string | undefined, digits = 1) => `${(toNumber(value) * 100).toFixed(digits)}%`;

export const buildCategoryDataZoom = (length: number, axis: 'x' | 'y' = 'y') => {
  if (length <= 8) {
    return [];
  }
  const start = Math.max(0, 100 - Math.round((8 / length) * 100));
  if (axis === 'x') {
    return [
      { type: 'inside', xAxisIndex: 0, filterMode: 'none' },
      { type: 'slider', xAxisIndex: 0, filterMode: 'none', start, end: 100, height: 14, bottom: 4 }
    ];
  }
  return [
    { type: 'inside', yAxisIndex: 0, filterMode: 'none' },
    { type: 'slider', yAxisIndex: 0, filterMode: 'none', start, end: 100, width: 12, right: 4 }
  ];
};

export const formatDateTime = (date: Date) => {
  const p = (n: number) => n.toString().padStart(2, '0');
  return `${date.getFullYear()}-${p(date.getMonth() + 1)}-${p(date.getDate())} ${p(date.getHours())}:${p(date.getMinutes())}:${p(date.getSeconds())}`;
};

export const createDefaultTimeRange = () => {
  const end = new Date();
  const start = new Date(end.getTime() - 24 * 3600 * 1000);
  return [formatDateTime(start), formatDateTime(end)];
};
