/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Artifact } from './artifact';
import { DisposableStub } from './disposable';
export class Screencast {
    _page;
    _started = false;
    _savePath;
    _onFrame = null;
    _artifact;
    constructor(page) {
        this._page = page;
        this._page._channel.on('screencastFrame', ({ data }) => {
            void this._onFrame?.({ data });
        });
    }
    async start(options = {}) {
        if (this._started)
            throw new Error('Screencast is already started');
        this._started = true;
        if (options.onFrame)
            this._onFrame = options.onFrame;
        const result = await this._page._channel.screencastStart({
            size: options.size,
            quality: options.quality,
            sendFrames: !!options.onFrame,
            record: !!options.path,
        });
        if (result.artifact) {
            this._artifact = Artifact.from(result.artifact);
            this._savePath = options.path;
        }
        return new DisposableStub(() => this.stop());
    }
    async stop() {
        await this._page._wrapApiCall(async () => {
            this._started = false;
            this._onFrame = null;
            await this._page._channel.screencastStop();
            if (this._savePath)
                await this._artifact?.saveAs(this._savePath);
            this._artifact = undefined;
            this._savePath = undefined;
        });
    }
    async showActions(options) {
        await this._page._channel.screencastShowActions({ duration: options?.duration, position: options?.position, fontSize: options?.fontSize });
        return new DisposableStub(() => this._page._channel.screencastHideActions());
    }
    async hideActions() {
        await this._page._channel.screencastHideActions();
    }
    async showOverlay(html, options) {
        const { id } = await this._page._channel.screencastShowOverlay({ html, duration: options?.duration });
        return new DisposableStub(() => this._page._channel.screencastRemoveOverlay({ id }));
    }
    async showChapter(title, options) {
        await this._page._channel.screencastChapter({ title, ...options });
    }
    async showOverlays() {
        await this._page._channel.screencastSetOverlayVisible({ visible: true });
    }
    async hideOverlays() {
        await this._page._channel.screencastSetOverlayVisible({ visible: false });
    }
}
//# sourceMappingURL=screencast.js.map