import { expect, test } from "bun:test";
import { habitProgressColor, themedHabitShade } from "./habit-palette";
import { HABIT_PALETTES } from "../components/design-system/HabitColorPicker";
import { shade } from "../habits/calendar";
const luminance = (hex: string) => [1, 3, 5].map(i => parseInt(hex.slice(i, i+2),16)/255).map(v => v <= .04045 ? v/12.92 : ((v+.055)/1.055)**2.4).reduce((n,v,i) => n+v*[.2126,.7152,.0722][i]!,0);
test("all suggested and extreme custom colors have eight distinct ordered steps in both themes", () => {
  for (const base of [...HABIT_PALETTES.flatMap(p=>[...p.colors]), '#ffffff','#000000','#ffff00','#0000ff','#ff0000','#00ff00']) {
    for (const theme of ['light','dark'] as const) {
      const colors = Array.from({length:8},(_,i)=>habitProgressColor(base,i/7,theme));
      expect(new Set(colors).size).toBe(8);
      const levels=colors.map(luminance);
      for(let i=1;i<8;i++) expect(theme==='dark' ? levels[i]! > levels[i-1]! : levels[i]! < levels[i-1]!).toBe(true);
      const empty=luminance(theme==='dark'?'#39413b':'#eeeeee');
      expect(theme==='dark' ? levels[0]! > empty : levels[0]! < empty).toBe(true);
    }
  }
});
test("server shade quantization maps to the same ramp as picker preview, including bright custom colors", () => {
  for (const base of ['#426582','#ffffff','#ffff00','#000000']) for (const theme of ['light','dark'] as const) {
    const colors=Array.from({length:8},(_,i)=>shade((i+1)/8,8,{mainColor:base} as any,true).color);
    const mapped=colors.map(c=>themedHabitShade(c,base,theme));
    expect(mapped[0]).toBe(habitProgressColor(base,0,theme));
    expect(mapped[7]).toBe(habitProgressColor(base,1,theme));
    expect(new Set(mapped).size).toBe(8);
  }
});
