/**
 * <tree-ui> behavioral tests.
 *
 * FB-46 (v0.5.19): tree-select detail now forwards ctrlKey/metaKey/shiftKey
 * from the originating MouseEvent or KeyboardEvent. Programmatic select()
 * calls (no event) surface all three as false.
 */

import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';

beforeAll(async () => {
  await import('./tree.js');
});

describe('<tree-ui> tree-select forwards modifier keys (FB-46)', () => {
  let host;
  let tree;

  beforeEach(async () => {
    host = document.createElement('div');
    document.body.appendChild(host);

    tree = document.createElement('tree-ui');
    const a = document.createElement('tree-item-ui');
    a.setAttribute('text', 'Alpha');
    a.setAttribute('value', 'a');
    const b = document.createElement('tree-item-ui');
    b.setAttribute('text', 'Beta');
    b.setAttribute('value', 'b');
    tree.appendChild(a);
    tree.appendChild(b);
    host.appendChild(tree);

    // Let connectedCallback + #stamp() settle.
    await new Promise((r) => setTimeout(r, 30));
  });

  afterEach(() => {
    host.remove();
  });

  it('programmatic select() emits ctrlKey/metaKey/shiftKey = false', () => {
    let received = null;
    tree.addEventListener('tree-select', (e) => { received = e.detail; });

    const alpha = tree.querySelector('tree-item-ui[value="a"]');
    tree.select(alpha);

    expect(received).not.toBeNull();
    expect(received.item).toBe(alpha);
    expect(received.text).toBe('Alpha');
    expect(received.value).toBe('a');
    expect(received.ctrlKey).toBe(false);
    expect(received.metaKey).toBe(false);
    expect(received.shiftKey).toBe(false);
  });

  it('select(item, event) forwards Mouse/KeyboardEvent modifier flags into detail', () => {
    let received = null;
    tree.addEventListener('tree-select', (e) => { received = e.detail; });

    const alpha = tree.querySelector('tree-item-ui[value="a"]');
    // Synthesize an event with all three modifiers held.
    const fakeEvent = new MouseEvent('click', {
      ctrlKey: true,
      metaKey: true,
      shiftKey: true,
    });
    tree.select(alpha, fakeEvent);

    expect(received.ctrlKey).toBe(true);
    expect(received.metaKey).toBe(true);
    expect(received.shiftKey).toBe(true);
  });

  it('detail shape stays backward-compatible (existing fields unchanged)', () => {
    let received = null;
    tree.addEventListener('tree-select', (e) => { received = e.detail; });

    const beta = tree.querySelector('tree-item-ui[value="b"]');
    tree.select(beta);

    // Existing consumers destructure { item, text, value } — those keys
    // MUST remain present + carry the expected values.
    const { item, text, value } = received;
    expect(item).toBe(beta);
    expect(text).toBe('Beta');
    expect(value).toBe('b');
  });

  it('partial modifier state passes through (single key held)', () => {
    let received = null;
    tree.addEventListener('tree-select', (e) => { received = e.detail; });

    const alpha = tree.querySelector('tree-item-ui[value="a"]');
    const onlyMeta = new MouseEvent('click', { metaKey: true });
    tree.select(alpha, onlyMeta);

    expect(received.ctrlKey).toBe(false);
    expect(received.metaKey).toBe(true);
    expect(received.shiftKey).toBe(false);
  });
});
