/**
 * 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 { EventEmitter } from 'events';
import { eventsHelper } from '@utils/eventsHelper';
import { packageJSON, packageRoot } from '../../package';
import { SessionProviderEvent } from './sessionProvider';
export class IdentitySessionProvider extends EventEmitter {
    _context;
    _browser;
    _descriptor;
    _listeners = [];
    _closed = false;
    constructor(context) {
        super();
        const browser = context.browser();
        if (!browser)
            throw new Error('SingleContextDashboardProvider requires a context with an attached browser');
        this._context = context;
        this._browser = browser;
        this._descriptor = synthesiseDescriptor(browser);
    }
    start() {
        const emitTabsChanged = () => this.emit(SessionProviderEvent.TabsChanged);
        this._listeners.push(eventsHelper.addEventListener(this._context, 'page', emitTabsChanged), eventsHelper.addEventListener(this._context, 'pageload', emitTabsChanged), eventsHelper.addEventListener(this._context, 'pageclose', emitTabsChanged), eventsHelper.addEventListener(this._context, 'framenavigated', (frame) => {
            if (frame === frame.page().mainFrame())
                this.emit(SessionProviderEvent.TabsChanged);
        }), eventsHelper.addEventListener(this._context, 'close', () => {
            this._closed = true;
            this.emit(SessionProviderEvent.ContextClosed, this._context);
            this.emit(SessionProviderEvent.TabsChanged);
        }));
        this.emit(SessionProviderEvent.SessionsChanged);
        this.emit(SessionProviderEvent.TabsChanged);
        const firstPage = this._context.pages()[0];
        if (firstPage)
            this.emit(SessionProviderEvent.AttachRequested, firstPage);
    }
    dispose() {
        this._listeners.forEach(d => d.dispose());
        this._listeners = [];
        this.removeAllListeners();
    }
    async sessions() {
        return this._closed ? [] : [this._descriptor];
    }
    contextEntries() {
        if (this._closed)
            return [];
        return [{ browser: this._browser, context: this._context, descriptor: this._descriptor }];
    }
    findContext(params) {
        if (this._closed)
            return undefined;
        if (params.browser !== this._descriptor.browser.guid)
            return undefined;
        if (contextId(this._context) !== params.context)
            return undefined;
        return this._context;
    }
    findPage(params) {
        const context = this.findContext(params);
        return context?.pages().find(p => pageId(p) === params.page);
    }
    async closeSession() {
        // No-op: lifecycle of the user-provided context is managed by the caller.
    }
}
function synthesiseDescriptor(browser) {
    const browserName = browser.browserType().name();
    const browserInfo = {
        // eslint-disable-next-line no-restricted-syntax -- _guid is very conservative.
        guid: browser._guid,
        browserName,
        launchOptions: {},
    };
    return {
        title: 'Playwright',
        playwrightVersion: packageJSON.version,
        playwrightLib: packageRoot,
        browser: browserInfo,
    };
}
function pageId(p) {
    // eslint-disable-next-line no-restricted-syntax -- _guid is very conservative.
    return p._guid;
}
function contextId(c) {
    // eslint-disable-next-line no-restricted-syntax -- _guid is very conservative.
    return c._guid;
}
//# sourceMappingURL=identitySessionProvider.js.map