/**
 * 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 fs from 'fs';
import path from 'path';
import { resolveWithinRoot } from '@utils/fileUtils';
import { ZipFile } from '@utils/zipFile';
export class DirTraceLoaderBackend {
    _dir;
    constructor(dir) {
        this._dir = dir;
    }
    isLive() {
        return false;
    }
    async entryNames() {
        const entries = [];
        const walk = async (dir, prefix) => {
            const items = await fs.promises.readdir(dir, { withFileTypes: true });
            for (const item of items) {
                if (item.isDirectory())
                    await walk(path.join(dir, item.name), prefix ? `${prefix}/${item.name}` : item.name);
                else
                    entries.push(prefix ? `${prefix}/${item.name}` : item.name);
            }
        };
        await walk(this._dir, '');
        return entries;
    }
    async hasEntry(entryName) {
        const resolved = resolveWithinRoot(this._dir, entryName);
        if (!resolved)
            return false;
        try {
            await fs.promises.access(resolved);
            return true;
        }
        catch {
            return false;
        }
    }
    async readText(entryName) {
        const resolved = resolveWithinRoot(this._dir, entryName);
        if (!resolved)
            return;
        try {
            return await fs.promises.readFile(resolved, 'utf-8');
        }
        catch {
        }
    }
    async readBlob(entryName) {
        const resolved = resolveWithinRoot(this._dir, entryName);
        if (!resolved)
            return;
        try {
            const buffer = await fs.promises.readFile(resolved);
            return new Blob([new Uint8Array(buffer)]);
        }
        catch {
        }
    }
}
export async function extractTrace(traceFile, outDir) {
    const zipFile = new ZipFile(traceFile);
    try {
        const entries = await zipFile.entries();
        for (const entry of entries) {
            const outPath = resolveWithinRoot(outDir, entry);
            if (!outPath)
                throw new Error(`Trace entry '${entry}' escapes output directory`);
            await fs.promises.mkdir(path.dirname(outPath), { recursive: true });
            const buffer = await zipFile.read(entry);
            await fs.promises.writeFile(outPath, buffer);
        }
    }
    finally {
        zipFile.close();
    }
}
//# sourceMappingURL=traceParser.js.map