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

test("Valid When fields retain normal borders and resting shadows", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("valid-fields");
  await browser.emulateMedia({ colorScheme: "light" });
  await t3.open("/settings/keybindings");
  await screen.getByLabel("Search keybindings").fill("Command Palette");
  await screen.getByRole("button", "Edit when clause for Command Palette: Toggle", { exact: true }).tap();
  const field = screen.getByLabel("When expression", { exact: true });
  await field.fill("terminalFocus");
  await browser.evaluate(() => new Promise(resolve => setTimeout(resolve, 2000)));
  await app.screenshot("valid-focused");
  const focused = await browser.evaluate(() => {
    const input = document.querySelector('[aria-label="When expression"]')!;
    const group = input.closest('[data-slot="input-group"]')!;
    return { invalid: input.getAttribute("aria-invalid"), border: getComputedStyle(group).borderColor };
  });
  await field.press("Tab");
  await browser.evaluate(() => new Promise(resolve => setTimeout(resolve, 2000)));
  await app.screenshot("valid-resting");
  const resting = await browser.evaluate(() => {
    const group = document.querySelector('[aria-label="When expression"]')!.closest('[data-slot="input-group"]')!;
    const reference = document.querySelector('[aria-label="Search keybindings"]')!.closest('[data-slot="input-group"]')!;
    return { border: getComputedStyle(group).borderColor, normal: getComputedStyle(reference).borderColor, shadow: getComputedStyle(group).boxShadow, normalShadow: getComputedStyle(reference).boxShadow, before: getComputedStyle(group, "::before").boxShadow, normalBefore: getComputedStyle(reference, "::before").boxShadow };
  });
  await browser.evaluate(() => { document.body.style.zoom = "200%"; });
  await app.screenshot("valid-resting-2x");
  await browser.evaluate(() => { document.body.style.zoom = ""; });
  await field.fill("terminalFocus &&");
  await screen.getByText("Use variables with !, &&, ||, and parentheses.", { exact: true }).waitFor();
  await browser.evaluate(() => new Promise(resolve => setTimeout(resolve, 2000)));
  await app.screenshot("invalid-focused");
  const invalid = await browser.evaluate(() => ({ value: document.querySelector('[aria-label="When expression"]')!.getAttribute("aria-invalid"), border: getComputedStyle(document.querySelector('[aria-label="When expression"]')!.closest('[data-slot="input-group"]')!).borderColor }));
  await t3.open(`/settings/secrets?machine=${await runPromise(environmentId(app.baseUrl!))}`);
  await screen.getByRole("button", "Add provider", { exact: true }).tap();
  await screen.getByLabel("Name", { exact: true }).fill("Example");
  await screen.getByLabel("Account", { exact: true }).fill("example.1password.com");
  await screen.getByRole("heading", "Add a 1Password provider", { exact: true }).tap();
  await browser.evaluate(() => new Promise(resolve => setTimeout(resolve, 2000)));
  await app.screenshot("valid-inputs-resting");
  const inputShadow = await browser.evaluate(() => {
    const field = document.querySelector("#secret-provider-account")!;
    const control = field.closest('[data-slot="input-control"]')!;
    return { invalid: field.getAttribute("aria-invalid"), shadow: getComputedStyle(control).boxShadow };
  });
  expect(focused.invalid).toBe("false");
  expect(invalid.value).toBe("true");
  expect(resting.border).toBe(resting.normal);
  expect(resting.shadow).toBe(resting.normalShadow);
  expect(resting.before).toBe(resting.normalBefore);
  expect(focused.border).not.toBe(invalid.border);
  expect(inputShadow.invalid).toBe("false");
  expect(inputShadow.shadow).toContain("0.05");
});
