"use strict";
/**
 * Project Caffeine v0.0.3
 * Copyright (c) 2025-2026 Gitconomy Research
 *
 * SPDX-License-Identifier: MIT
 *
 * Contributors:
 * - 郭晧 <guohao@gitconomy.org> (Initial Author)
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.searchArxiv = searchArxiv;
/**
 * 连接 Arxiv API 进行文献检索。
 * * @param {string} query - 检索关键字
 * @param {number} [maxResults=5] - 最大返回数量，默认为 5
 * @returns {Promise<Array<{ id: string, title: string, summary: string }>>} 包含文献 ID、标题和摘要的数组
 * @throws {Error} 若网络请求或 XML 解析失败则抛出错误
 */
async function searchArxiv(query, maxResults = 5) {
    const url = `http://export.arxiv.org/api/query?search_query=all:${encodeURIComponent(query)}&start=0&max_results=${maxResults}`;
    try {
        const response = await fetch(url);
        if (!response.ok) {
            throw new Error(`Arxiv API 响应错误: ${response.status}`);
        }
        const xmlData = await response.text();
        // 使用正则提取 entry 节点（轻量级解析，避免引入繁重的 XML 解析库）
        const entries = xmlData.split('<entry>').slice(1);
        return entries.map(entry => {
            const titleMatch = entry.match(/<title>([\s\S]*?)<\/title>/);
            const summaryMatch = entry.match(/<summary>([\s\S]*?)<\/summary>/);
            const idMatch = entry.match(/<id>([\s\S]*?)<\/id>/);
            return {
                id: idMatch ? idMatch[1].trim() : '未知链接',
                title: titleMatch ? titleMatch[1].replace(/\s+/g, ' ').trim() : '无标题',
                summary: summaryMatch ? summaryMatch[1].replace(/\s+/g, ' ').trim() : '无摘要'
            };
        });
    }
    catch (error) {
        throw new Error(`获取文献失败: ${error.message}`);
    }
}
//# sourceMappingURL=arxivService.js.map