"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.showTraceViewer = void 0;
const fs = require("fs");
const path = require("path");
const playwright = require("../../..");
const util = require("util");
const screenshotGenerator_1 = require("./screenshotGenerator");
const traceModel_1 = require("./traceModel");
const snapshotServer_1 = require("./snapshotServer");
const fsReadFileAsync = util.promisify(fs.readFile.bind(fs));
const emptyModel = {
    contexts: [
        {
            startTime: 0,
            endTime: 1,
            created: {
                timestamp: Date.now(),
                type: 'context-created',
                browserName: 'none',
                contextId: '<empty>',
                deviceScaleFactor: 1,
                isMobile: false,
                viewportSize: { width: 800, height: 600 },
            },
            destroyed: {
                timestamp: Date.now(),
                type: 'context-destroyed',
                contextId: '<empty>',
            },
            name: '<empty>',
            filePath: '',
            pages: [],
            resourcesByUrl: new Map()
        }
    ]
};
class TraceViewer {
    async load(traceDir) {
        const resourcesDir = path.join(traceDir, 'resources');
        const model = { contexts: [] };
        this._document = {
            model,
            resourcesDir,
        };
        for (const name of fs.readdirSync(traceDir)) {
            if (!name.endsWith('.trace'))
                continue;
            const filePath = path.join(traceDir, name);
            const traceContent = await fsReadFileAsync(filePath, 'utf8');
            const events = traceContent.split('\n').map(line => line.trim()).filter(line => !!line).map(line => JSON.parse(line));
            traceModel_1.readTraceFile(events, model, filePath);
        }
    }
    async show() {
        const browser = await playwright.chromium.launch({ headless: false });
        const server = await snapshotServer_1.SnapshotServer.create(path.join(__dirname, 'web'), this._document ? this._document.resourcesDir : undefined, this._document ? this._document.model : emptyModel, this._document ? new screenshotGenerator_1.ScreenshotGenerator(this._document.resourcesDir, this._document.model) : undefined);
        const uiPage = await browser.newPage({ viewport: null });
        uiPage.on('close', () => process.exit(0));
        await uiPage.goto(server.traceViewerUrl('index.html'));
    }
}
async function showTraceViewer(traceDir) {
    const traceViewer = new TraceViewer();
    if (traceDir)
        await traceViewer.load(traceDir);
    await traceViewer.show();
}
exports.showTraceViewer = showTraceViewer;
//# sourceMappingURL=traceViewer.js.map