import { describe, expect, it } from "vitest"
import { applyLegacyRemaps } from "@/engine/legacyPropRemap"

describe("applyLegacyRemaps", () => {
  describe("configuration-block prop renames", () => {
    it("renames selectOptions → nestedElements", () => {
      const result = applyLegacyRemaps("configuration-block.elements-block", {
        selectOptions: [{ id: "a" }],
        label: "Block",
      })
      expect(result.nestedElements).toEqual([{ id: "a" }])
      expect(result.selectOptions).toBeUndefined()
    })

    it("renames triggerButtonLabel → triggerLabel", () => {
      const result = applyLegacyRemaps("configuration-block.dialog", {
        triggerButtonLabel: "Open",
      })
      expect(result.triggerLabel).toBe("Open")
      expect(result.triggerButtonLabel).toBeUndefined()
    })

    it("renames dialogTitle / modalTitle → title", () => {
      expect(
        applyLegacyRemaps("configuration-block.dialog", {
          dialogTitle: "X",
        }).title,
      ).toBe("X")
      expect(
        applyLegacyRemaps("configuration-block.modal", {
          modalTitle: "Y",
        }).title,
      ).toBe("Y")
    })

    it("renames addOptionButtonLabel → addButtonLabel", () => {
      const result = applyLegacyRemaps("configuration-block.repeatable", {
        addOptionButtonLabel: "Add row",
      })
      expect(result.addButtonLabel).toBe("Add row")
    })

    it("does not overwrite a key the consumer already set in the new name", () => {
      const result = applyLegacyRemaps("configuration-block.dialog", {
        triggerLabel: "explicit",
        triggerButtonLabel: "legacy",
      })
      expect(result.triggerLabel).toBe("explicit")
    })
  })

  describe("action variant aliases", () => {
    it.each([
      ["primary", "default"],
      ["danger", "destructive"],
      ["outline-primary", "outline"],
      ["outline-danger", "outline"],
      ["btn-success", "success"],
      ["light", "ghost"],
      ["dark", "default"],
    ])("aliases variant=%s → %s", (legacy, normalised) => {
      const result = applyLegacyRemaps("action.button", { variant: legacy })
      expect(result.variant).toBe(normalised)
    })

    it("passes unknown variants through unchanged", () => {
      const result = applyLegacyRemaps("action.button", {
        variant: "custom-thing",
      })
      expect(result.variant).toBe("custom-thing")
    })

    it("leaves non-string variant values alone", () => {
      const result = applyLegacyRemaps("action.button", { variant: 42 })
      expect(result.variant).toBe(42)
    })
  })

  describe("display prop renames", () => {
    it("renames icon → iconName", () => {
      const result = applyLegacyRemaps("display.icon", { icon: "check" })
      expect(result.iconName).toBe("check")
      expect(result.icon).toBeUndefined()
    })
  })

  describe("pass-through", () => {
    it("returns a new object without changes for unknown categories", () => {
      const input = { label: "x", required: true }
      const result = applyLegacyRemaps("input.text", input)
      // Same shape, but a different reference is acceptable.
      expect(result).toEqual(input)
    })

    it("returns the input object reference when no remaps exist for the category", () => {
      const input = { label: "x" }
      const result = applyLegacyRemaps("input.text", input)
      expect(result).toBe(input)
    })

    it("passes through props that don't match any remap key", () => {
      const result = applyLegacyRemaps("configuration-block.dialog", {
        label: "Title",
        helpText: "Hint",
      })
      expect(result.label).toBe("Title")
      expect(result.helpText).toBe("Hint")
    })
  })

  describe("GridViewCard's gridLayout (legacy element refs)", () => {
    it("passes the gridLayout through (resolution happens in CardRenderer)", () => {
      // legacyPropRemap doesn't itself resolve grid layout element IDs —
      // that lives in CardRenderer. This test documents that the remap
      // doesn't accidentally strip the layout.
      const layout = {
        lg: [{ i: "el-1", x: 0, y: 0, w: 4, h: 4 }],
      }
      const result = applyLegacyRemaps("display.type", { gridLayout: layout })
      expect(result.gridLayout).toBe(layout)
    })
  })
})
