// Copyright 2026 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import { screenshotToClient } from '../host/conversions.js';
import { ResponseExtras } from '../transport/messaging.js';
export class ExperimentalTriggeringClientImpl {
    sender;
    host;
    constructor(sender, host) {
        this.sender = sender;
        this.host = host;
    }
    async getExperimentalTriggeringUpdates(handler) {
        const id = this.host.addExperimentalTriggeringUpdatesHandler(handler);
        try {
            const result = await this.sender.requestWithResponse('getExperimentalTriggeringUpdates', {
                observationId: id,
            });
            if (!result.success) {
                this.host.deleteExperimentalTriggeringUpdatesHandler(id);
            }
            return { success: result.success };
        }
        catch (e) {
            this.host.deleteExperimentalTriggeringUpdatesHandler(id);
            throw e;
        }
    }
    async uploadEncryptedScreenshot(screenshot) {
        const extras = new ResponseExtras();
        const clientScreenshot = screenshotToClient(screenshot, extras);
        if (!clientScreenshot) {
            return { fileToken: null };
        }
        try {
            const result = await this.sender.requestWithResponse('uploadEncryptedScreenshot', {
                screenshot: clientScreenshot,
            }, extras.transfers);
            return { fileToken: result.fileToken };
        }
        catch (e) {
            console.warn('Failed to upload encrypted screenshot over postMessage:', e);
            return { fileToken: null };
        }
    }
}
