"use strict";
// src/orchestrator/util/ResponseStructureExtractor.ts
// v1.17.0 — Pure-regex utilities for parsing LLM CLI output into structured
// data. Used by the Autonomous Story Loop's HostInvoker adapters.
//
// Design principles:
//   - Stateless, no network calls of its own.
//   - Each extractor handles one dimension; failures are independent.
//   - Tolerant of noise (prose between code blocks, decorative text).
//   - Conservative defaults: when in doubt, return empty rather than wrong.
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractFilePaths = extractFilePaths;
exports.extractCodeBlocks = extractCodeBlocks;
exports.extractACsChecked = extractACsChecked;
exports.extractTestCmd = extractTestCmd;
exports.parseAll = parseAll;
function extractFilePaths(text) {
    if (!text)
        return [];
    const out = [];
    const seen = new Set();
    const re = /`([a-zA-Z0-9_./\\-]+(?:\.[a-zA-Z0-9]+|\/[a-zA-Z0-9_.-]+))(?::\d+)?`/g;
    let m;
    while ((m = re.exec(text)) !== null) {
        const p = m[1];
        if (seen.has(p))
            continue;
        if (p.startsWith('node_modules/'))
            continue;
        if (/^@[a-z0-9-]+\/[a-z0-9-]+$/i.test(p))
            continue;
        seen.add(p);
        out.push(p);
    }
    return out;
}
function extractCodeBlocks(text) {
    if (!text)
        return [];
    const out = [];
    const re = /```([a-zA-Z0-9_+./\\-]*)(?:[ \t]+([a-zA-Z0-9_./\\-]+))?\n([\s\S]*?)\n```/g;
    let m;
    while ((m = re.exec(text)) !== null) {
        const language = (m[1] || 'text').toLowerCase();
        const pathHint = m[2];
        let content = m[3];
        let filePath = pathHint;
        if (!filePath) {
            const firstLine = content.split('\n', 1)[0];
            const cmt = /^\s*(?:\/\/|#|--)\s*([a-zA-Z0-9_./\\-]+\.[a-zA-Z0-9]+)\s*$/.exec(firstLine);
            if (cmt) {
                filePath = cmt[1];
                content = content.slice(firstLine.length + 1);
            }
        }
        out.push({ language, content, filePath });
    }
    return out;
}
function extractACsChecked(text) {
    if (!text)
        return [];
    const out = [];
    const re = /^(?:\s*(?:[-*]|\d+\.))\s*\[([ xX])\]\s+.+$/gm;
    let m;
    let idx = 0;
    while ((m = re.exec(text)) !== null) {
        if (m[1].toLowerCase() === 'x')
            out.push(idx);
        idx++;
    }
    return out;
}
function extractTestCmd(text) {
    if (!text)
        return null;
    const knownRunners = '(?:npm|yarn|pnpm|bun|pytest|jest|vitest|mocha|cargo|go|deno)';
    const cmdRe = new RegExp(`\`(${knownRunners}\\s+[^\`\\n]{1,120})\``);
    const m = cmdRe.exec(text);
    if (m)
        return m[1].trim();
    const proseRe = new RegExp(`(?:^|\\s)(?:Run|Tests?|Execute)\\s*[:=]\\s*(${knownRunners}\\s+[^\\n]{1,120})`, 'i');
    const p = proseRe.exec(text);
    if (p)
        return p[1].trim();
    return null;
}
function parseAll(text) {
    return {
        filePaths: extractFilePaths(text),
        codeBlocks: extractCodeBlocks(text),
        acsChecked: extractACsChecked(text),
        testCmd: extractTestCmd(text),
    };
}
