import { DEFAULT_MIDSCENE_RECORDER_MARKDOWN_MAX_SCREENSHOTS, createMidsceneRecorderMarkdownScreenshotAssets, getMidsceneRecorderEventDescription } from "@midscene/shared/recorder";
function cleanRecorderSemanticField(value) {
    return value?.trim() === 'AI is analyzing element...' ? void 0 : value;
}
const validateEvents = (events)=>{
    if (!events.length) throw new Error('No events provided for test generation');
};
const getScreenshotsForLLM = (events, maxScreenshots = 1)=>createMidsceneRecorderMarkdownScreenshotAssets(events, {
        baseDir: './screenshots',
        maxScreenshots
    }).map((asset)=>asset.dataUrl);
const filterEventsByType = (events)=>({
        navigationEvents: events.filter((event)=>'navigation' === event.type),
        clickEvents: events.filter((event)=>'click' === event.type),
        inputEvents: events.filter((event)=>'input' === event.type),
        scrollEvents: events.filter((event)=>'scroll' === event.type)
    });
const createEventCounts = (filteredEvents, totalEvents)=>({
        navigation: filteredEvents.navigationEvents.length,
        click: filteredEvents.clickEvents.length,
        input: filteredEvents.inputEvents.length,
        scroll: filteredEvents.scrollEvents.length,
        total: totalEvents
    });
const extractInputDescriptions = (inputEvents)=>inputEvents.map((event)=>({
            description: cleanRecorderSemanticField(event.elementDescription) || '',
            value: event.value || ''
        })).filter((item)=>item.description && item.value);
const processEventsForLLM = (events, screenshotPathByEventHash = new Map())=>events.map((event)=>({
            hashId: event.hashId,
            type: event.type,
            timestamp: event.timestamp,
            source: event.source,
            actionType: event.actionType,
            descriptionSource: event.descriptionSource,
            descriptionError: event.descriptionError,
            url: event.url,
            title: event.title,
            elementDescription: cleanRecorderSemanticField(event.elementDescription),
            replayInstruction: cleanRecorderSemanticField(event.replayInstruction),
            actionSummary: cleanRecorderSemanticField(event.actionSummary),
            semanticConfidence: event.semanticConfidence,
            description: getMidsceneRecorderEventDescription(event),
            value: event.value,
            pageInfo: event.pageInfo,
            elementRect: event.elementRect,
            screenshotPath: screenshotPathByEventHash.get(event.hashId)
        }));
const prepareEventSummary = (events, options = {})=>{
    const filteredEvents = filterEventsByType(events);
    const eventCounts = createEventCounts(filteredEvents, events.length);
    const startUrl = filteredEvents.navigationEvents.length > 0 ? filteredEvents.navigationEvents[0].url || '' : '';
    const clickDescriptions = filteredEvents.clickEvents.map((event)=>event.elementDescription).filter((desc)=>Boolean(desc)).slice(0, 10);
    const inputDescriptions = extractInputDescriptions(filteredEvents.inputEvents).slice(0, 10);
    const urls = filteredEvents.navigationEvents.map((e)=>e.url).filter((url)=>Boolean(url)).slice(0, 5);
    const processedEvents = processEventsForLLM(events, options.screenshotPathByEventHash);
    return {
        testName: options.testName || 'Automated test from recorded events',
        startUrl,
        eventCounts,
        urls,
        clickDescriptions,
        inputDescriptions,
        events: processedEvents
    };
};
function prepareRecorderGenerationContext(input) {
    validateEvents(input.events);
    const maxScreenshots = input.maxScreenshots ?? DEFAULT_MIDSCENE_RECORDER_MARKDOWN_MAX_SCREENSHOTS;
    const screenshotAssets = createMidsceneRecorderMarkdownScreenshotAssets(input.events, {
        baseDir: './screenshots',
        maxScreenshots
    });
    const screenshotPathByEventHash = new Map(screenshotAssets.map((asset)=>[
            asset.eventHashId,
            asset.relativePath
        ]));
    return {
        summary: prepareEventSummary(input.events, {
            testName: input.testName,
            screenshotPathByEventHash
        }),
        screenshotAssets
    };
}
const createMessageContent = (promptText, screenshots = [], includeScreenshots = true)=>{
    const messageContent = [
        {
            type: 'text',
            text: promptText
        }
    ];
    if (includeScreenshots && screenshots.length > 0) {
        messageContent.unshift({
            type: 'text',
            text: 'Here are screenshots from the recording session to help you understand the context:'
        });
        screenshots.forEach((screenshot)=>{
            messageContent.push({
                type: 'image_url',
                image_url: {
                    url: screenshot
                }
            });
        });
    }
    return messageContent;
};
export { createEventCounts, createMessageContent, extractInputDescriptions, filterEventsByType, getScreenshotsForLLM, prepareEventSummary, prepareRecorderGenerationContext, processEventsForLLM, validateEvents };

//# sourceMappingURL=recorder-generation-common.mjs.map