/** Normalize whitespace for stable hashing/preview output. */
export function normalizeQuery(q) {
    return q.replace(/\s+/g, ' ').trim();
}
/**
 * Split SQL into individual statements.
 * Handles: 'single quotes' (incl '' escaping), "double quotes" (incl "" escaping),
 * `backticks`, -- line comments, /* block comments */
export function splitSqlStatements(sql) {
    const out = [];
    let buf = '';
    let inSingle = false;
    let inDouble = false;
    let inBacktick = false;
    let inLineComment = false;
    let inBlockComment = false;
    for (let i = 0; i < sql.length; i++) {
        const c = sql[i];
        const n = i + 1 < sql.length ? sql[i + 1] : '';
        if (inLineComment) {
            buf += c;
            if (c === '\n')
                inLineComment = false;
            continue;
        }
        if (inBlockComment) {
            buf += c;
            if (c === '*' && n === '/') {
                buf += n;
                i++;
                inBlockComment = false;
            }
            continue;
        }
        if (inSingle) {
            buf += c;
            if (c === "'") {
                if (n === "'") {
                    buf += n;
                    i++;
                }
                else {
                    inSingle = false;
                }
            }
            continue;
        }
        if (inDouble) {
            buf += c;
            if (c === '"') {
                if (n === '"') {
                    buf += n;
                    i++;
                }
                else {
                    inDouble = false;
                }
            }
            continue;
        }
        if (inBacktick) {
            buf += c;
            if (c === '`')
                inBacktick = false;
            continue;
        }
        // start comments
        if (c === '-' && n === '-') {
            inLineComment = true;
            buf += c + n;
            i++;
            continue;
        }
        if (c === '/' && n === '*') {
            inBlockComment = true;
            buf += c + n;
            i++;
            continue;
        }
        // start quotes
        if (c === "'") {
            inSingle = true;
            buf += c;
            continue;
        }
        if (c === '"') {
            inDouble = true;
            buf += c;
            continue;
        }
        if (c === '`') {
            inBacktick = true;
            buf += c;
            continue;
        }
        // statement terminator
        if (c === ';') {
            const stmt = buf.trim();
            if (stmt)
                out.push(stmt);
            buf = '';
            continue;
        }
        buf += c;
    }
    const last = buf.trim();
    if (last)
        out.push(last);
    return out;
}
/** Cover common SELECT shapes like "WITH ... SELECT ..." */
export function isSelectish(sql) {
    return /^\s*(select|with)\b/i.test(sql);
}
