"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordSwitchTabAndSync = recordSwitchTabAndSync;
const toolExecutor_types_1 = require("../toolExecutor-types");
const stepCapture_1 = require("../../background/capture/stepCapture");
const SWITCH_TAB_WAIT_TIME_MS = 1_000;
async function recordSwitchTabAndSync(targetTabId, stateProvider, eventHandler, logger) {
    const trainingContext = stateProvider.getTrainingContext();
    const contextInfo = { tabId: targetTabId, frameId: 0 };
    const result = await (0, toolExecutor_types_1.recordStepAndTrackResult)(() => (0, stepCapture_1.recordTrainedStep)({ action: 'record', listener: 'no-op' }, contextInfo, true, undefined, stateProvider, eventHandler), SWITCH_TAB_WAIT_TIME_MS, stateProvider, eventHandler, logger);
    if (result.success) {
        await syncTrainingContextToTab(targetTabId, trainingContext, eventHandler);
    }
    return result;
}
async function syncTrainingContextToTab(targetTabId, trainingContext, eventHandler) {
    if (targetTabId !== trainingContext.getCurrentTab()) {
        trainingContext.setCurrentTab(targetTabId);
        trainingContext.setCurrentFrameToRoot();
    }
    const activePage = await trainingContext.getActivePage();
    if (activePage) {
        eventHandler.setLastVisitUrl(activePage.url());
    }
}
