"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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.Page = void 0;
const util = __importStar(require("./cdp-util.js"));
const dom_js_1 = require("./dom.js");
const frame_manager_js_1 = require("./frame-manager.js");
const input_manager_js_1 = require("./input-manager.js");
const network_manager_js_1 = require("./network-manager.js");
const screenshot_manager_js_1 = require("./screenshot-manager.js");
class Page {
    constructor(target, frameTree) {
        this.target = target;
        this.domManager = new dom_js_1.DomManager(this);
        this.frameManager = new frame_manager_js_1.FrameManager(this, frameTree);
        this.inputManager = new input_manager_js_1.InputManager(this);
        this.networkManager = new network_manager_js_1.NetworkManager(this);
        this.screenshotManager = new screenshot_manager_js_1.ScreenshotManager(this);
    }
    get browser() {
        return this.target.browser;
    }
    get logger() {
        return this.target.logger;
    }
    get toolkitBinding() {
        return this.browser.config.toolkitBinding;
    }
    async send(method, params, timeout) {
        return await this.target.send(method, params, timeout);
    }
    sendAndForget(method, params) {
        this.target.sendAndForget(method, params);
    }
    async activate() {
        await this.target.activate();
        await this.send('Page.bringToFront');
    }
    close() {
        this.target.close();
    }
    mainFrame() {
        return this.frameManager.mainFrame;
    }
    allFrames() {
        return this.frameManager.frames.values();
    }
    url() {
        return this.target.url;
    }
    async navigate(url, options = {}) {
        await this.target.navigate(url, options);
    }
    async waitForReady(options = {}) {
        await this.target.waitForReady(options);
    }
    async waitForLoad(options = {}) {
        await this.target.waitForLoad(options);
    }
    async waitForAnimationFrame(timeout = 1000) {
        await Promise.race([
            this.evaluate(() => {
                return new Promise(r => requestAnimationFrame(r));
            }),
            new Promise(r => setTimeout(r, timeout)),
        ]);
    }
    async evaluate(pageFn, ...args) {
        return await this.mainFrame().evaluate(pageFn, ...args);
    }
    async evaluateElement(pageFn, ...args) {
        return await this.mainFrame().evaluateElement(pageFn, ...args);
    }
    async evaluateJson(pageFn, ...args) {
        return await this.mainFrame().evaluateJson(pageFn, ...args);
    }
    async evaluateOnNewDocument(pageFn, ...args) {
        const source = util.evaluationString(pageFn, ...args);
        await this.send('Page.addScriptToEvaluateOnNewDocument', {
            source,
        });
    }
    async document() {
        return await this.mainFrame().document();
    }
    async querySelectorAll(selector) {
        return (await this.document()).querySelectorAll(selector);
    }
    async querySelector(selector) {
        return (await this.document()).querySelector(selector);
    }
    async getLayoutMetrics() {
        return await this.send('Page.getLayoutMetrics');
    }
    async grabHtmlSnapshot() {
        return await this.evaluateJson(toolkitBinding => {
            return window[toolkitBinding].htmlSnapshot();
        }, this.toolkitBinding);
    }
    async captureHtmlSnapshot() {
        return await this.mainFrame().captureHtmlSnapshot();
    }
    async clearBrowsingData() {
        await this.send('Storage.clearDataForOrigin', {
            origin: '*',
            storageTypes: [
                'appcache',
                'cookies',
                'file_systems',
                'indexeddb',
                'local_storage',
                'websql',
                'service_workers',
                'interest_groups',
                'shared_storage',
                'storage_buckets',
                'other',
            ].join(','),
        });
        await this.send('Network.clearBrowserCookies');
    }
    async resolveFrameById(frameId) {
        return this.browser.resolveFrameById(frameId);
    }
    async getAllCookies() {
        const { cookies } = await this.send('Network.getAllCookies');
        return cookies;
    }
    async setCookies(cookies) {
        await this.send('Network.setCookies', { cookies });
    }
    async getFrameTree() {
        const { frameTree } = await this.send('Page.getFrameTree');
        return frameTree;
    }
}
exports.Page = Page;
//# sourceMappingURL=page.js.map