'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 fs = require('node:fs');
var nodePath = require('node:path');
var llmUtils = require('@windrun-huaiin/lib/llm-utils');

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
 * General MDX content reading and processing tool function, logs are consistent with router.
 */
function LLMCopyHandler(options) {
    return tslib_es6.__awaiter(this, void 0, void 0, function* () {
        const { sourceDir, dataSource, requestedPath, locale } = options;
        // log received parameters
        console.log(`[LLMCopy] Received, locale=${locale}, path=${requestedPath}`);
        const slug = (requestedPath === null || requestedPath === void 0 ? void 0 : requestedPath.split('/')) || [];
        try {
            console.log('[LLMCopy] Attempting to call getPage()');
            const page = dataSource.getPage(slug, locale);
            // console.log(page);
            console.log('[LLMCopy] Call to getPage() completed.');
            if (!page) {
                console.error(`[LLMCopy] Page or page data not found for locale=${locale}, path=${requestedPath}`);
                return { error: 'Page data not found', status: 404 };
            }
            if (!page.path) {
                console.error(`[LLMCopy] file path information missing in page data for locale=${locale}, path=${requestedPath}`);
                return { error: 'Page file path information missing', status: 500 };
            }
            const title = page.title;
            const description = page.description;
            const relativeMdxFilePath = page.path;
            const absoluteFilePath = nodePath.join(process.cwd(), sourceDir, relativeMdxFilePath);
            console.log(`[LLMCopy] Attempting to read MDX content from: ${absoluteFilePath}`);
            let mdxContent;
            try {
                mdxContent = fs.readFileSync(absoluteFilePath, 'utf-8');
                console.log(`[LLMCopy] Successfully read MDX content from: ${absoluteFilePath}`);
            }
            catch (readError) {
                console.error(`[LLMCopy] Failed to read file at: ${absoluteFilePath}. Error: ${readError.message}`);
                console.error('[LLMCopy] Read Error object details:', JSON.stringify(readError, Object.getOwnPropertyNames(readError), 2));
                // directory traversal debug logs
                try {
                    console.log(`[LLMCopy] Current CWD: ${process.cwd()}`);
                    console.log(`[LLMCopy] CWD contents: ${fs.readdirSync(process.cwd()).join(', ')}`);
                    const srcPath = nodePath.join(process.cwd(), 'src');
                    if (fs.existsSync(srcPath)) {
                        console.log(`[LLMCopy] src dir contents: ${fs.readdirSync(srcPath).join(', ')}`);
                        const srcMdxPath = nodePath.join(process.cwd(), 'src', 'mdx');
                        if (fs.existsSync(srcMdxPath)) {
                            console.log(`[LLMCopy] src/mdx dir contents: ${fs.readdirSync(srcMdxPath).join(', ')}`);
                            const srcMdxDocsPath = nodePath.join(process.cwd(), sourceDir);
                            if (fs.existsSync(srcMdxDocsPath)) {
                                console.log(`[LLMCopy] ${sourceDir} dir contents: ${fs.readdirSync(srcMdxDocsPath).join(', ')}`);
                            }
                        }
                    }
                }
                catch (listDirError) {
                    console.warn(`[LLMCopy] Could not list directory contents for debugging: ${listDirError.message}`);
                }
                return { error: `Error reading MDX file: ${readError.message}`, status: 500 };
            }
            try {
                const text = yield llmUtils.getLLMText(mdxContent, title, description);
                return { text, status: 200 };
            }
            catch (error) {
                console.error(`[LLMCopy] Error processing MDX content for locale=${locale}, path=${requestedPath}:`, error);
                console.error('[LLMCopy] General Error object details:', JSON.stringify(error, Object.getOwnPropertyNames(error), 2));
                return { error: 'Error processing MDX content', status: 500 };
            }
        }
        catch (error) {
            console.error(`[LLMCopy] General error for locale=${locale}, path=${requestedPath}:`, error);
            console.error('[LLMCopy] General Error object details:', JSON.stringify(error, Object.getOwnPropertyNames(error), 2));
            return { error: 'Internal Server Error', status: 500 };
        }
    });
}

exports.LLMCopyHandler = LLMCopyHandler;
