import { __awaiter } from '../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.mjs';
import { jsx, jsxs } from 'react/jsx-runtime';
import { DocsPage, DocsTitle, DocsDescription, DocsBody } from 'fumadocs-ui/page';
import { cloneElement } from 'react';
import { TocFooterWrapper } from './mdx/toc-footer-wrapper.mjs';
import { getAsNeededLocalizedUrl } from '@windrun-huaiin/lib';

function createFumaPage({ sourceKey, mdxContentSource, getMDXComponents, mdxSourceDir, githubBaseUrl, copyButtonComponent, siteIcon, FallbackPage, supportedLocales = ['en'], showBreadcrumb = true, showTableOfContent = true, showTableOfContentPopover = false, localePrefixAsNeeded = true, defaultLocale = 'en', }) {
    const Page = function Page(_a) {
        return __awaiter(this, arguments, void 0, function* ({ params }) {
            var _b, _c, _d;
            const { slug, locale } = yield params;
            const page = mdxContentSource.getPage(slug, locale);
            if (!page) {
                console.log('[FumaPage] missing page', { slug, locale, available: (_d = (_c = (_b = mdxContentSource.pageTree) === null || _b === void 0 ? void 0 : _b[locale]) === null || _c === void 0 ? void 0 : _c.children) === null || _d === void 0 ? void 0 : _d.map((c) => c.url) });
                return jsx(FallbackPage, { siteIcon: siteIcon });
            }
            const path = githubBaseUrl ? `${mdxSourceDir}/${page.path}` : undefined;
            const tocFooterElement = (jsx(TocFooterWrapper, { lastModified: page.data.date, copyButtonComponent: copyButtonComponent
                    ? cloneElement(copyButtonComponent, { sourceKey })
                    : undefined, editPath: path, githubBaseUrl: githubBaseUrl }));
            const MDX = page.data.body;
            return (jsxs(DocsPage, { breadcrumb: { enabled: showBreadcrumb }, tableOfContent: {
                    enabled: showTableOfContent,
                    style: 'clerk',
                    single: false,
                    footer: tocFooterElement,
                }, tableOfContentPopover: {
                    enabled: showTableOfContentPopover,
                    footer: tocFooterElement,
                }, toc: page.data.toc, article: { className: 'max-sm:pb-16' }, children: [jsx(DocsTitle, { children: page.data.title }), jsx(DocsDescription, { className: "mb-2", children: page.data.description }), jsx(DocsBody, { className: "text-fd-foreground/80", children: jsx(MDX, { components: getMDXComponents() }) })] }));
        });
    };
    function generateStaticParams() {
        return mdxContentSource.generateParams('slug', 'locale');
    }
    function generateMetadata(props) {
        return __awaiter(this, void 0, void 0, function* () {
            const { slug, locale } = yield props.params;
            const page = mdxContentSource.getPage(slug, locale);
            if (!page) {
                return {
                    title: '404 - Page Not Found',
                    description: 'This page could not be found.',
                };
            }
            const baseUrl = process.env.NEXT_PUBLIC_BASE_URL;
            const baseRoute = mdxSourceDir.replace('src/mdx/', '');
            // build the current page path
            const currentPath = slug ? slug.join('/') : '';
            const localizedPath = getAsNeededLocalizedUrl(locale || defaultLocale, `/${baseRoute}${currentPath ? `/${currentPath}` : ''}`, localePrefixAsNeeded, defaultLocale);
            const currentUrl = `${baseUrl}${localizedPath}`;
            // generate the seo language map
            const seoLanguageMap = {};
            supportedLocales.forEach(loc => {
                const seoPath = getAsNeededLocalizedUrl(loc, `/${baseRoute}${currentPath ? `/${currentPath}` : ''}`, localePrefixAsNeeded, defaultLocale);
                seoLanguageMap[loc] = `${baseUrl}${seoPath}`;
            });
            return {
                metadataBase: new URL(baseUrl),
                title: page.data.title,
                description: page.data.description,
                alternates: {
                    canonical: currentUrl,
                    languages: seoLanguageMap
                },
            };
        });
    }
    return {
        Page,
        generateStaticParams,
        generateMetadata,
    };
}

export { createFumaPage };
