"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
const FindStrategy_1 = __importDefault(require("../strategies/FindStrategy"));
const webViewUtil_1 = require("../../../util/webViewUtil");
class WebViewFindStrategy extends FindStrategy_1.default {
    isRecordingWhileTrainingMode;
    constructor(isRecordingWhileTrainingMode) {
        super();
        this.isRecordingWhileTrainingMode = isRecordingWhileTrainingMode;
    }
    isApplicable(descriptor) {
        return (0, mablscriptFind_1.isWebViewFindElementDescriptor)(descriptor);
    }
    async findElement(driver, mobileExecutionContext, descriptor, _isShortCircuited) {
        if (!(0, mablscriptFind_1.isWebViewFindElementDescriptor)(descriptor)) {
            throw new Error('Expected a web view descriptor.');
        }
        const webViewFindTarget = descriptor.webView.descriptor.findTarget;
        if (!(0, mablscriptFind_1.isNormalizedFindDescriptor)(webViewFindTarget)) {
            throw new Error(`Expected a normalized find descriptor.`);
        }
        try {
            const webViewIdOrWindowHandleId = await mobileExecutionContext.contextSwitcher.switchToCorrectWebview(descriptor);
            const webViewFindResult = await (0, webViewUtil_1.executeFindInWebView)(driver, mobileExecutionContext, webViewFindTarget);
            return {
                context: this.getMobileElementContext(webViewIdOrWindowHandleId),
                elementReference: webViewFindResult.elementReference,
                scoredCandidates: [],
                webViewElementBoundingClientRect: webViewFindResult.elementBoundingClientRect,
                webViewElementSelector: webViewFindResult.elementSelector,
            };
        }
        finally {
            await mobileExecutionContext.contextSwitcher.switchToNativeContext();
        }
    }
}
exports.default = WebViewFindStrategy;
