import type { Protocol } from "devtools-protocol";
import { CdpConnection } from "./cdp.js";
import { Page } from "./page.js";
import type { StagehandAPIClient } from "../api.js";
import { LocalBrowserLaunchOptions } from "../types/public/index.js";
import { InitScriptSource } from "../types/private/index.js";
import { Cookie, ClearCookieOptions, CookieParam } from "../types/public/context.js";
/**
 * V3Context
 *
 * Owns the root CDP connection and wires Target/Page events into Page.
 * Maintains one Page per top-level target, adopts OOPIF child sessions into the owner Page,
 * and tracks target→page and (root) frame→target mappings for lookups.
 *
 * IMPORTANT: FrameId → session ownership is managed inside Page (via its FrameRegistry).
 * Context never “guesses” owners; it simply forwards events (with the emitting session)
 * so Page can record the correct owner at event time.
 */
export declare class V3Context {
    readonly conn: CdpConnection;
    private readonly env;
    private readonly apiClient;
    private readonly localBrowserLaunchOptions;
    private constructor();
    private readonly _piercerInstalled;
    private _lastPopupSignalAt;
    private readonly _targetSessionListeners;
    private readonly _sessionInit;
    private pagesByTarget;
    private mainFrameToTarget;
    private sessionOwnerPage;
    private frameOwnerPage;
    private pendingOopifByMainFrame;
    private createdAtByTarget;
    private typeByTarget;
    private _pageOrder;
    private pendingCreatedTargetUrl;
    private readonly initScripts;
    private extraHttpHeaders;
    private installTargetSessionListeners;
    /**
     * Create a Context for a given CDP websocket URL and bootstrap target wiring.
     */
    static create(wsUrl: string, opts?: {
        env?: "LOCAL" | "BROWSERBASE";
        apiClient?: StagehandAPIClient | null;
        localBrowserLaunchOptions?: LocalBrowserLaunchOptions | null;
    }): Promise<V3Context>;
    /**
     * Wait until at least one top-level Page has been created and registered.
     * We poll internal maps that bootstrap/onAttachedToTarget populate.
     */
    private waitForFirstTopLevelPage;
    private waitForInitialTopLevelTargets;
    private ensurePiercer;
    /** Mark a page target as the most-recent one (active). */
    private _pushActive;
    /** Remove a page target from the recency list (used on close). */
    private _removeFromOrder;
    /** Return the current active Page (most-recent page that still exists). */
    activePage(): Page | undefined;
    /** Explicitly mark a known Page as the most-recent active page (and focus it). */
    setActivePage(page: Page): void;
    addInitScript<Arg>(script: InitScriptSource<Arg>, arg?: Arg): Promise<void>;
    setExtraHTTPHeaders(headers: Record<string, string>): Promise<void>;
    /**
     * Return top-level `Page`s (oldest → newest). OOPIF targets are not included.
     */
    pages(): Page[];
    private applyInitScriptsToPage;
    /**
     * Resolve an owning `Page` by the **top-level main frame id**.
     * Note: child (OOPIF) roots are intentionally not present in this mapping.
     */
    resolvePageByMainFrameId(frameId: string): Page | undefined;
    /**
     * Serialize the full frame tree for a given top-level main frame id.
     */
    getFullFrameTreeByMainFrameId(rootMainFrameId: string): Promise<Protocol.Page.FrameTree>;
    /**
     * Create a new top-level page (tab) with the given URL and return its Page object.
     * Waits until the target is attached and registered.
     */
    newPage(url?: string): Promise<Page>;
    /**
     * Close CDP and clear all mappings. Best-effort cleanup.
     */
    close(): Promise<void>;
    /**
     * Bootstrap target lifecycle:
     * - Attach to existing targets.
     * - Handle auto-attach events.
     * - Clean up on detach/destroy.
     */
    private bootstrap;
    /**
     * Handle a newly attached target (top-level or potential OOPIF):
     * - Enable Page domain and lifecycle events.
     * - If top-level → create Page, wire listeners, resume.
     * - Else → probe child root frame id via `Page.getFrameTree` and adopt immediately
     *   if the parent is known; otherwise stage until parent `frameAttached`.
     * - Resume the target only after listeners are wired.
     */
    private onAttachedToTarget;
    /**
     * Detach handler:
     * - Remove child session ownership and prune its subtree.
     * - If a top-level target, cleanup its `Page` and mappings.
     * - Drop any staged child for this session.
     */
    private onDetachedFromTarget;
    /**
     * Cleanup a top-level Page by target id, removing its root and staged children.
     */
    private cleanupByTarget;
    /**
     * Wire Page-domain frame events for a session into the owning Page & mappings.
     * We forward the *emitting session* with every event so Page can stamp ownership precisely.
     */
    private installFrameEventBridges;
    /**
     * Register that a session belongs to a Page (used by event routing).
     */
    private wireSessionToOwnerPage;
    /**
     * Utility: reverse-lookup the top-level target id that owns a given Page.
     */
    private findTargetIdByPage;
    private _notePopupSignal;
    /**
     * Await the current active page, waiting briefly if a popup/open was just triggered.
     * Normal path returns immediately; popup path waits up to timeoutMs for the new page.
     */
    awaitActivePage(timeoutMs?: number): Promise<Page>;
    /**
     * Get all browser cookies, optionally filtered by URL(s).
     *
     * When `urls` is omitted or empty every cookie in the browser context is
     * returned. When one or more URLs are supplied only cookies whose
     * domain/path/secure attributes match are included.
     */
    cookies(urls?: string | string[]): Promise<Cookie[]>;
    /**
     * Add one or more cookies to the browser context.
     *
     * Each cookie must specify either a `url` (from which domain/path/secure are
     * derived) or an explicit `domain` + `path` pair.
     *
     * We surface CDP errors if the browser rejects a cookie.
     */
    addCookies(cookies: CookieParam[]): Promise<void>;
    /**
     * Clear cookies from the browser context.
     *
     * - Called with no arguments: clears **all** cookies atomically via
     *   `Storage.clearCookies`.
     * - Called with filter options: fetches all cookies, clears everything,
     *   then re-adds only the cookies that do NOT match the filter via
     *   `Storage.setCookies`. This is necessary on the browser endpoint because
     *   the Storage domain does not support targeted deletes.
     */
    clearCookies(options?: ClearCookieOptions): Promise<void>;
}
