// exercise-colors.jsx — palette + helpers for per-exercise / per-set color cues.
//
// Honors RJ's color preferences: no pink, dark backgrounds compatible.
// Hues are picked for maximum at-a-glance discrimination:
//   A · cobalt blue   B · crimson red   C · emerald green
//   D · violet        E · cyan          F · amber
//
// Two layers of color identity per row:
//   1. Exercise hue   — drives a 3px left accent bar on every row in
//      that exercise. Same hue across all the rows for that exercise.
//   2. Set tonal step — drives a faint tinted row background that gets
//      progressively darker for later sets. Combined with the colored
//      bar, you can tell "row 3 of exercise B" at a glance.
//
// The accent bar lives on the OUTER row container, so when a row wraps
// (extras panel expanded, multi-setting equipment, etc.) the bar spans
// every visual line of that set — solves the wrap case.

const EX_PALETTE_LIGHT = [
  '#2563EB', // A — cobalt blue
  '#DC2626', // B — crimson red
  '#059669', // C — emerald green
  '#7C3AED', // D — violet
  '#0891B2', // E — cyan
  '#CA8A04', // F — amber
];
const EX_PALETTE_DARK = [
  '#60A5FA', // A — sky blue
  '#F87171', // B — coral red
  '#34D399', // C — mint green
  '#A78BFA', // D — light violet
  '#22D3EE', // E — bright cyan
  '#FACC15', // F — gold
];

function exerciseColor(theme, exIdx) {
  const palette = theme.isDark ? EX_PALETTE_DARK : EX_PALETTE_LIGHT;
  return palette[exIdx % palette.length];
}

// Hex (#RRGGBB) → rgba(r,g,b,a). Standalone — does not depend on hexToRgba
// in screens-1.jsx because exercise-colors loads before that module.
function exRgba(hex, alpha) {
  if (!hex || !hex.startsWith('#') || hex.length !== 7) return `rgba(0,0,0,${alpha})`;
  const r = parseInt(hex.slice(1, 3), 16);
  const g = parseInt(hex.slice(3, 5), 16);
  const b = parseInt(hex.slice(5, 7), 16);
  return `rgba(${r},${g},${b},${alpha})`;
}

// Tonal step per set within an exercise.
// Set 0 is faintest; each subsequent set steps up in saturation.
// Capped so big circuits (6+ sets) don't bake to opaque color.
function setTintAlpha(setIdx, isDark) {
  const base = isDark ? 0.08 : 0.06;
  const step = isDark ? 0.05 : 0.045;
  return Math.min(0.32, base + setIdx * step);
}

// Convenience — full row tint for a given (exIdx, setIdx) in the current theme.
function exerciseSetTint(theme, exIdx, setIdx) {
  return exRgba(exerciseColor(theme, exIdx), setTintAlpha(setIdx, theme.isDark));
}

// Slightly stronger tint for hover/active states or for the legend.
function exerciseChipBg(theme, exIdx) {
  return exRgba(exerciseColor(theme, exIdx), theme.isDark ? 0.18 : 0.12);
}

// Pick ink color that reads on top of a filled exercise swatch.
// For our palette, white reads on every hue in both themes.
function exerciseChipInk(/* theme, exIdx */) { return '#FFFFFF'; }

Object.assign(window, {
  EX_PALETTE_LIGHT, EX_PALETTE_DARK,
  exerciseColor, exerciseSetTint, exerciseChipBg, exerciseChipInk,
  setTintAlpha, exRgba,
});
