import { describe, expect, it } from 'vitest';
import { derivedBorder, resolveBannerColors } from './bannerColors';
import type { BannerColors } from './model';

const red = { bg: '#aa0000', fg: '#ffffff', link: '#ffdd00' };
const darkRed = { bg: '#330000', fg: '#eeeeee', link: '#ffcc00' };

describe('resolveBannerColors', () => {
  it('returns null when the campaign has no override (neutral tokens)', () => {
    const none: BannerColors = { all: null, dark: null };
    expect(resolveBannerColors(none, 'light')).toBeNull();
    expect(resolveBannerColors(none, 'dark')).toBeNull();
  });

  it('applies the all-themes set on both themes when no dark override', () => {
    const c: BannerColors = { all: red, dark: null };
    expect(resolveBannerColors(c, 'light')).toMatchObject({ bg: red.bg, fg: red.fg, link: red.link });
    expect(resolveBannerColors(c, 'dark')).toMatchObject({ bg: red.bg, fg: red.fg, link: red.link });
  });

  it('prefers the dark override on the dark theme, and the all set on light', () => {
    const c: BannerColors = { all: red, dark: darkRed };
    expect(resolveBannerColors(c, 'light')).toMatchObject({ bg: red.bg });
    expect(resolveBannerColors(c, 'dark')).toMatchObject({ bg: darkRed.bg });
  });

  it('supports a dark-only override (light keeps the neutral tokens)', () => {
    const c: BannerColors = { all: null, dark: darkRed };
    expect(resolveBannerColors(c, 'light')).toBeNull();
    expect(resolveBannerColors(c, 'dark')).toMatchObject({ bg: darkRed.bg });
  });

  it('derives the border from the resolved background', () => {
    const c: BannerColors = { all: red, dark: null };
    expect(resolveBannerColors(c, 'light')?.border).toBe(derivedBorder(red.bg));
  });
});

describe('derivedBorder', () => {
  it('darkens a light background and lightens a dark one', () => {
    expect(derivedBorder('#ffffff')).toBe('#dbdbdb');
    expect(derivedBorder('#000000')).toBe('#242424');
  });

  it('returns a malformed colour unchanged', () => {
    expect(derivedBorder('nope')).toBe('nope');
  });
});
