/**
 * 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.
 */
import { SocksProxy } from '@utils/socksProxy';
import { eventsHelper } from '@utils/eventsHelper';
import { GlobalAPIRequestContext } from '../fetch';
import { AndroidDispatcher } from './androidDispatcher';
import { AndroidDeviceDispatcher } from './androidDispatcher';
import { BrowserDispatcher } from './browserDispatcher';
import { BrowserTypeDispatcher } from './browserTypeDispatcher';
import { Dispatcher } from './dispatcher';
import { LocalUtilsDispatcher } from './localUtilsDispatcher';
import { APIRequestContextDispatcher } from './networkDispatchers';
import { SdkObject } from '../instrumentation';
export class PlaywrightDispatcher extends Dispatcher {
    _type_Playwright;
    _browserDispatcher;
    _denyLaunch;
    constructor(scope, playwright, options = {}) {
        const denyLaunch = options.denyLaunch ?? false;
        const chromium = new BrowserTypeDispatcher(scope, playwright.chromium, denyLaunch);
        const firefox = new BrowserTypeDispatcher(scope, playwright.firefox, denyLaunch);
        const webkit = new BrowserTypeDispatcher(scope, playwright.webkit, denyLaunch);
        const android = new AndroidDispatcher(scope, playwright.android, denyLaunch);
        const initializer = {
            chromium,
            firefox,
            webkit,
            android,
            utils: playwright.options.isServer ? undefined : new LocalUtilsDispatcher(scope, playwright),
            socksSupport: options.socksProxy ? new SocksSupportDispatcher(scope, playwright, options.socksProxy) : undefined,
        };
        let browserDispatcher;
        if (options.preLaunchedBrowser) {
            const browserTypeDispatcher = initializer[options.preLaunchedBrowser.options.name];
            browserDispatcher = new BrowserDispatcher(browserTypeDispatcher, options.preLaunchedBrowser, {
                ignoreStopAndKill: true,
                isolateContexts: !options.sharedBrowser,
            });
            initializer.preLaunchedBrowser = browserDispatcher;
        }
        if (options.preLaunchedAndroidDevice)
            initializer.preConnectedAndroidDevice = new AndroidDeviceDispatcher(android, options.preLaunchedAndroidDevice);
        super(scope, playwright, 'Playwright', initializer);
        this._type_Playwright = true;
        this._browserDispatcher = browserDispatcher;
        this._denyLaunch = denyLaunch;
    }
    async newRequest(params, progress) {
        if (this._denyLaunch)
            throw new Error(`Creating new API request contexts is not allowed.`);
        const request = new GlobalAPIRequestContext(this._object, params);
        return { request: APIRequestContextDispatcher.from(this.parentScope(), request) };
    }
    async cleanup() {
        // Cleanup contexts upon disconnect.
        await this._browserDispatcher?.cleanupContexts();
    }
}
class SocksSupportDispatcher extends Dispatcher {
    _type_SocksSupport;
    _socksProxy;
    _socksListeners;
    constructor(scope, parent, socksProxy) {
        super(scope, new SdkObject(parent, 'socksSupport'), 'SocksSupport', {});
        this._type_SocksSupport = true;
        this._socksProxy = socksProxy;
        this._socksListeners = [
            eventsHelper.addEventListener(socksProxy, SocksProxy.Events.SocksRequested, (payload) => this._dispatchEvent('socksRequested', payload)),
            eventsHelper.addEventListener(socksProxy, SocksProxy.Events.SocksData, (payload) => this._dispatchEvent('socksData', payload)),
            eventsHelper.addEventListener(socksProxy, SocksProxy.Events.SocksClosed, (payload) => this._dispatchEvent('socksClosed', payload)),
        ];
    }
    async socksConnected(params, progress) {
        this._socksProxy?.socketConnected(params);
    }
    async socksFailed(params, progress) {
        this._socksProxy?.socketFailed(params);
    }
    async socksData(params, progress) {
        this._socksProxy?.sendSocketData(params);
    }
    async socksError(params, progress) {
        this._socksProxy?.sendSocketError(params);
    }
    async socksEnd(params, progress) {
        this._socksProxy?.sendSocketEnd(params);
    }
    _onDispose() {
        eventsHelper.removeEventListeners(this._socksListeners);
    }
}
//# sourceMappingURL=playwrightDispatcher.js.map