"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    createEventCounts: ()=>createEventCounts,
    filterEventsByType: ()=>filterEventsByType,
    validateEvents: ()=>validateEvents,
    createMessageContent: ()=>createMessageContent,
    getScreenshotsForLLM: ()=>getScreenshotsForLLM,
    extractInputDescriptions: ()=>extractInputDescriptions,
    prepareEventSummary: ()=>prepareEventSummary,
    prepareRecorderGenerationContext: ()=>prepareRecorderGenerationContext,
    processEventsForLLM: ()=>processEventsForLLM
});
const recorder_namespaceObject = require("@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)=>(0, recorder_namespaceObject.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: (0, recorder_namespaceObject.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 ?? recorder_namespaceObject.DEFAULT_MIDSCENE_RECORDER_MARKDOWN_MAX_SCREENSHOTS;
    const screenshotAssets = (0, recorder_namespaceObject.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;
};
exports.createEventCounts = __webpack_exports__.createEventCounts;
exports.createMessageContent = __webpack_exports__.createMessageContent;
exports.extractInputDescriptions = __webpack_exports__.extractInputDescriptions;
exports.filterEventsByType = __webpack_exports__.filterEventsByType;
exports.getScreenshotsForLLM = __webpack_exports__.getScreenshotsForLLM;
exports.prepareEventSummary = __webpack_exports__.prepareEventSummary;
exports.prepareRecorderGenerationContext = __webpack_exports__.prepareRecorderGenerationContext;
exports.processEventsForLLM = __webpack_exports__.processEventsForLLM;
exports.validateEvents = __webpack_exports__.validateEvents;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "createEventCounts",
    "createMessageContent",
    "extractInputDescriptions",
    "filterEventsByType",
    "getScreenshotsForLLM",
    "prepareEventSummary",
    "prepareRecorderGenerationContext",
    "processEventsForLLM",
    "validateEvents"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

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