/**
 * ECharts helpers: color palettes and create/dispose wrappers.
 */

export const TAB10_COLORS = [
  '#38bdf8', '#22c55e', '#f59e0b', '#ec4899',
  '#a78bfa', '#fb923c', '#34d399', '#f472b6',
  '#60a5fa', '#fbbf24',
]

/**
 * Map a speed value to a coolwarm hex colour.
 * t in [0,1]: 0 = blue (fast), 1 = red (slow)
 */
export function coolwarmColor(t) {
  const r = t < .5 ? Math.round(t * 2 * 140) : Math.round(140 + (t - .5) * 2 * 115)
  const g = t < .5 ? Math.round(50 + t * 2 * 100) : Math.round(150 - (t - .5) * 2 * 150)
  const b = t < .5 ? Math.round(200 - t * 2 * 100) : Math.round(100 - (t - .5) * 2 * 100)
  return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`
}

/**
 * Create an ECharts instance on a container element.
 * @param {HTMLElement} container
 * @returns {object} ECharts instance
 */
export function createChart(container) {
  return echarts.init(container, 'dark')
}

/**
 * Dispose an ECharts instance on a container if one exists.
 * @param {object|null} chartInstance - The echarts instance to dispose
 * @returns {null}
 */
export function disposeChart(chartInstance) {
  if (chartInstance) {
    chartInstance.dispose()
  }
  return null
}
