import { describe, expect, test } from "bun:test";
import {
  ACCENT_COOKIE,
  renderThemeCss,
  renderThemeRuntime,
  resolveAccentName,
  resolveThemeConfig,
} from "../src/theme.ts";

describe("theme accents", () => {
  test("uses the configured named palette as the default accent", () => {
    const theme = resolveThemeConfig({ palette: "rose", default: "light" });

    expect(theme.defaultAccent).toBe("rose");
    expect(resolveAccentName(undefined, theme)).toBe("rose");
  });

  test("validates the accent cookie against enabled palettes", () => {
    const theme = resolveThemeConfig({
      palette: "rose",
      accent: { options: ["rose", "emerald"] },
    });

    expect(resolveAccentName("emerald", theme)).toBe("emerald");
    expect(resolveAccentName("blue", theme)).toBe("rose");
    expect(resolveAccentName("not-real", theme)).toBe("rose");
  });

  test("generates complete palette selectors for light and dark themes", () => {
    const css = renderThemeCss(resolveThemeConfig({ palette: "rose" }));

    expect(css).toContain('[data-theme="light"][data-accent="emerald"]');
    expect(css).toContain('[data-theme="dark"][data-accent="emerald"]');
    expect(css).toContain("--wire-color-primary-soft:");
    expect(css).toContain("--wire-color-primary-muted:");
    expect(css).toContain("--wire-color-primary-text:");
    expect(css).toContain("--wire-color-on-primary:");
    expect(css).toContain("--wire-color-secondary-soft:");
  });

  test("runtime persists accents with cookies and never writes inline tokens", () => {
    const runtime = renderThemeRuntime(resolveThemeConfig({ palette: "rose" }));

    expect(runtime).toContain(ACCENT_COOKIE);
    expect(runtime).toContain('el.setAttribute("data-accent",name)');
    expect(runtime).not.toContain("localStorage");
    expect(runtime).not.toContain("style.setProperty");
  });
});
test("live document accent has priority over cookie and default", () => {
  const runtime = renderThemeRuntime(
    resolveThemeConfig({
      palette: "rose",
      default: "light",
      accent: {
        default: "rose",
        options: ["rose", "emerald", "cyan"],
      },
    }),
  );

  expect(runtime).toContain(
    'el.getAttribute("data-accent")||readCookie(ACCENT_COOKIE)||DEFAULT_ACCENT',
  );

  expect(runtime).toContain(
    'el.getAttribute("data-theme")||readCookie(THEME_COOKIE)||DEFAULT_THEME',
  );
});
