/**
 * CSV and TSV rows, keyed by their first cell.
 *
 * Line numbers are physical, counted over every line including blank ones, so
 * they match what an editor shows. Filtering blanks before indexing — which is
 * the obvious implementation — silently shifts every subsequent row's reported
 * position, and blame then points at the wrong line.
 */
export function extractCsvEntities(content, relativePath) {
    const separator = relativePath.toLowerCase().endsWith('.tsv') ? '\t' : ',';
    const lines = content.split(/\r?\n/);
    const entities = [];
    let header;
    const seen = new Map();
    for (const [position, line] of lines.entries()) {
        if (line.trim() === '')
            continue;
        const cells = parseRow(line, separator);
        if (!header) {
            header = cells;
            continue;
        }
        const key = cells[0]?.trim() ?? '';
        const name = key === '' ? `row_${position + 1}` : key;
        // Duplicate keys are ordinary in real data, so the identity falls back to
        // the line rather than fusing two rows into one entity.
        const occurrence = seen.get(name);
        seen.set(name, (occurrence ?? 0) + 1);
        const qualifiedName = occurrence === undefined ? `row[${name}]` : `row[${name}]@L${position + 1}`;
        entities.push({
            kind: 'row',
            name,
            qualifiedName,
            startLine: position + 1,
            endLine: position + 1,
        });
    }
    return entities;
}
/** Split one line, honouring quoted cells and the `""` escape inside them. */
function parseRow(line, separator) {
    const cells = [];
    let cell = '';
    let quoted = false;
    for (let index = 0; index < line.length; index++) {
        const char = line[index];
        if (quoted) {
            if (char === '"') {
                if (line[index + 1] === '"') {
                    cell += '"';
                    index++;
                    continue;
                }
                quoted = false;
                continue;
            }
            cell += char;
            continue;
        }
        if (char === '"') {
            quoted = true;
            continue;
        }
        if (char === separator) {
            cells.push(cell);
            cell = '';
            continue;
        }
        cell += char;
    }
    cells.push(cell);
    return cells;
}
//# sourceMappingURL=csv.js.map