import { describe, expect, it } from 'vitest';

import { buildSelectedCellClipboardText } from './dataGridSelectionCopy';

describe('dataGridSelectionCopy helpers', () => {
  it('builds clipboard text in visible row and column order', () => {
    const text = buildSelectedCellClipboardText({
      selectedCells: [
        { rowKey: 'row-2', colName: 'name' },
        { rowKey: 'row-1', colName: 'id' },
        { rowKey: 'row-1', colName: 'name' },
        { rowKey: 'row-2', colName: 'id' },
      ],
      rows: [
        { __rowKey: 'row-1', id: 1, name: 'Alice' },
        { __rowKey: 'row-2', id: 2, name: 'Bob' },
      ],
      columnOrder: ['id', 'name', 'email'],
      rowKeyField: '__rowKey',
    });

    expect(text).toBe('1\tAlice\n2\tBob');
  });

  it('normalizes null, objects and multiline text for clipboard safety', () => {
    const text = buildSelectedCellClipboardText({
      selectedCells: [
        { rowKey: 'row-1', colName: 'notes' },
        { rowKey: 'row-1', colName: 'meta' },
        { rowKey: 'row-2', colName: 'notes' },
        { rowKey: 'row-2', colName: 'meta' },
      ],
      rows: [
        { __rowKey: 'row-1', notes: null, meta: { a: 1 } },
        { __rowKey: 'row-2', notes: 'line1\nline2\tvalue', meta: [1, 2] },
      ],
      columnOrder: ['notes', 'meta'],
      rowKeyField: '__rowKey',
    });

    expect(text).toBe('NULL\t{"a":1}\nline1 line2 value\t[1,2]');
  });
});
