(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.searxngSearch = searxngSearch;
    async function searxngSearch(baseUrl, query, options) {
        let searchQuery = query;
        if (options.includeDomains?.length) {
            const siteFilter = options.includeDomains.map((d) => `site:${d}`).join(' OR ');
            searchQuery = `${query} (${siteFilter})`;
        }
        if (options.excludeDomains?.length) {
            searchQuery += options.excludeDomains.map((d) => ` -site:${d}`).join('');
        }
        const normalizedUrl = baseUrl.replace(/\/+$/, '');
        const params = new URLSearchParams({
            q: searchQuery,
            format: 'json',
            pageno: '1',
        });
        const response = await fetch(`${normalizedUrl}/search?${params}`, {
            headers: {
                Accept: 'application/json',
            },
        });
        if (!response.ok) {
            throw new Error(`SearXNG search failed: ${response.status} ${response.statusText}`);
        }
        const data = (await response.json());
        const maxResults = options.maxResults ?? 5;
        return {
            query,
            results: (data.results ?? []).slice(0, maxResults).map((r) => ({
                title: r.title,
                url: r.url,
                snippet: r.content,
                ...(r.publishedDate ? { publishedDate: r.publishedDate } : {}),
            })),
        };
    }
});
//# sourceMappingURL=searxng-search.js.map