/**
 * Search overlay tests — open/close, input, results display, grouping.
 */

describe("Search overlay", () => {
  it("should open via Ctrl+Shift+F", async () => {
    await browser.keys(["Control", "Shift", "f"]);
    await browser.pause(400);

    const backdrop = await $(".overlay-backdrop");
    if (await backdrop.isExisting()) {
      const display = await backdrop.getCSSProperty("display");
      expect(display.value).not.toBe("none");
    }
  });

  it("should focus the search input on open", async () => {
    const input = await $(".search-input");
    if (await input.isExisting()) {
      const focused = await browser.execute(() => {
        return document.activeElement?.classList.contains("search-input");
      });
      expect(focused).toBe(true);
    }
  });

  it("should show the overlay panel", async () => {
    const panel = await $(".overlay-panel");
    if (await panel.isExisting()) {
      expect(await panel.isDisplayed()).toBe(true);
    }
  });

  it("should show 'No results' for non-matching query", async () => {
    const input = await $(".search-input");
    if (!(await input.isExisting())) return;

    await input.setValue("zzz_nonexistent_query_zzz");
    await browser.pause(500); // debounce 300ms + render

    const noResults = await $(".no-results");
    if (await noResults.isExisting()) {
      expect(await noResults.isDisplayed()).toBe(true);
    }
  });

  it("should show Esc hint badge", async () => {
    const hint = await $(".esc-hint");
    if (await hint.isExisting()) {
      expect(await hint.getText()).toBe("Esc");
    }
  });

  it("should show loading indicator while searching", async () => {
    // Loading dot appears briefly during search
    const dot = await $(".loading-dot");
    // May or may not be visible depending on timing — just verify class exists
    expect(dot).toBeDefined();
  });

  it("should have grouped results structure", async () => {
    // Results list and group labels exist in the DOM structure
    const resultsList = await $(".results-list");
    const groupLabel = await $(".group-label");
    // These may not be visible if no results, but structure should exist
    expect(resultsList).toBeDefined();
    expect(groupLabel).toBeDefined();
  });

  it("should close on Escape key", async () => {
    await browser.keys("Escape");
    await browser.pause(300);

    const backdrop = await $(".overlay-backdrop");
    if (await backdrop.isExisting()) {
      const display = await backdrop.getCSSProperty("display");
      expect(display.value).toBe("none");
    }
  });
});
