"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.unpackBundle = void 0;
const tslib_1 = require("tslib");
const pako_1 = tslib_1.__importDefault(require("pako"));
/**
 * Unpacks the .tar.gz (odbx format) file and returns the items.
 *
 * @param data - The .tar.gz (odbx format) file.
 * @returns - The items in the .tar.gz file.
 */
function unpackBundle(data) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        // @ts-ignore
        if (typeof window !== 'undefined') {
            const module = yield Promise.resolve().then(() => tslib_1.__importStar(require('js-untar')));
            const untar = module.default;
            const buffer = data instanceof ArrayBuffer ? data : yield data.arrayBuffer();
            const archive = pako_1.default.ungzip(new Uint8Array(buffer));
            const items = yield untar(archive.buffer);
            const hasCache = items.some((item) => item.name.startsWith('cache/'));
            let images = [];
            const manifestJson = items.find((item) => item.name.match(/manifest.json$/));
            if (manifestJson) {
                const decoder = new TextDecoder('utf-8');
                const content = decoder.decode(manifestJson.buffer);
                const manifest = JSON.parse(content);
                images = manifest.map((m) => m.RepoTags).reduce((a, b) => a.concat(b), []);
            }
            const packages = items.filter((item) => item.name.match(/\/package(.*).zip/));
            return {
                hasCache,
                images,
                items,
                packages,
            };
        }
        else {
            return { hasCache: false, images: [], items: [], packages: [] };
        }
    });
}
exports.unpackBundle = unpackBundle;
//# sourceMappingURL=unpack-bundle.js.map