"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RemoteObject = void 0;
class RemoteObject {
    constructor(executionContext, cdpRemoteObject) {
        this.executionContext = executionContext;
        this.cdpRemoteObject = cdpRemoteObject;
    }
    get frame() {
        return this.executionContext.frame;
    }
    get page() {
        return this.executionContext.frame.page;
    }
    isNull() {
        return !this.cdpRemoteObject.objectId && this.cdpRemoteObject.value == null;
    }
    async evaluate(pageFn, ...args) {
        return await this.executionContext.evaluate(pageFn, this, ...args);
    }
    async evaluateElement(pageFn, ...args) {
        return await this.executionContext.evaluateElement(pageFn, this, ...args);
    }
    async evaluateElementList(pageFn, ...args) {
        return await this.executionContext.evaluateElementList(pageFn, this, ...args);
    }
    async evaluateJson(pageFn, ...args) {
        return await this.executionContext.evaluateJson(pageFn, this, ...args);
    }
    async jsonValue() {
        const { objectId } = this.cdpRemoteObject;
        if (this.cdpRemoteObject.objectId) {
            const { result } = await this.page.send('Runtime.callFunctionOn', {
                objectId,
                functionDeclaration: 'function() { return this; }',
                returnByValue: true,
            });
            return result.value;
        }
        return this.cdpRemoteObject.value;
    }
    async getOwnProperties() {
        const { objectId } = this.cdpRemoteObject;
        const { result } = await this.page.send('Runtime.getProperties', {
            objectId,
            ownProperties: true,
        });
        const props = new Map();
        for (const prop of result) {
            if (prop.enumerable) {
                props.set(prop.name, this.executionContext.createRemoteObject(prop.value));
            }
        }
        return props;
    }
    async release() {
        const { objectId } = this.cdpRemoteObject;
        if (objectId) {
            await this.page.send('Runtime.releaseObject', { objectId });
        }
    }
}
exports.RemoteObject = RemoteObject;
//# sourceMappingURL=remote-object.js.map