'use strict';

var tslib_es6 = require('../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.js');
var jsxRuntime = require('react/jsx-runtime');
var page = require('fumadocs-ui/page');
var React = require('react');
var tocFooterWrapper = require('./mdx/toc-footer-wrapper.js');
var lib = require('@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 tslib_es6.__awaiter(this, arguments, void 0, function* ({ params }) {
            var _b, _c, _d;
            const { slug, locale } = yield params;
            const page$1 = mdxContentSource.getPage(slug, locale);
            if (!page$1) {
                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 jsxRuntime.jsx(FallbackPage, { siteIcon: siteIcon });
            }
            const path = githubBaseUrl ? `${mdxSourceDir}/${page$1.path}` : undefined;
            const tocFooterElement = (jsxRuntime.jsx(tocFooterWrapper.TocFooterWrapper, { lastModified: page$1.data.date, copyButtonComponent: copyButtonComponent
                    ? React.cloneElement(copyButtonComponent, { sourceKey })
                    : undefined, editPath: path, githubBaseUrl: githubBaseUrl }));
            const MDX = page$1.data.body;
            return (jsxRuntime.jsxs(page.DocsPage, { breadcrumb: { enabled: showBreadcrumb }, tableOfContent: {
                    enabled: showTableOfContent,
                    style: 'clerk',
                    single: false,
                    footer: tocFooterElement,
                }, tableOfContentPopover: {
                    enabled: showTableOfContentPopover,
                    footer: tocFooterElement,
                }, toc: page$1.data.toc, article: { className: 'max-sm:pb-16' }, children: [jsxRuntime.jsx(page.DocsTitle, { children: page$1.data.title }), jsxRuntime.jsx(page.DocsDescription, { className: "mb-2", children: page$1.data.description }), jsxRuntime.jsx(page.DocsBody, { className: "text-fd-foreground/80", children: jsxRuntime.jsx(MDX, { components: getMDXComponents() }) })] }));
        });
    };
    function generateStaticParams() {
        return mdxContentSource.generateParams('slug', 'locale');
    }
    function generateMetadata(props) {
        return tslib_es6.__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 = lib.getAsNeededLocalizedUrl(locale || defaultLocale, `/${baseRoute}${currentPath ? `/${currentPath}` : ''}`, localePrefixAsNeeded, defaultLocale);
            const currentUrl = `${baseUrl}${localizedPath}`;
            // generate the seo language map
            const seoLanguageMap = {};
            supportedLocales.forEach(loc => {
                const seoPath = lib.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,
    };
}

exports.createFumaPage = createFumaPage;
