/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

const config = '[remote "origin"]\n url = https://github.com/team/orange-studio.git\n fetch = +refs/heads/*:refs/remotes/origin/*\n';
const logo = '<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64"><rect width="64" height="64" rx="12" fill="#f97316"/><path d="M12 14h40v10H37v28H27V24H12z" fill="white"/></svg>';

test("An iconless checkout uses its project image in thread and draft rows", WEB, async ({t3, app, browser, screen}) => {
  const first = await t3.project("First checkout", {"brand/logo.svg": logo, ".git/config": config});
  await t3.open("/settings/projects");
  await screen.getByRole("button", "Project scope: All projects").tap();
  await screen.getByRole("menuitemradio").filter({hasText: "First checkout"}).tap();
  await screen.getByRole("button", "Choose a project icon file", {exact: true}).tap();
  await screen.getByRole("option").filter({hasText: "brand/logo.svg"}).tap();
  await screen.getByText("brand/logo.svg", {exact: true}).waitFor();
  await app.screenshot("selected-group-image");
  const second = await t3.project("Second checkout", {"brand/logo.svg": logo, ".git/config": config});
  const thread = await t3.thread({project: second, title: "Checkout icon proof", messages: ["first"]});
  await t3.open("/settings/labs");
  await screen.getByRole("switch", "Boring sidebar (group by project)").check();
  await screen.getByRole("switch", "Two-line folder rows").check();
  await t3.open(thread);
  await screen.getByText("first fixture turn complete", {exact: false}).waitFor();
  await browser.locator('[data-testid="sidebar-row-folder"]').waitFor();
  await screen.getByText("Syncing messages...", {exact:false}).waitFor({state:"hidden"});
  await app.screenshot("group-folder-image");
  await t3.open("/settings/labs");
  await screen.getByRole("switch", "Boring sidebar (group by project)").uncheck();
  await t3.open(thread);
  const row = browser.locator('[data-testid="sidebar-row-card"]').filter({hasText: "Checkout icon proof"});
  await row.waitFor();
  await screen.getByText("Syncing messages...", {exact:false}).waitFor({state:"hidden"});
  await app.screenshot("sidebar-thread");
  const threadHasImage = await browser.locator('[data-testid="sidebar-row-card"] img:not([hidden])').count();
  await row.hover();
  await browser.locator('[data-slot="tooltip-popup"]').waitFor();
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().filter(a => Number.isFinite(a.effect?.getComputedTiming().endTime)).map(a => a.finished.catch(() => {}))); return null; });
  await app.screenshot("thread-hover-card");
  const hoverHasImage = await browser.locator('[data-slot="tooltip-popup"] img:not([hidden])').count();
  await browser.keyboard.press("Escape");
  await screen.getByRole("combobox", "Search threads").fill("Checkout icon proof");
  const results = screen.getByRole("listbox", "Thread search results");
  await results.waitFor();
  await app.screenshot("thread-search");
  const searchHasImage = await browser.locator('[role="listbox"][aria-label="Thread search results"] img:not([hidden])').count();
  await browser.keyboard.press("Escape");
  await screen.getByRole("combobox", "Search threads").fill("");
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("Control+Shift+n");
  await browser.locator("[data-draft-project-trigger]").waitFor();
  await screen.getByRole("textbox").fill("Draft for the second checkout");
  await browser.evaluate(async () => { await new Promise(resolve => setTimeout(resolve, 1000)); return null; });
  await t3.open(thread);
  await row.waitFor();
  await browser.locator('[data-sidebar-row^="draft"]').waitFor();
  await screen.getByText("Syncing messages...", {exact:false}).waitFor({state:"hidden"});
  await app.screenshot("sidebar-draft");
  const draftHasImage = await browser.locator('[data-sidebar-row^="draft"] img:not([hidden])').count();
  expect({threadHasImage, hoverHasImage, searchHasImage, draftHasImage}).toEqual({threadHasImage:1, hoverHasImage:1, searchHasImage:1, draftHasImage:1});
});
