import React, { lazy } from 'react';
import { useLocation } from '@docusaurus/router';
import BrowserOnly from '@docusaurus/BrowserOnly';
import { useActiveVersion, useVersions } from '@docusaurus/plugin-content-docs/client';
import { usePluginData } from '@docusaurus/useGlobalData';
import useOrama from './useOrama.js';
import { getColorMode, getPreferredVersion } from "./utils.js";
const OramaSearchButton = lazy(() => import('@orama/react-components').then(module => ({
    default: module.OramaSearchButton
})));
const OramaSearchBox = lazy(() => import('@orama/react-components').then(module => ({
    default: module.OramaSearchBox
})));
// Add `where` when collectionManager is provided
// Handles different query APIs
function formatSearchParams(versionName, collectionManager) {
    if (collectionManager) {
        return {
            version: versionName
        };
    }
    return {
        version: { eq: versionName }
    };
}
export function OramaSearchNoDocs() {
    var _a;
    const colorMode = getColorMode();
    const { searchBoxConfig, searchBtnConfig = {
        text: 'Search'
    } } = useOrama();
    const collectionManager = (_a = searchBoxConfig.basic) === null || _a === void 0 ? void 0 : _a.collectionManager;
    return (React.createElement(React.Fragment, null,
        React.createElement(OramaSearchButton, { colorScheme: colorMode, className: "DocSearch-Button", ...searchBtnConfig }, searchBtnConfig === null || searchBtnConfig === void 0 ? void 0 : searchBtnConfig.text),
        React.createElement(OramaSearchBox, { ...(collectionManager ? {} : searchBoxConfig.basic), ...searchBoxConfig.custom, oramaCoreClientInstance: collectionManager, colorScheme: colorMode, searchParams: {
                where: formatSearchParams('current', collectionManager)
            } })));
}
export function OramaSearchWithDocs({ pluginId }) {
    var _a;
    const colorMode = getColorMode();
    const { searchBoxConfig, searchBtnConfig } = useOrama();
    const collectionManager = (_a = searchBoxConfig.basic) === null || _a === void 0 ? void 0 : _a.collectionManager;
    const versions = useVersions(pluginId);
    const activeVersion = useActiveVersion(pluginId);
    const preferredVersion = getPreferredVersion(searchBoxConfig.basic.clientInstance);
    const currentVersion = activeVersion || preferredVersion || versions[0];
    const searchParams = {
        ...(currentVersion && {
            ...formatSearchParams(currentVersion, collectionManager)
        })
    };
    return (React.createElement(React.Fragment, null,
        React.createElement(OramaSearchButton, { colorScheme: colorMode, className: "DocSearch-Button", ...searchBtnConfig }, (searchBtnConfig === null || searchBtnConfig === void 0 ? void 0 : searchBtnConfig.text) || 'Search'),
        React.createElement(OramaSearchBox, { ...(collectionManager ? {} : searchBoxConfig.basic), ...searchBoxConfig.custom, oramaCoreClientInstance: collectionManager, colorScheme: colorMode, searchParams: {
                where: searchParams
            } })));
}
export default function OramaSearchWrapper() {
    const { pathname } = useLocation();
    const { docsInstances } = usePluginData('@orama/plugin-docusaurus-v3');
    let pluginId = undefined;
    if (docsInstances) {
        pluginId = docsInstances.find((id) => pathname.includes(id)) || (docsInstances === null || docsInstances === void 0 ? void 0 : docsInstances[0]);
    }
    return React.createElement(BrowserOnly, { fallback: React.createElement("div", null, "Loading Search...") }, () => {
        return pluginId ? React.createElement(OramaSearchWithDocs, { pluginId: pluginId }) : React.createElement(OramaSearchNoDocs, null);
    });
}
