import { describe, expect, it } from "vitest"
import { mount } from "@vue/test-utils"
import IconToggleBoolean from "./IconToggleBoolean.vue"

describe("IconToggleBoolean (props-based composition)", () => {
  it("mounts and renders the label via FieldFrame", () => {
    const wrapper = mount(IconToggleBoolean, {
      props: { label: "Dark mode" },
    })
    expect(wrapper.text()).toContain("Dark mode")
  })

  it("hides the visible label when hideLabel is true", () => {
    const wrapper = mount(IconToggleBoolean, {
      props: { label: "Hidden", hideLabel: true },
    })
    expect(wrapper.find("[data-testid='field-frame-label-sr']").exists()).toBe(
      true,
    )
    expect(wrapper.find("[data-testid='field-frame-label']").exists()).toBe(
      false,
    )
  })

  it("renders a toggle button", () => {
    const wrapper = mount(IconToggleBoolean, { props: { label: "X" } })
    expect(wrapper.find("button").exists()).toBe(true)
  })

  it("emits update:modelValue when toggled", async () => {
    const wrapper = mount(IconToggleBoolean, {
      props: { label: "X", modelValue: false },
    })
    await wrapper.find("button").trigger("click")
    const emitted = wrapper.emitted("update:modelValue")
    expect(emitted).toBeTruthy()
    expect(emitted![0]).toEqual([true])
  })

  it("reflects modelValue=true via aria-pressed", () => {
    const wrapper = mount(IconToggleBoolean, {
      props: { label: "X", modelValue: true },
    })
    expect(wrapper.find("button").attributes("aria-pressed")).toBe("true")
  })

  it("respects disabled / required", () => {
    const wrapper = mount(IconToggleBoolean, {
      props: { label: "X", disabled: true, required: true },
    })
    expect(
      (wrapper.find("button").element as HTMLButtonElement).disabled,
    ).toBe(true)
  })

  it("renders helpText and errors", () => {
    const wrapper = mount(IconToggleBoolean, {
      props: { label: "X", helpText: "Hint", errors: ["Bad"] },
    })
    expect(wrapper.find("[data-testid='field-frame-help']").text()).toBe("Hint")
    expect(wrapper.find("[data-testid='field-frame-errors']").text()).toContain(
      "Bad",
    )
  })

  it("resolves trueIcon / falseIcon to SVGs", () => {
    const wrapper = mount(IconToggleBoolean, {
      props: {
        label: "Mode",
        trueIcon: "moon",
        falseIcon: "sun",
        modelValue: true,
      },
    })
    expect(wrapper.find("svg").exists()).toBe(true)
  })

  it("works without any props at all", () => {
    const wrapper = mount(IconToggleBoolean)
    expect(wrapper.find("button").exists()).toBe(true)
  })
})
