"use client";
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, Fragment } from 'react/jsx-runtime';
import { useState } from 'react';
import { useParams } from 'next/navigation';
import { useTranslations } from 'next-intl';
import { useCopyButton } from 'fumadocs-ui/utils/use-copy-button';
import Link from 'fumadocs-core/link';
import { globalLucideIcons } from '@windrun-huaiin/base-ui/components/server';
import { Button } from '@windrun-huaiin/base-ui/ui';

const cache = new Map();
function LLMCopyButton({ llmApiUrl, sourceKey } = {}) {
    const t = useTranslations('fuma');
    const [isLoading, setLoading] = useState(false);
    const params = useParams();
    const locale = params.locale;
    const slug = params.slug;
    const [checked, onClick] = useCopyButton(() => __awaiter(this, void 0, void 0, function* () {
        setLoading(true);
        // Handle cases where slug might be undefined or empty
        const path = (slug && Array.isArray(slug)) ? slug.join('/') : '';
        const apiPrefix = llmApiUrl || (sourceKey ? `/api/${sourceKey}/llm-content` : '/api/llm-content');
        let apiUrl = `${apiPrefix}?locale=${encodeURIComponent(locale)}&path=${encodeURIComponent(path)}`;
        if (sourceKey) {
            apiUrl += `&sourceKey=${encodeURIComponent(sourceKey)}`;
        }
        console.log('Fetching LLM content from:', apiUrl);
        let content;
        try {
            if (cache.has(apiUrl)) {
                content = cache.get(apiUrl);
            }
            else {
                const res = yield fetch(apiUrl);
                if (!res.ok) {
                    content = `Error: Failed to fetch LLM content: ${res.status} ${res.statusText}`;
                }
                else {
                    content = yield res.text();
                    // only success then store cache
                    cache.set(apiUrl, content);
                }
            }
            yield navigator.clipboard.writeText(content);
        }
        catch (error) {
            const errMsg = `Error: ${error instanceof Error ? error.message : String(error)}`;
            yield navigator.clipboard.writeText(errMsg);
            console.error("Error fetching or copying LLM content:", error);
        }
        finally {
            setLoading(false);
        }
    }));
    return (jsx(Button, { variant: "ghost", size: "sm", loading: isLoading, 
        // force button to left align
        className: "justify-start px-0 text-stone-600 hover:text-stone-500 dark:text-stone-400 dark:hover:text-stone-300", onClick: onClick, children: checked ? (jsxs(Fragment, { children: [jsx(globalLucideIcons.Check, {}), t('copyMarkdownDone')] })) : (jsxs(Fragment, { children: [jsx(globalLucideIcons.Markdown, {}), t('copyMarkdown')] })) }));
}
function EditOnGitHub({ url }) {
    const t = useTranslations('fuma');
    return (jsxs(Link, { className: "flex items-center gap-x-2 text-stone-600 hover:text-stone-500 dark:text-stone-400 dark:hover:text-stone-300 text-sm", href: url, children: [jsx(globalLucideIcons.GitHub, {}), t('editOnGithub')] }));
}
// New component for displaying the last updated date with an icon
function LastUpdatedDate({ date }) {
    const t = useTranslations('fuma');
    const viewDate = date ? `${t('lastUpdate')} ${date}` : `${t('emptyLastUpdate')}`;
    return (jsxs("div", { className: "flex items-center gap-x-2 text-stone-600 dark:text-stone-400 text-sm", children: [jsx(globalLucideIcons.LastUpdated, {}), viewDate] }));
}

export { EditOnGitHub, LLMCopyButton, LastUpdatedDate };
