import { contextData, explainData, exportsData, fileDepsData, fnDepsData, fnImpactData, impactAnalysisData, whereData, } from '../domain/queries.js';
import { ConfigError } from '../shared/errors.js';
import { complexityData } from './complexity.js';
import { dataflowData } from './dataflow.js';
import { flowData } from './flow.js';
export const BATCH_COMMANDS = {
    'fn-impact': { fn: fnImpactData, sig: 'name' },
    context: { fn: contextData, sig: 'name' },
    explain: { fn: explainData, sig: 'target' },
    where: { fn: whereData, sig: 'target' },
    query: { fn: fnDepsData, sig: 'name' },
    impact: { fn: impactAnalysisData, sig: 'file' },
    deps: { fn: fileDepsData, sig: 'file' },
    exports: { fn: exportsData, sig: 'file' },
    flow: { fn: flowData, sig: 'name' },
    dataflow: { fn: dataflowData, sig: 'name' },
    complexity: { fn: complexityData, sig: 'dbOnly' },
};
export function batchData(command, targets, customDbPath, opts = {}) {
    const entry = BATCH_COMMANDS[command];
    if (!entry) {
        throw new ConfigError(`Unknown batch command "${command}". Valid commands: ${Object.keys(BATCH_COMMANDS).join(', ')}`);
    }
    const results = [];
    let succeeded = 0;
    let failed = 0;
    for (const target of targets) {
        try {
            let data;
            if (entry.sig === 'dbOnly') {
                data = entry.fn(customDbPath, { ...opts, target });
            }
            else {
                data = entry.fn(target, customDbPath, opts);
            }
            results.push({ target, ok: true, data });
            succeeded++;
        }
        catch (err) {
            results.push({ target, ok: false, error: err.message });
            failed++;
        }
    }
    return { command, total: targets.length, succeeded, failed, results };
}
export function splitTargets(targets) {
    const out = [];
    for (const item of targets) {
        if (typeof item !== 'string') {
            out.push(item);
            continue;
        }
        for (const part of item.split(',')) {
            const trimmed = part.trim();
            if (trimmed)
                out.push(trimmed);
        }
    }
    return out;
}
export function multiBatchData(items, customDbPath, sharedOpts = {}) {
    const results = [];
    let succeeded = 0;
    let failed = 0;
    for (const item of items) {
        const { command, target, opts: itemOpts } = item;
        const entry = BATCH_COMMANDS[command];
        if (!entry) {
            results.push({
                command,
                target,
                ok: false,
                error: `Unknown batch command "${command}". Valid commands: ${Object.keys(BATCH_COMMANDS).join(', ')}`,
            });
            failed++;
            continue;
        }
        const merged = { ...sharedOpts, ...itemOpts };
        try {
            let data;
            if (entry.sig === 'dbOnly') {
                data = entry.fn(customDbPath, { ...merged, target });
            }
            else {
                data = entry.fn(target, customDbPath, merged);
            }
            results.push({ command, target, ok: true, data });
            succeeded++;
        }
        catch (err) {
            results.push({ command, target, ok: false, error: err.message });
            failed++;
        }
    }
    return { mode: 'multi', total: items.length, succeeded, failed, results };
}
//# sourceMappingURL=batch.js.map