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

test("Labs device preferences are read-only for project and checkout scopes", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("labs-demo");
  const other = await t3.project("labs-checkout");
  for (const cwd of [project, other]) {
    await runPromise(run("git", ["remote", "add", "origin", "https://github.com/example/labs-demo.git"], { cwd }));
  }
  const thread = await t3.thread({ project, title: "Sidebar example", messages: ["hello"] });
  await t3.thread({ project: other, title: "Second checkout", messages: ["hello"] });
  await browser.setViewport({ width: 1440, height: 1000 });
  await t3.open(thread);
  await t3.open("/settings/labs");
  const capture = async (label: string) => {
    await browser.evaluate(async () => {
      await Promise.all(document.getAnimations().map(animation => animation.finished.catch(() => {})));
    });
    await app.screenshot(label);
  };
  const boring = screen.getByRole("switch", "Boring sidebar (group by project)");
  const multiline = screen.getByRole("switch", "Two-line folder rows");
  await boring.tap();
  await multiline.waitFor();
  await multiline.tap();
  await browser.locator('[aria-label="Two-line folder rows"][aria-checked="true"]').waitFor();
  await capture("all-projects-editable");
  const results: Array<{ scope: string; readOnly: boolean; explanation: boolean }> = [];
  const inspect = async (scope: string) => {
    for (const label of ["Boring sidebar (group by project)", "Two-line folder rows"]) {
      const readOnly = await browser.evaluate((label) => {
        const control = document.querySelector(`[aria-label="${label}"]`);
        return control !== null && control.closest("[inert]") !== null;
      }, label);
      let explanation = false;
      if (readOnly) {
        await browser.locator(`span:has(> [inert] [aria-label="${label}"])`).hover();
        await screen.getByText("Saved on this device. Select All projects to change it.", { exact: true }).waitFor();
        explanation = true;
      } else {
        await screen.getByRole("switch", label).tap();
        await screen.getByText("Setting not saved", { exact: true }).waitFor();
      }
      await capture(`${scope}-${label.startsWith("Boring") ? "boring" : "multiline"}`);
      results.push({ scope: `${scope}-${label.startsWith("Boring") ? "boring" : "multiline"}`, readOnly, explanation });
      if (!readOnly) {
        await screen.getByRole("button", "Dismiss notification").tap();
        await screen.getByText("Setting not saved", { exact: true }).waitFor({ state: "hidden" });
      }
      await screen.getByRole("heading", "Beta").hover();
      if (readOnly) await screen.getByText("Saved on this device. Select All projects to change it.", { exact: true }).waitFor({ state: "hidden" });
    }
  };
  await screen.getByRole("button", "Project scope: All projects").tap();
  await screen.getByRole("menuitemradio").filter({ hasText: "labs-demo" }).tap();
  await inspect("project");
  await screen.getByRole("button", "Checkout scope: All checkouts").tap();
  await screen.getByRole("menuitemradio").filter({ hasText: "labs-checkout" }).tap();
  await inspect("checkout");
  await screen.getByRole("button", /^Project scope:/).tap();
  await screen.getByRole("menuitemradio", "All projects").tap();
  await multiline.tap();
  await boring.tap();
  await boring.tap();
  await multiline.tap();
  await browser.locator('[aria-label="Two-line folder rows"][aria-checked="true"]').waitFor();
  await capture("environment-editable");
  await expect(results).toEqual([
    { scope: "project-boring", readOnly: true, explanation: true },
    { scope: "project-multiline", readOnly: true, explanation: true },
    { scope: "checkout-boring", readOnly: true, explanation: true },
    { scope: "checkout-multiline", readOnly: true, explanation: true },
  ]);
});
