var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import JSZip from 'jszip';
function loadZip(jarUrl) {
    return __awaiter(this, void 0, void 0, function* () {
        if (Array.isArray(jarUrl)) {
            return yield Promise.all(jarUrl.map((url) => __awaiter(this, void 0, void 0, function* () {
                const zipFile = yield (yield fetch(url)).blob();
                const zip = yield JSZip.loadAsync(zipFile);
                return zip;
            })));
        }
        else {
            const zipFile = yield (yield fetch(jarUrl)).blob();
            const zip = yield JSZip.loadAsync(zipFile);
            return zip;
        }
    });
}
export function getResourceLoader(jarUrl) {
    return __awaiter(this, void 0, void 0, function* () {
        const zip = yield loadZip(jarUrl);
        const stringCache = new Map();
        const blobCache = new Map();
        const getResourceBlob = (name) => __awaiter(this, void 0, void 0, function* () {
            var _a, _b;
            if (blobCache.has(name)) {
                return blobCache.get(name);
            }
            else {
                let data;
                if (Array.isArray(zip)) {
                    for (const zipFile of zip) {
                        data = yield ((_a = zipFile
                            .file(`assets/minecraft/${name}`)) === null || _a === void 0 ? void 0 : _a.async('blob'));
                        if (data) {
                            break;
                        }
                    }
                }
                else {
                    data = yield ((_b = zip
                        .file(`assets/minecraft/${name}`)) === null || _b === void 0 ? void 0 : _b.async('blob'));
                }
                if (!data) {
                    blobCache.set(name, undefined);
                    return undefined;
                }
                const url = URL.createObjectURL(data);
                blobCache.set(name, url);
                return url;
            }
        });
        const getResourceString = (name) => __awaiter(this, void 0, void 0, function* () {
            var _c, _d;
            if (stringCache.has(name)) {
                return stringCache.get(name);
            }
            else {
                let data;
                if (Array.isArray(zip)) {
                    for (const zipFile of zip) {
                        data = yield ((_c = zipFile
                            .file(`assets/minecraft/${name}`)) === null || _c === void 0 ? void 0 : _c.async('string'));
                        if (data) {
                            break;
                        }
                    }
                }
                else {
                    data = yield ((_d = zip
                        .file(`assets/minecraft/${name}`)) === null || _d === void 0 ? void 0 : _d.async('string'));
                }
                stringCache.set(name, data);
                return data;
            }
        });
        const clearCache = () => {
            stringCache.clear();
            blobCache.clear();
        };
        return {
            getResourceBlob,
            getResourceString,
            clearCache,
        };
    });
}
//# sourceMappingURL=resourceLoader.js.map