import { describe, expect, it } from "vitest"
import {
  cardRegistry,
  elementDefaultStyle,
  elementRegistry,
  resolveCardPath,
  resolveElementPath,
} from "@/engine/registry"
import { CardElementMetadataMap, DisplayCardMetadataMap } from "@/definitions"

describe("elementRegistry", () => {
  it("covers every ElementType from the metadata map", () => {
    for (const key of Object.keys(CardElementMetadataMap)) {
      expect(
        (elementRegistry as unknown as Record<string, unknown>)[key],
      ).toBeDefined()
    }
  })

  it("declares a default style for every type", () => {
    for (const key of Object.keys(CardElementMetadataMap)) {
      expect(elementDefaultStyle[key as keyof typeof elementDefaultStyle]).toBeTruthy()
    }
  })
})

describe("resolveElementPath", () => {
  it("returns the registered path for an exact (type, style) match", () => {
    expect(resolveElementPath("input.text")).toBe("input/TextInput")
    expect(resolveElementPath("display.image")).toBe("display/ImageDisplay")
    expect(resolveElementPath("picker.date-picker")).toBe("picker/DatePicker")
  })

  it("falls back to the default style when style is omitted", () => {
    expect(resolveElementPath("input")).toBe("input/TextInput")
    expect(resolveElementPath("boolean")).toBe("boolean/CheckboxBoolean")
  })

  it("returns null for unknown type / style", () => {
    expect(resolveElementPath("bogus")).toBeNull()
    expect(resolveElementPath("input.bogus")).toBeNull()
  })

  it("accepts the `buttongroup` v2 alias", () => {
    expect(resolveElementPath("action.buttongroup")).toBe(
      "action/ButtonGroupAction",
    )
    expect(resolveElementPath("action.button-group")).toBe(
      "action/ButtonGroupAction",
    )
  })
})

describe("cardRegistry", () => {
  it("covers every cardType from the metadata map", () => {
    for (const key of Object.keys(DisplayCardMetadataMap)) {
      expect((cardRegistry as Record<string, unknown>)[key]).toBeDefined()
    }
  })
})

describe("resolveCardPath", () => {
  it("returns the registered path", () => {
    expect(resolveCardPath("element-list")).toBe("ElementListCard")
    expect(resolveCardPath("datatable")).toBe("DataTableCard")
  })

  it("returns null for unknown cardType", () => {
    expect(resolveCardPath("bogus")).toBeNull()
  })
})
