// Planu — Web Fetcher: HTML content extraction + fallback placeholders
export function extractTextContent(html) {
    return html
        .replace(/<script[\s\S]*?<\/script>/gi, '')
        .replace(/<style[\s\S]*?<\/style>/gi, '')
        .replace(/<nav[\s\S]*?<\/nav>/gi, '')
        .replace(/<footer[\s\S]*?<\/footer>/gi, '')
        .replace(/<[^>]+>/g, ' ')
        .replace(/&[a-z]+;/gi, ' ')
        .replace(/\s+/g, ' ')
        .trim();
}
export function extractCodeBlocks(html) {
    const blocks = [];
    const regex = /<(?:pre|code)[^>]*>([\s\S]*?)<\/(?:pre|code)>/gi;
    let match;
    while ((match = regex.exec(html)) !== null) {
        const code = (match[1] ?? '')
            .replace(/<[^>]+>/g, '')
            .replace(/&lt;/g, '<')
            .replace(/&gt;/g, '>')
            .replace(/&amp;/g, '&')
            .replace(/&quot;/g, '"')
            .trim();
        if (code.length > 10) {
            blocks.push(code);
        }
    }
    return blocks.slice(0, 5);
}
export function extractBestPractices(html, _topic) {
    const practices = [];
    const liRegex = /<li[^>]*>([\s\S]*?)<\/li>/gi;
    let match;
    while ((match = liRegex.exec(html)) !== null) {
        const text = (match[1] ?? '').replace(/<[^>]+>/g, '').trim();
        if (text.length > 15 && text.length < 300) {
            practices.push(text);
        }
        if (practices.length >= 5) {
            break;
        }
    }
    return practices;
}
export function buildSummaryFromContent(text, topic, framework) {
    const maxLength = 500;
    if (text.length > maxLength) {
        return `Documentation for "${topic}" in ${framework}: ${text.substring(0, maxLength)}...`;
    }
    if (text.length > 0) {
        return `Documentation for "${topic}" in ${framework}: ${text}`;
    }
    return `Documentation for "${topic}" in ${framework} was fetched but content could not be extracted.`;
}
export function buildPlaceholderSummary(topic, framework, baseUrl) {
    if (baseUrl) {
        return (`Documentation for "${topic}" in ${framework} is available at ${baseUrl}. ` +
            `Web fetch was unavailable — this data may be stale.`);
    }
    return (`To get documentation for "${topic}" in ${framework}, ` +
        `use web search or the MCP browser tools to fetch current information. ` +
        `The docsUrl field contains a search link to find relevant documentation.`);
}
export function buildPlaceholderExamples(topic, framework) {
    return [
        `// Example for ${topic} in ${framework}`,
        `// Use MCP web tools to fetch real code examples from official docs.`,
        `// Search: "${framework} ${topic} example"`,
    ];
}
export function buildPlaceholderBestPractices(topic, framework) {
    return [
        `Follow ${framework} official guidelines for ${topic}.`,
        'Use TypeScript/type annotations where available for safety.',
        'Write tests for new functionality.',
        'Handle errors and edge cases explicitly.',
        'Keep components/functions small and focused.',
    ];
}
//# sourceMappingURL=content-extractor.js.map