import { trimTrailingBlank } from './types.js';
const ARRAY_TABLE = /^\s*\[\[\s*(.+?)\s*\]\]\s*(#.*)?$/;
const TABLE = /^\s*\[\s*(.+?)\s*\]\s*(#.*)?$/;
const KEY = /^\s*([A-Za-z0-9_."'-]+?)\s*=\s*(.*)$/;
/**
 * TOML tables and keys, both addressable.
 *
 * Deeper than a header-only scan: `[tool.ruff]` becomes an entity *and* so does
 * each key inside it, so changing one setting is one entity change rather than
 * a rewrite of the whole table.
 *
 * The span is hashed as raw text rather than as a re-serialized value, which
 * makes a comment edit inside a table visible — the same choice YAML makes, so
 * the two formats do not disagree about what counts as a change.
 */
export function extractTomlEntities(content) {
    const lines = content.split(/\r?\n/);
    const entities = [];
    const arrayTableCounts = new Map();
    let table;
    let tablePath = '';
    let lastKey;
    const closeKey = (endLine) => {
        if (lastKey)
            lastKey.endLine = trimTrailingBlank(lines, endLine, lastKey.startLine);
        lastKey = undefined;
    };
    const closeTable = (endLine) => {
        closeKey(endLine);
        if (table)
            table.endLine = trimTrailingBlank(lines, endLine, table.startLine);
        table = undefined;
    };
    for (const [position, line] of lines.entries()) {
        const lineNumber = position + 1;
        const trimmed = line.trim();
        if (trimmed === '' || trimmed.startsWith('#'))
            continue;
        const arrayMatch = ARRAY_TABLE.exec(line);
        if (arrayMatch) {
            closeTable(lineNumber - 1);
            const key = arrayMatch[1];
            // Array-of-table entries are addressed by ordinal, so appending one is an
            // addition rather than a renumbering of everything that follows.
            const ordinal = arrayTableCounts.get(key) ?? 0;
            arrayTableCounts.set(key, ordinal + 1);
            tablePath = `${key}/${ordinal}`;
            table = {
                kind: 'array-table',
                name: key,
                qualifiedName: tablePath,
                startLine: lineNumber,
                endLine: lines.length,
            };
            entities.push(table);
            continue;
        }
        const tableMatch = TABLE.exec(line);
        if (tableMatch) {
            closeTable(lineNumber - 1);
            tablePath = tableMatch[1];
            table = {
                kind: 'table',
                name: tablePath,
                qualifiedName: tablePath,
                startLine: lineNumber,
                endLine: lines.length,
            };
            entities.push(table);
            continue;
        }
        const keyMatch = KEY.exec(line);
        if (!keyMatch)
            continue;
        closeKey(lineNumber - 1);
        const name = keyMatch[1].replace(/^['"]|['"]$/g, '');
        lastKey = {
            kind: 'property',
            name,
            qualifiedName: tablePath ? `${tablePath}.${name}` : name,
            containerName: tablePath || undefined,
            startLine: lineNumber,
            endLine: lineNumber,
        };
        entities.push(lastKey);
    }
    closeTable(lines.length);
    return entities;
}
//# sourceMappingURL=toml.js.map