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

const TABLE = '| ID | Value |\n| --- | --- |\n| 𠮷 | 1 |\n| A゙ | 2 |\n| B | 3 |';

test('Copied chat table keeps wide CJK and combining marks aligned', WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project('clipboard-demo');
  const thread = await t3.thread({ project, title: 'Unicode table', messages: [TABLE] });
  await t3.open(thread);
  await screen.getByRole('button', /^Agent/).tap();
  await screen.getByRole('button', 'Copy table', { exact: true }).waitFor();
  // Preserve the real clipboard write, and inspect its actual MIME payloads without a read permission prompt.
  await browser.evaluate(() => {
    const write = navigator.clipboard.write.bind(navigator.clipboard);
    (window as any).__copied = null;
    navigator.clipboard.write = async (items) => {
      const data: Record<string, string> = {};
      for (const item of items) for (const type of item.types) data[type] = await (await item.getType(type)).text();
      (window as any).__copied = data;
      await write(items);
    };
    return null;
  });
  await screen.getByRole('button', 'Copy table', { exact: true }).tap();
  await screen.getByRole('menuitem', 'Copy for chat apps', { exact: true }).tap();
  const copied = await browser.evaluate(async () => {
    const data = (window as any).__copied as Record<string, string>;
    if (!data) throw new Error('No clipboard write captured');
    const panel = document.createElement('section');
    panel.style.cssText = 'position:fixed;z-index:99999;right:20px;top:90px;width:620px;background:#111827;color:#f9fafb;padding:24px;border:2px solid #94a3b8;border-radius:8px;font:16px sans-serif';
    const title = document.createElement('h2'); title.textContent = 'Actual Copy for chat apps clipboard'; panel.append(title);
    const note = document.createElement('p'); note.textContent = 'Spaces shown as ·. Intended widths: 𠮷 = 2, combining ゙ = 0.'; panel.append(note);
    for (const type of ['text/plain', 'text/html']) {
      const label = document.createElement('h3'); label.textContent = type; panel.append(label);
      const pre = document.createElement('pre'); pre.style.cssText = 'font:18px/1.5 monospace;white-space:pre-wrap;overflow-wrap:anywhere';
      pre.textContent = data[type].replaceAll(' ', '·'); panel.append(pre);
    }
    const lines = data['text/plain'].split('\n').filter(line => /[123]$/.test(line));
    const cols = lines.map(line => {
      let width = 0;
      for (const char of line.slice(0, -1)) width += /\p{Mark}/u.test(char) ? 0 : char === '𠮷' ? 2 : 1;
      return width;
    });
    const result = document.createElement('p'); result.textContent = `Value column offsets (0-based): header 4; rows ${cols.join(', ')}.`; panel.append(result);
    document.body.append(panel);
    await document.fonts.ready;
    return data;
  });
  await app.screenshot('copied-chat-table');
  const expected = '```\nID  Value\n--  -----\n𠮷  1\nA゙   2\nB   3\n```';
  expect(copied['text/plain']).toBe(expected);
  expect(copied['text/html']).toBe('<meta charset="utf-8"><pre>' + expected.slice(4, -4) + '</pre>');
});
