import { useEffect, useState } from 'react';
import useBaseUrl from '@docusaurus/useBaseUrl';
import useIsBrowser from '@docusaurus/useIsBrowser';
import { usePluginData } from '@docusaurus/useGlobalData';
import { ungzip } from 'pako';
import { create, insertMultiple } from '@orama/orama';
import { pluginAnalytics } from '@orama/plugin-analytics';
import { CollectionManager } from '@orama/core';
import { DOCS_PRESET_SCHEMA } from '../../constants.js';
import { createOramaInstance } from '../../utils.js';
function getOramaPlugins(plugins) {
    const pluginsArray = [];
    if (plugins === null || plugins === void 0 ? void 0 : plugins.analytics) {
        pluginsArray.push(pluginAnalytics({
            apiKey: plugins.analytics.apiKey,
            indexId: plugins.analytics.indexId,
            enabled: plugins.analytics.enabled
        }));
    }
    return pluginsArray;
}
async function getOramaLocalData(indexGzipURL, plugins) {
    try {
        const searchResponse = await fetch(indexGzipURL);
        if (!searchResponse.ok) {
            const errorText = await searchResponse.text();
            throw new Error(`HTTP error ${searchResponse.status}: ${errorText}`);
        }
        const buffer = await searchResponse.arrayBuffer();
        const deflatedString = ungzip(buffer, { to: 'string' });
        const parsedData = JSON.parse(deflatedString);
        const db = create({
            schema: { ...DOCS_PRESET_SCHEMA, version: 'enum' },
            plugins: getOramaPlugins(plugins)
        });
        const documents = Object.values(parsedData.docs.docs);
        await insertMultiple(db, documents);
        return db;
    }
    catch (error) {
        console.error('Error loading search index:', error);
        throw error;
    }
}
function isCloudData(data) {
    return data.oramaMode === 'cloud';
}
export default function useOrama() {
    const [searchBoxConfig, setSearchBoxConfig] = useState({
        basic: {},
        custom: {}
    });
    const oramaData = usePluginData('@orama/plugin-docusaurus-v3');
    const indexGzipURL = useBaseUrl('orama-search-index-current.json.gz');
    const isBrowser = useIsBrowser();
    useEffect(() => {
        async function loadOrama() {
            var _a;
            let oramaInstance;
            let searchBoxBasicConfig = {};
            if (isCloudData(oramaData)) {
                const collectionId = oramaData.indexConfig.collection_id;
                const apiKey = oramaData.indexConfig.api_key;
                let collectionManager;
                if (collectionId) { // Note: collectionId is ONLY available in OramaCore
                    collectionManager = new CollectionManager({
                        url: oramaData.indexConfig.endpoint,
                        collectionID: collectionId,
                        readAPIKey: apiKey
                    });
                }
                searchBoxBasicConfig = {
                    index: {
                        endpoint: oramaData.indexConfig.endpoint,
                        api_key: oramaData.indexConfig.api_key
                    },
                    collectionManager: collectionManager
                };
            }
            else if (oramaData.oramaDocs) {
                oramaInstance = await createOramaInstance(oramaData.oramaDocs);
                searchBoxBasicConfig = { clientInstance: oramaInstance };
            }
            else {
                oramaInstance = await getOramaLocalData(indexGzipURL, oramaData.plugins);
                searchBoxBasicConfig = { clientInstance: oramaInstance };
            }
            setSearchBoxConfig({
                basic: {
                    ...searchBoxBasicConfig,
                    facetProperty: 'category',
                    disableChat: !isCloudData(oramaData)
                },
                custom: (_a = oramaData.searchbox) !== null && _a !== void 0 ? _a : {}
            });
        }
        if (!isBrowser) {
            return;
        }
        loadOrama().catch((error) => {
            console.error('Cannot load search index.', error);
        });
    }, [isBrowser]);
    return { searchBoxConfig, searchBtnConfig: oramaData.searchButton };
}
