import { fireEvent, render, screen } from "@solidjs/testing-library";
import { describe, expect, it } from "vitest";
import { CommandPalette } from "../../../src/components/command-palette/index";
import { CommandPaletteRootPropsSchema, CommandPaletteMeta } from "../../../src/components/command-palette/command-palette.props";

describe("CommandPalette", () => {
  it("renders input and items", () => {
    render(() => (
      <CommandPalette>
        <CommandPalette.Input placeholder="Search commands..." />
        <CommandPalette.List>
          <CommandPalette.Item value="copy">Copy</CommandPalette.Item>
          <CommandPalette.Item value="paste">Paste</CommandPalette.Item>
        </CommandPalette.List>
      </CommandPalette>
    ));
    expect(screen.getByPlaceholderText("Search commands...")).toBeTruthy();
    expect(screen.getByText("Copy")).toBeTruthy();
    expect(screen.getByText("Paste")).toBeTruthy();
  });

  it("renders groups with headings", () => {
    render(() => (
      <CommandPalette>
        <CommandPalette.Input placeholder="Search..." />
        <CommandPalette.List>
          <CommandPalette.Group heading="Actions">
            <CommandPalette.Item value="save">Save</CommandPalette.Item>
          </CommandPalette.Group>
        </CommandPalette.List>
      </CommandPalette>
    ));
    expect(screen.getByText("Actions")).toBeTruthy();
    expect(screen.getByText("Save")).toBeTruthy();
  });

  it("calls onSelect when item activated", () => {
    let selected = "";
    render(() => (
      <CommandPalette onSelect={(v) => { selected = v; }}>
        <CommandPalette.Input />
        <CommandPalette.List>
          <CommandPalette.Item value="run">Run</CommandPalette.Item>
        </CommandPalette.List>
      </CommandPalette>
    ));
    fireEvent.click(screen.getByText("Run"));
    expect(selected).toBe("run");
  });

  it("schema validates", () => {
    expect(CommandPaletteRootPropsSchema.safeParse({ search: "test", loop: true, filter: false }).success).toBe(true);
  });

  it("meta has fields", () => {
    expect(CommandPaletteMeta.name).toBe("CommandPalette");
    expect(CommandPaletteMeta.parts).toContain("Root");
    expect(CommandPaletteMeta.parts).toContain("Input");
    expect(CommandPaletteMeta.parts).toContain("Item");
  });
});
