"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.smartScrape = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const validatePayloadAndCreateSdk_1 = require("../../lib/validatePayloadAndCreateSdk");
const config_1 = require("./config");
exports.smartScrape = {
    key: 'smartScrape',
    category: extensions_core_1.Category.DATA,
    title: 'Smart Scrape a web page',
    description: 'Scrape a web page with Airtop.',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        const { fields, airtopSdk } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        // Create a headless browser session
        const session = await airtopSdk.sessions.create();
        // Create a new window in the session and open the page
        const window = await airtopSdk.windows.create(session.data.id, {
            url: fields.pageUrl,
        });
        // Scrape the content of the window
        const content = await airtopSdk.windows.scrapeContent(session.data.id, window.data.windowId);
        // Close the window and terminate the session
        await airtopSdk.windows.close(session.data.id, window.data.windowId);
        await airtopSdk.sessions.terminate(session.data.id);
        // Return the result
        await onComplete({
            data_points: {
                result: content.data.modelResponse.scrapedContent.text,
                mimeType: content.data.modelResponse.scrapedContent.contentType,
            },
        });
    },
};
//# sourceMappingURL=smartScrape.js.map