"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeFindElement = void 0;
const buffer_1 = require("buffer");
function makeFindElement({ req }) {
    return async function findElement({ session, request, response, logger, }) {
        var _a, _b, _c;
        logger.log('Inspecting element lookup request to collect self-healing metadata');
        const proxyResponse = await req(request.url, {
            baseUrl: session.ecServerUrl,
            io: { request, response, handle: false },
            logger,
        });
        const responseBody = new Uint8Array(await proxyResponse.arrayBuffer());
        const parsed = JSON.parse(new TextDecoder().decode(responseBody));
        if ((_b = (_a = parsed === null || parsed === void 0 ? void 0 : parsed.appliCustomData) === null || _a === void 0 ? void 0 : _a.selfHealing) === null || _b === void 0 ? void 0 : _b.successfulSelector) {
            logger.log('Self-healed locators detected', parsed.appliCustomData.selfHealing);
            (_c = session.metadata) !== null && _c !== void 0 ? _c : (session.metadata = []);
            session.metadata.push(parsed.appliCustomData.selfHealing);
        }
        else {
            logger.log('No self-healing metadata found');
        }
        response
            .writeHead(proxyResponse.status, Object.fromEntries(proxyResponse.headers.entries()))
            .end(buffer_1.Buffer.from(responseBody));
    };
}
exports.makeFindElement = makeFindElement;
