// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '/strings.m.js';
import { loadTimeData } from '//resources/js/load_time_data.js';
import { GlicPreloadHandlerFactory, GlicPreloadHandlerRemote, PageCallbackRouter, PageHandlerFactory, PageHandlerRemote, PreloadPageCallbackRouter } from './glic.mojom-webui.js';
import { ObservableValue } from './observable.js';
// Whether to enable PageHandler debug logging. Can be enabled with the
// --enable-features=GlicDebugWebview command-line flag.
const kEnableDebug = loadTimeData.getBoolean('enableDebug');
export class BrowserProxyImpl {
    pageHandler;
    pageCallbackRouter;
    glicPreloadHandler;
    preloadPageCallbackRouter;
    instanceId = ObservableValue.withNoValue();
    constructor() {
        this.pageCallbackRouter = new PageCallbackRouter();
        this.preloadPageCallbackRouter = new PreloadPageCallbackRouter();
        const pageHandlerRemote = new PageHandlerRemote();
        if (!kEnableDebug) {
            this.pageHandler = pageHandlerRemote;
        }
        else {
            this.pageHandler = new Proxy(pageHandlerRemote, {
                get(target, name, receiver) {
                    const prop = Reflect.get(target, name, receiver);
                    if (!target.hasOwnProperty(name)) {
                        if (typeof prop === 'function') {
                            return function (...args) {
                                /* eslint no-console: ["error", { allow: ["log"] }] */
                                console.log('PageHandler#', name, args);
                                return prop.apply(this, args);
                            };
                        }
                    }
                    return prop;
                },
            });
        }
        PageHandlerFactory.getRemote()
            .createPageHandler(this.pageHandler.$.bindNewPipeAndPassReceiver(), this.pageCallbackRouter.$.bindNewPipeAndPassRemote())
            .then(({ instanceId }) => {
            this.instanceId.assignAndSignal(instanceId);
        });
        const preloadHandlerRemote = new GlicPreloadHandlerRemote();
        this.glicPreloadHandler = preloadHandlerRemote;
        GlicPreloadHandlerFactory.getRemote().createPreloadHandler(this.glicPreloadHandler.$.bindNewPipeAndPassReceiver(), this.preloadPageCallbackRouter.$.bindNewPipeAndPassRemote());
    }
}
