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';

function createFumaPage({ sourceKey, mdxContentSource, getMDXComponents, mdxSourceDir, githubBaseUrl, copyButtonComponent, siteIcon, FallbackPage, supportedLocales = ['en'], showBreadcrumb = true, showTableOfContent = true, showTableOfContentPopover = false, }) {
    const Page = function Page(_a) {
        return __awaiter(this, arguments, void 0, function* ({ params }) {
            const { slug, locale } = yield params;
            const page = mdxContentSource.getPage(slug, locale);
            if (!page) {
                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.title }), jsx(DocsDescription, { className: "mb-2", children: page.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 currentUrl = `${baseUrl}/${locale}/${baseRoute}${currentPath ? `/${currentPath}` : ''}`;
            // generate the seo language map
            const seoLanguageMap = {};
            supportedLocales.forEach(loc => {
                seoLanguageMap[loc] = `${baseUrl}/${loc}/${baseRoute}${currentPath ? `/${currentPath}` : ''}`;
            });
            return {
                metadataBase: new URL(baseUrl),
                title: page.title,
                description: page.description,
                alternates: {
                    canonical: currentUrl,
                    languages: seoLanguageMap
                },
            };
        });
    }
    return {
        Page,
        generateStaticParams,
        generateMetadata,
    };
}

export { createFumaPage };
