"use strict";
/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InspectorController = void 0;
const recorderSupplement_1 = require("./recorderSupplement");
const debugLogger_1 = require("../../utils/debugLogger");
const utils_1 = require("../../utils/utils");
const consoleApiSource = __importStar(require("../../generated/consoleApiSource"));
class InspectorController {
    async onContextCreated(context) {
        if (utils_1.debugMode() === 'inspector')
            await recorderSupplement_1.RecorderSupplement.getOrCreate(context, { pauseOnNextStatement: true });
        else if (utils_1.debugMode() === 'console')
            await context.extendInjectedScript(consoleApiSource.source);
    }
    async onBeforeCall(sdkObject, metadata) {
        const context = sdkObject.attribution.context;
        if (!context)
            return;
        if (shouldOpenInspector(sdkObject, metadata))
            await recorderSupplement_1.RecorderSupplement.getOrCreate(context, { pauseOnNextStatement: true });
        const recorder = await recorderSupplement_1.RecorderSupplement.getNoCreate(context);
        await (recorder === null || recorder === void 0 ? void 0 : recorder.onBeforeCall(sdkObject, metadata));
    }
    async onAfterCall(sdkObject, metadata) {
        if (!sdkObject.attribution.context)
            return;
        const recorder = await recorderSupplement_1.RecorderSupplement.getNoCreate(sdkObject.attribution.context);
        await (recorder === null || recorder === void 0 ? void 0 : recorder.onAfterCall(sdkObject, metadata));
    }
    async onBeforeInputAction(sdkObject, metadata) {
        if (!sdkObject.attribution.context)
            return;
        const recorder = await recorderSupplement_1.RecorderSupplement.getNoCreate(sdkObject.attribution.context);
        await (recorder === null || recorder === void 0 ? void 0 : recorder.onBeforeInputAction(sdkObject, metadata));
    }
    async onCallLog(logName, message, sdkObject, metadata) {
        debugLogger_1.debugLogger.log(logName, message);
        if (!sdkObject.attribution.context)
            return;
        const recorder = await recorderSupplement_1.RecorderSupplement.getNoCreate(sdkObject.attribution.context);
        recorder === null || recorder === void 0 ? void 0 : recorder.updateCallLog([metadata]);
    }
}
exports.InspectorController = InspectorController;
function shouldOpenInspector(sdkObject, metadata) {
    var _a;
    if (!((_a = sdkObject.attribution.browser) === null || _a === void 0 ? void 0 : _a.options.headful) && !utils_1.isUnderTest())
        return false;
    return metadata.method === 'pause';
}
//# sourceMappingURL=inspectorController.js.map