"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.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PersistentSnapshotStorage = exports.TraceModel = exports.trace = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const snapshotStorage_1 = require("../../snapshot/snapshotStorage");
exports.trace = __importStar(require("../common/traceEvents"));
class TraceModel {
    constructor(snapshotStorage) {
        this.pageEntries = new Map();
        this.contextResources = new Map();
        this._snapshotStorage = snapshotStorage;
    }
    appendEvents(events, snapshotStorage) {
        for (const event of events)
            this.appendEvent(event);
        const actions = [];
        for (const page of this.contextEntry.pages)
            actions.push(...page.actions);
        const resources = snapshotStorage.resources().reverse();
        actions.reverse();
        for (const action of actions) {
            while (resources.length && resources[0].timestamp > action.timestamp)
                action.resources.push(resources.shift());
            action.resources.reverse();
        }
    }
    appendEvent(event) {
        switch (event.type) {
            case 'context-metadata': {
                this.contextEntry = {
                    startTime: Number.MAX_VALUE,
                    endTime: Number.MIN_VALUE,
                    created: event,
                    pages: [],
                };
                break;
            }
            case 'page-created': {
                const pageEntry = {
                    created: event,
                    destroyed: undefined,
                    actions: [],
                    interestingEvents: [],
                    screencastFrames: [],
                };
                this.pageEntries.set(event.pageId, pageEntry);
                this.contextEntry.pages.push(pageEntry);
                break;
            }
            case 'page-destroyed': {
                this.pageEntries.get(event.pageId).destroyed = event;
                break;
            }
            case 'page-screencast-frame': {
                this.pageEntries.get(event.pageId).screencastFrames.push(event);
                break;
            }
            case 'action': {
                const metadata = event.metadata;
                const pageEntry = this.pageEntries.get(metadata.pageId);
                const action = {
                    actionId: metadata.id,
                    resources: [],
                    ...event,
                };
                pageEntry.actions.push(action);
                break;
            }
            case 'dialog-opened':
            case 'dialog-closed':
            case 'navigation':
            case 'load': {
                const pageEntry = this.pageEntries.get(event.pageId);
                pageEntry.interestingEvents.push(event);
                break;
            }
            case 'resource-snapshot':
                this._snapshotStorage.addResource(event.snapshot);
                break;
            case 'frame-snapshot':
                this._snapshotStorage.addFrameSnapshot(event.snapshot);
                break;
        }
        this.contextEntry.startTime = Math.min(this.contextEntry.startTime, event.timestamp);
        this.contextEntry.endTime = Math.max(this.contextEntry.endTime, event.timestamp);
    }
}
exports.TraceModel = TraceModel;
class PersistentSnapshotStorage extends snapshotStorage_1.BaseSnapshotStorage {
    constructor(resourcesDir) {
        super();
        this._resourcesDir = resourcesDir;
    }
    resourceContent(sha1) {
        return fs_1.default.readFileSync(path_1.default.join(this._resourcesDir, sha1));
    }
}
exports.PersistentSnapshotStorage = PersistentSnapshotStorage;
//# sourceMappingURL=traceModel.js.map