import { describe, expect, it } from "vitest";
import { DEFAULT_THEME, headerStyle, readableTextOn, themeToCssVars } from "../theme";

describe("themeToCssVars", () => {
  it("maps a seller's theme onto CSS custom properties", () => {
    const vars = themeToCssVars({
      primary_color: "#123456",
      accent_color: "#abcdef",
      font: "serif",
      corner_radius: "lg",
    }) as Record<string, string>;

    expect(vars["--sf-primary"]).toBe("#123456");
    expect(vars["--sf-accent"]).toBe("#abcdef");
    expect(vars["--sf-radius"]).toBe("14px");
    expect(vars["--sf-font"]).toContain("serif");
  });

  it("falls back to the defaults for values that are not hex colours", () => {
    const vars = themeToCssVars({
      primary_color: "rebeccapurple",
      // A CSS injection attempt must not reach the style attribute.
      background_color: "red; background-image: url(javascript:alert(1))",
    }) as Record<string, string>;

    expect(vars["--sf-primary"]).toBe(DEFAULT_THEME.primary_color);
    expect(vars["--sf-bg"]).toBe(DEFAULT_THEME.background_color);
  });

  it("falls back for an unknown font or radius", () => {
    const vars = themeToCssVars({
      font: "comic" as never,
      corner_radius: "huge" as never,
    }) as Record<string, string>;

    expect(vars["--sf-font"]).toContain("sans-serif");
    expect(vars["--sf-radius"]).toBe("6px");
  });

  it("handles a missing theme entirely", () => {
    const vars = themeToCssVars(null) as Record<string, string>;
    expect(vars["--sf-primary"]).toBe(DEFAULT_THEME.primary_color);
  });
});

describe("readableTextOn", () => {
  it("picks dark text on light backgrounds and light text on dark", () => {
    expect(readableTextOn("#ffffff")).toBe("#0f172a");
    expect(readableTextOn("#f5f1ea")).toBe("#0f172a");
    expect(readableTextOn("#000000")).toBe("#ffffff");
    expect(readableTextOn("#065f46")).toBe("#ffffff");
  });

  it("expands three-digit hex", () => {
    expect(readableTextOn("#fff")).toBe("#0f172a");
    expect(readableTextOn("#000")).toBe("#ffffff");
  });

  it("defaults to white for junk input", () => {
    expect(readableTextOn("not-a-colour")).toBe("#ffffff");
  });
});

describe("headerStyle", () => {
  it("only accepts known styles", () => {
    expect(headerStyle({ header_style: "transparent" })).toBe("transparent");
    expect(headerStyle({ header_style: "minimal" })).toBe("minimal");
    expect(headerStyle({ header_style: "wild" as never })).toBe("solid");
    expect(headerStyle(undefined)).toBe("solid");
  });
});
