import { test, expect } from "bun:test";
import {
  resolveThemeConfig,
  resolveThemeName,
  renderThemeCss,
  renderThemeRuntime,
} from "../src/index.ts";

test("resolveThemeConfig merges user tokens over built-in light/dark", () => {
  const t = resolveThemeConfig({ default: "dark", themes: { dark: { "color-primary": "#abc" } } });
  expect(t.default).toBe("dark");
  expect(t.names).toContain("light");
  expect(t.names).toContain("dark");
  expect(t.themes.dark["color-primary"]).toBe("#abc"); // overridden
  expect(t.themes.dark["color-bg"]).toBeDefined(); // built-in kept
});

test("resolveThemeName validates against configured names", () => {
  const t = resolveThemeConfig();
  expect(resolveThemeName("light", t)).toBe("light");
  expect(resolveThemeName("nonsense", t)).toBe(t.default);
  expect(resolveThemeName(undefined, t)).toBe(t.default);
});

test("renderThemeCss emits :root + per-theme blocks and --wire-* vars", () => {
  const t = resolveThemeConfig({
    default: "dark",
    themes: { dark: { "color-primary": "#6c8cff" } },
  });
  const css = renderThemeCss(t);
  expect(css).toContain(":root{");
  expect(css).toContain('[data-theme="dark"]{');
  expect(css).toContain("--wire-color-primary:#6c8cff");
  expect(css).toContain("color-scheme:dark"); // reserved token → native property
});

test("renderThemeRuntime bakes the theme names for cycling", () => {
  const t = resolveThemeConfig();
  const js = renderThemeRuntime(t);
  expect(js).toContain("data-wire-theme-toggle");
  expect(js).toContain(JSON.stringify(t.names));
  expect(js).toContain('addEventListener("wrnexus:navigated"');
});
