"use strict";
/**
 * 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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.InMemorySnapshotter = void 0;
const httpServer_1 = require("../../utils/httpServer");
const snapshotRenderer_1 = require("./snapshotRenderer");
const snapshotServer_1 = require("./snapshotServer");
const snapshotter_1 = require("./snapshotter");
class InMemorySnapshotter {
    constructor(context) {
        this._blobs = new Map();
        this._resources = new Map();
        this._frameSnapshots = new Map();
        this._snapshots = new Map();
        this._contextResources = new Map();
        this._server = new httpServer_1.HttpServer();
        new snapshotServer_1.SnapshotServer(this._server, this);
        this._snapshotter = new snapshotter_1.Snapshotter(context, this);
    }
    async start() {
        await this._snapshotter.start();
        return await this._server.start();
    }
    stop() {
        this._snapshotter.dispose();
        this._server.stop().catch(() => { });
    }
    async forceSnapshot(page, snapshotId) {
        await this._snapshotter.forceSnapshot(page, snapshotId);
    }
    onBlob(blob) {
        this._blobs.set(blob.sha1, blob.buffer);
    }
    onResource(resource) {
        this._resources.set(resource.resourceId, resource);
        let resources = this._contextResources.get(resource.url);
        if (!resources) {
            resources = [];
            this._contextResources.set(resource.url, resources);
        }
        resources.push({ frameId: resource.frameId, resourceId: resource.resourceId });
    }
    onFrameSnapshot(snapshot) {
        const key = snapshot.pageId + '/' + snapshot.frameId;
        let frameSnapshots = this._frameSnapshots.get(key);
        if (!frameSnapshots) {
            frameSnapshots = [];
            this._frameSnapshots.set(key, frameSnapshots);
        }
        frameSnapshots.push(snapshot);
        this._snapshots.set(snapshot.snapshotId, new snapshotRenderer_1.SnapshotRenderer(new Map(this._contextResources), frameSnapshots, frameSnapshots.length - 1));
    }
    resourceContent(sha1) {
        return this._blobs.get(sha1);
    }
    resourceById(resourceId) {
        return this._resources.get(resourceId);
    }
    snapshotById(snapshotId) {
        return this._snapshots.get(snapshotId);
    }
}
exports.InMemorySnapshotter = InMemorySnapshotter;
//# sourceMappingURL=inMemorySnapshotter.js.map