/**
 * 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.
 */
export class ExtensionProtocolV1 {
    _sendCommand;
    _sendToCDPClient = null;
    _connectedTabInfo;
    _nextSessionId = 1;
    constructor(sendCommand) {
        this._sendCommand = sendCommand;
    }
    connectOverCDP(sendToCDPClient) {
        this._sendToCDPClient = sendToCDPClient;
    }
    handleExtensionEvent(method, params) {
        switch (method) {
            case 'forwardCDPEvent': {
                const p = params;
                const sessionId = p.sessionId || this._connectedTabInfo?.sessionId;
                this._sendToCDPClient?.({
                    sessionId,
                    method: p.method,
                    params: p.params,
                });
                break;
            }
        }
    }
    async handleCDPCommand(method, params, sessionId) {
        switch (method) {
            case 'Target.setAutoAttach': {
                if (sessionId)
                    return undefined;
                const { targetInfo } = await this._sendCommand('attachToTab', {});
                this._connectedTabInfo = {
                    targetInfo,
                    sessionId: `pw-tab-${this._nextSessionId++}`,
                };
                this._sendToCDPClient?.({
                    method: 'Target.attachedToTarget',
                    params: {
                        sessionId: this._connectedTabInfo.sessionId,
                        targetInfo: {
                            ...this._connectedTabInfo.targetInfo,
                            attached: true,
                        },
                        waitingForDebugger: false,
                    },
                });
                return { result: {} };
            }
            case 'Target.getTargetInfo': {
                return { result: this._connectedTabInfo?.targetInfo };
            }
            case 'Target.createTarget': {
                throw new Error('Tab creation is not supported yet.');
            }
        }
        return undefined;
    }
    async forwardToExtension(method, params, sessionId) {
        // Top level sessionId is only passed between the relay and the client.
        if (this._connectedTabInfo?.sessionId === sessionId)
            sessionId = undefined;
        return await this._sendCommand('forwardCDPCommand', { sessionId, method, params });
    }
    ready() {
        // v1 has no initial handshake; messages from Playwright are processed immediately.
        return Promise.resolve();
    }
    onExtensionDisconnect(_reason) {
        // v1 has no pending ready() promise to reject.
    }
}
//# sourceMappingURL=cdpRelayV1.js.map