/**
 * Utilities for searching Apify documentation using Algolia.
 *
 * Provides a function to query the Apify docs via Algolia's search API and return structured results.
 *
 * @module utils/apify-docs
 */

!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="5aa9d5ce-6e19-52b1-8100-1b633be0fd0f")}catch(e){}}();
import { algoliasearch } from 'algoliasearch';
import log from '@apify/log';
import { DOCS_SOURCES } from '../const.js';
import { searchApifyDocsCache } from '../state.js';
/**
 * Pool of Algolia search clients, keyed by app ID to handle multiple Algolia accounts.
 */
const clientPool = {};
function getAlgoliaClient(appId, apiKey) {
    if (!clientPool[appId]) {
        clientPool[appId] = algoliasearch(appId, apiKey);
    }
    return clientPool[appId];
}
/**
 * Builds an Algolia search request with conditional filters based on documentation source configuration.
 *
 * @param {object} indexConfig - The documentation source configuration from DOCS_SOURCES
 * @param {string} query - The search query string
 * @returns {object} Algolia search request object with index name, query, and conditional filters
 */
function prepareAlgoliaRequest(indexConfig, query) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const searchRequest = {
        indexName: indexConfig.indexName,
        query: query.trim(),
    };
    // Apply filters if configured
    if ('filters' in indexConfig && indexConfig.filters) {
        searchRequest.filters = indexConfig.filters;
    }
    // Apply type filter if configured (e.g., for Crawlee to filter to lvl1 pages only)
    if ('typeFilter' in indexConfig && indexConfig.typeFilter) {
        const typeFilter = `type:${indexConfig.typeFilter}`;
        if (searchRequest.filters) {
            // Combine with existing filters using AND
            searchRequest.filters = `${searchRequest.filters} AND ${typeFilter}`;
        }
        else {
            searchRequest.filters = typeFilter;
        }
    }
    // Apply facet filters if configured
    if ('facetFilters' in indexConfig && indexConfig.facetFilters) {
        searchRequest.facetFilters = indexConfig.facetFilters;
    }
    return searchRequest;
}
/**
 * Processes Algolia search response and transforms hits into ApifyDocsSearchResult array.
 *
 * @param {AlgoliaResult[]} results - Raw Algolia search results
 * @returns {ApifyDocsSearchResult[]} Processed search results with URL (may include anchor) and optional content
 */
function processAlgoliaResponse(results) {
    var _a;
    const searchResults = [];
    for (const result of results) {
        if (!((_a = result.hits) === null || _a === void 0 ? void 0 : _a.length)) {
            continue;
        }
        for (const hit of result.hits) {
            if (!hit.url_without_anchor) {
                continue;
            }
            // Build URL with anchor if present
            let url = hit.url_without_anchor;
            if (hit.anchor && hit.anchor.trim()) {
                url += `#${hit.anchor}`;
            }
            searchResults.push({
                url,
                ...(hit.content ? { content: hit.content } : {}),
            });
        }
    }
    return searchResults;
}
/**
 * Searches a specific documentation source by ID using Algolia.
 *
 * @param {string} docSource - The documentation source ID ('apify', 'crawlee-js', or 'crawlee-py').
 * @param {string} query - The search query string.
 * @returns {Promise<ApifyDocsSearchResult[]>} Array of search results with URL (may include anchor) and optional content.
 */
export async function searchDocsBySource(docSource, query) {
    const indexConfig = DOCS_SOURCES.find((idx) => idx.id === docSource);
    if (!indexConfig) {
        throw new Error(`Unknown documentation source: ${docSource}`);
    }
    const client = getAlgoliaClient(indexConfig.appId, indexConfig.apiKey);
    const searchRequest = prepareAlgoliaRequest(indexConfig, query);
    const response = await client.search({
        requests: [searchRequest],
    });
    const results = response.results;
    const searchResults = processAlgoliaResponse(results);
    log.info(`[Algolia] Search completed successfully. Found ${searchResults.length} results for "${docSource}"`);
    return searchResults;
}
/**
 * Searches a documentation source with caching.
 *
 * @param {string} docSource - The documentation source ID ('apify', 'crawlee-js', or 'crawlee-py').
 * @param {string} query - The search query string.
 * @returns {Promise<ApifyDocsSearchResult[]>} Array of search results with URL (may include anchor) and optional content.
 */
export async function searchDocsBySourceCached(docSource, query) {
    const cacheKey = `${docSource}::${query.trim().toLowerCase()}`;
    const cachedResults = searchApifyDocsCache.get(cacheKey);
    if (cachedResults) {
        log.debug(`[Algolia] Cache hit for key: "${cacheKey}". Returning ${cachedResults.length} cached results`);
        return cachedResults;
    }
    log.debug(`[Algolia] Cache miss for key: "${cacheKey}". Executing search...`);
    const results = await searchDocsBySource(docSource, query);
    searchApifyDocsCache.set(cacheKey, results);
    return results;
}
//# sourceMappingURL=apify_docs.js.map
//# debugId=5aa9d5ce-6e19-52b1-8100-1b633be0fd0f
