import { rewriteCsvForImport } from '../rewriteCsv';
import { parseCsv } from '../parseCsv';
// Parse the rewritten output back so assertions are on logical rows, not on
// exact serialization details.
function reparse(content) {
    const result = parseCsv(content);
    if ('code' in result) {
        throw new Error('rewrite produced unparseable CSV');
    }
    return result;
}
describe('rewriteCsvForImport', () => {
    it('renames headers to the destination field keys, keeping the data', () => {
        const content = 'Name,Price\nApple,1\nPear,2';
        const out = reparse(rewriteCsvForImport(content, { Name: 'title', Price: 'price' }));
        expect(out.headers).toEqual(['title', 'price']);
        expect(out.rows).toEqual([
            ['Apple', '1'],
            ['Pear', '2'],
        ]);
    });
    it('drops columns mapped to null', () => {
        const content = 'Name,Price,Internal\nApple,1,x\nPear,2,y';
        const out = reparse(rewriteCsvForImport(content, {
            Name: 'title',
            Price: 'price',
            Internal: null,
        }));
        expect(out.headers).toEqual(['title', 'price']);
        expect(out.rows).toEqual([
            ['Apple', '1'],
            ['Pear', '2'],
        ]);
    });
    it('drops columns absent from the mapping (treated as skip)', () => {
        const content = 'Name,Extra\nApple,x';
        const out = reparse(rewriteCsvForImport(content, { Name: 'title' }));
        expect(out.headers).toEqual(['title']);
        expect(out.rows).toEqual([['Apple']]);
    });
    it('keeps original column order', () => {
        const content = 'A,B,C\n1,2,3';
        const out = reparse(rewriteCsvForImport(content, { A: 'a', B: null, C: 'c' }));
        expect(out.headers).toEqual(['a', 'c']);
        expect(out.rows).toEqual([['1', '3']]);
    });
    it('fills missing trailing cells with empty strings', () => {
        // second row is missing the Price cell
        const content = 'Name,Price\nApple,1\nPear';
        const out = reparse(rewriteCsvForImport(content, { Name: 'title', Price: 'price' }));
        expect(out.rows).toEqual([
            ['Apple', '1'],
            ['Pear', ''],
        ]);
    });
    it('returns an empty result when nothing is mapped', () => {
        const content = 'Name,Price\nApple,1';
        const out = rewriteCsvForImport(content, { Name: null, Price: null });
        // no headers, no rows
        expect(reparse(out)).toEqual({ headers: [], rows: [] });
    });
    it('quotes values containing commas so the round-trip is safe', () => {
        const content = 'Name\n"Apple, Inc."';
        const out = reparse(rewriteCsvForImport(content, { Name: 'title' }));
        expect(out.rows).toEqual([['Apple, Inc.']]);
    });
});
//# sourceMappingURL=rewriteCsv.spec.js.map