/// <reference lib="dom" />
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import { runPromise, test, WEB } from "./t3.ts";
const source = Array.from({length:250}, (_, i) => `export const setting${String(i+1).padStart(3,"0")} = "value-${i+1}";`).join("\n");
test("Select all from a read-only source gutter copies the entire file", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("demo");
  await runPromise(Effect.gen(function*() {
    const fs = yield* FileSystem.FileSystem;
    const path = yield* Path.Path;
    const outside = path.join(path.dirname(project), "shared-config.ts");
    yield* fs.writeFileString(outside, source);
    yield* fs.symlink(outside, path.join(project, "shared-config.ts"));
  }));
  const thread = await t3.thread({project, title:"Review source", messages:["first"]});
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Files", {exact:true}).tap();
  await screen.getByRole("treeitem", "shared-config.ts", {exact:true}).waitFor();
  await browser.evaluate(() => {
    for (const host of document.querySelectorAll("*")) {
      const row = host.shadowRoot?.querySelector<HTMLElement>('[data-item-path="shared-config.ts"]');
      if(row) { row.click(); return true; }
    }
    return false;
  });
  await browser.locator("diffs-container").waitFor();
  const point = await browser.evaluate(() => {
    const host = document.querySelector("diffs-container")!;
    const row = host.shadowRoot!.querySelector('[data-column-number="4"]')!;
    if(!row) throw new Error("Line 4 missing");
    const r = row.getBoundingClientRect();
    return {x:r.left+8,y:r.top+r.height/2};
  });
  await browser.mouse.move(point.x,point.y);
  await browser.mouse.down();
  await browser.mouse.up();
  await browser.keyboard.press("Control+a");
  await app.screenshot("source-selected-from-gutter");
  await browser.keyboard.press("Control+c");
  await screen.getByRole("textbox").tap();
  await browser.keyboard.press("Control+v");
  await app.screenshot("copied-source-pasted-in-composer");
  const pasted = await browser.evaluate(() => Array.from(document.querySelector('[data-testid="composer-editor"]')?.children ?? []).map(p => p.textContent).join("\n"));
  expect(pasted).toBe(source);
});
