import { describe, expect, it, vi } from "vitest"
import { resolveTheme, themePresets, themeToCSSVars } from "@/lib/theme"

describe("resolveTheme", () => {
  it("returns default for undefined", () => {
    const t = resolveTheme(undefined)
    expect(t.name).toBe("default")
  })

  it("returns default for 'default'", () => {
    const t = resolveTheme("default")
    expect(t.name).toBe("default")
  })

  it("returns kanagawa preset by name", () => {
    const t = resolveTheme("kanagawa")
    expect(t.name).toBe("kanagawa")
    expect(t.colors?.primary).toBe("#7fb4ca")
  })

  it("returns unknown preset with warning", () => {
    const warn = vi.spyOn(console, "warn").mockImplementation(() => {})
    const t = resolveTheme("nonexistent")
    expect(t.name).toBe("default")
    expect(warn).toHaveBeenCalledWith(expect.stringContaining("nonexistent"))
    warn.mockRestore()
  })

  it("passes through config object", () => {
    const config = { name: "custom", colors: { primary: "#ff0" } }
    const t = resolveTheme(config)
    expect(t).toBe(config)
  })
})

describe("themeToCSSVars", () => {
  it("returns empty for theme without colors", () => {
    const vars = themeToCSSVars({ name: "minimal" })
    expect(vars).toEqual({})
  })

  it("maps colors to --ui-* vars", () => {
    const vars = themeToCSSVars({
      name: "test",
      colors: {
        primary: "#111",
        success: "#222",
        error: "#333",
        text: { main: "#aaa", muted: "#bbb", highlight: "#ccc" },
        background: { elevated: "#000" },
        border: "#444",
      },
    })
    expect(vars).toEqual({
      "--ui-primary": "#111",
      "--ui-success": "#222",
      "--ui-error": "#333",
      "--ui-text-main": "#aaa",
      "--ui-text-muted": "#bbb",
      "--ui-text-highlight": "#ccc",
      "--ui-bg-elevated": "#000",
      "--ui-border": "#444",
    })
  })

  it("maps syntax colors to --code-* vars", () => {
    const vars = themeToCSSVars({
      name: "code-test",
      colors: {},
      syntax: {
        keyword: "#a",
        string: "#b",
        comment: "#c",
        number: "#d",
        function: "#e",
        type: "#f",
      },
    })
    expect(vars).toMatchInlineSnapshot(`
      {
        "--code-comment": "#c",
        "--code-function": "#e",
        "--code-keyword": "#a",
        "--code-number": "#d",
        "--code-string": "#b",
        "--code-type": "#f",
      }
    `)
  })
})

describe("themePresets", () => {
  it("has default and kanagawa", () => {
    expect(themePresets.default).toBeDefined()
    expect(themePresets.kanagawa).toBeDefined()
  })

  it("kanagawa has all major sections", () => {
    const k = themePresets.kanagawa
    expect(k?.colors?.primary).toBeTruthy()
    expect(k?.colors?.text?.main).toBeTruthy()
    expect(k?.colors?.background?.elevated).toBeTruthy()
    expect(k?.syntax?.keyword).toBeTruthy()
    expect(k?.syntax?.string).toBeTruthy()
  })
})
