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

describe("PillSelectOption (props-based composition)", () => {
  it("mounts without error", () => {
    const wrapper = mount(PillSelectOption, { props: { label: "Tags" } })
    expect(wrapper.exists()).toBe(true)
  })

  it("renders the label via FieldFrame", () => {
    const wrapper = mount(PillSelectOption, {
      props: { label: "Custom label" },
    })
    expect(wrapper.text()).toContain("Custom label")
  })

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

  it("renders pills for each option", () => {
    const wrapper = mount(PillSelectOption, {
      props: {
        label: "Tags",
        options: [
          { label: "Vue", value: "vue" },
          { label: "React", value: "react" },
        ],
      },
    })
    expect(wrapper.text()).toContain("Vue")
    expect(wrapper.text()).toContain("React")
  })

  it("marks selected values as active", () => {
    const wrapper = mount(PillSelectOption, {
      props: {
        label: "Tags",
        modelValue: ["vue"],
        options: [
          { label: "Vue", value: "vue" },
          { label: "React", value: "react" },
        ],
      },
    })
    const actives = wrapper
      .findAll("[data-active]")
      .filter((c) => c.attributes("data-active") === "true")
    expect(actives.length).toBeGreaterThan(0)
  })

  it("emits update:modelValue on toggle", async () => {
    const wrapper = mount(PillSelectOption, {
      props: {
        label: "Tags",
        options: [
          { label: "Vue", value: "vue" },
          { label: "React", value: "react" },
        ],
      },
    })
    await wrapper.findAll("[data-active]")[0]!.trigger("click")
    expect(wrapper.emitted("update:modelValue")).toBeTruthy()
  })

  it("renders errors when provided", () => {
    const wrapper = mount(PillSelectOption, {
      props: { label: "X", errors: ["Pick at least one"] },
    })
    expect(wrapper.find("[data-testid='field-frame-errors']").exists()).toBe(
      true,
    )
  })

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