import { jsx as _jsx } from "react/jsx-runtime";
import { Search as AlgoliaSearch, ZH_LOCALES, } from '@rspress/plugin-algolia/runtime';
import virtual from 'doom-@global-virtual';
import { useMemo } from 'react';
import { useLocation } from 'react-router';
import { useLang, usePageData, withBase } from 'rspress/runtime';
import { Search as OriginalSearch, Layout as OriginalLayout, getCustomMDXComponent, } from 'rspress/theme';
import classes from '../styles/link.module.scss';
import { useTranslation } from './runtime/index.js';
// eslint-disable-next-line import-x/export
export * from 'rspress/theme';
const X = getCustomMDXComponent();
const getClosestSidebar_ = (sidebarItems, pathname, exportItem, matched, depth = 0) => {
    for (const sidebar of sidebarItems) {
        if ('_fileKey' in sidebar && sidebar._fileKey) {
            if (depth === 0) {
                matched = undefined;
            }
            if (exportItem.flattenScope.includes(sidebar._fileKey)) {
                matched = {
                    sidebar,
                    exportItem,
                    depth,
                };
            }
            if (withBase(sidebar.link) ===
                (pathname.endsWith('.html') ? pathname.slice(0, -5) : pathname)) {
                return matched;
            }
        }
        if ('items' in sidebar) {
            const found = getClosestSidebar_(sidebar.items, pathname, exportItem, matched, depth + 1);
            if (found) {
                return found;
            }
        }
    }
};
const getClosestSidebar = (sidebarItems, pathname) => {
    let found;
    for (const item of virtual.export) {
        const matched = getClosestSidebar_(sidebarItems, pathname, item);
        if (matched) {
            if (!found || matched.depth >= found.depth) {
                found = matched;
            }
        }
    }
    return found;
};
// eslint-disable-next-line import-x/export
export const Layout = () => {
    const { siteData: { lang: siteLang, themeConfig }, } = usePageData();
    const lang = useLang();
    const t = useTranslation();
    const { pathname } = useLocation();
    const found = useMemo(() => {
        if (!virtual.download || !virtual.export?.length) {
            return;
        }
        if (themeConfig.locales?.length) {
            let found;
            for (const { lang, sidebar } of themeConfig.locales) {
                const sidebarItems = sidebar[siteLang === lang ? '/' : `/${lang}`];
                found ??= getClosestSidebar(sidebarItems, pathname);
                if (found) {
                    return found;
                }
            }
        }
        else {
            const sidebarItems = themeConfig.sidebar['/'];
            return getClosestSidebar(sidebarItems, pathname);
        }
    }, [pathname, siteLang, themeConfig]);
    const pdfLink = useMemo(() => found &&
        withBase(`${found.exportItem.name ?? found.sidebar.text}-${lang}.pdf`), [found, lang]);
    return (_jsx(OriginalLayout, { beforeOutline: pdfLink && (_jsx(X.p, { children: _jsx("a", { className: classes.link, href: pdfLink, target: "_blank", rel: "noopener noreferrer", children: t('view_docs_as_pdf') }) })) }));
};
// eslint-disable-next-line import-x/export
export const Search = process.env.ALGOLIA_APP_ID &&
    process.env.ALGOLIA_API_KEY &&
    process.env.ALGOLIA_INDEX_NAME
    ? () => {
        const lang = useLang();
        const docSearchProps = useMemo(() => ({
            appId: process.env.ALGOLIA_APP_ID,
            apiKey: process.env.ALGOLIA_API_KEY,
            indexName: process.env.ALGOLIA_INDEX_NAME,
            searchParameters: {
                facetFilters: [`lang:${lang}`],
            },
        }), [lang]);
        return (_jsx(AlgoliaSearch, { docSearchProps: docSearchProps, locales: ZH_LOCALES }));
    }
    : OriginalSearch;
