import { __awaiter } from "tslib";
import axios from "axios";
import semver from "semver";
import { staleMemoize } from "@qorebase/app-runtime/src/utils/staleMemoize";
const getPackageVersions = (name, version) => __awaiter(void 0, void 0, void 0, function* () {
    const { data: { versions = {} }, } = yield axios(`https://registry.npmjs.org/${name}`);
    return Object.keys(versions).filter((v) => semver.satisfies(v, version));
});
const toCDNUrl = (name, path = null, version = "latest") => process.env.NEXT_PUBLIC_NPM_CDN_URL.replace("{name}", name).replace("{version}", path === null ? version : `${version}/${path}`);
const generatePackageEntries = (name, version, url, path = null) => __awaiter(void 0, void 0, void 0, function* () {
    const versions = yield getPackageVersions(name, version);
    return Object.fromEntries(versions
        .map((version) => [toCDNUrl(name, path, version), url])
        .concat([[path === null ? name : `${name}/${path}`, url]]));
});
const generateImportMaps = () => __awaiter(void 0, void 0, void 0, function* () {
    return {
        imports: Object.assign(Object.assign(Object.assign({}, (yield generatePackageEntries("react", ">16.8", "/react.js"))), (yield generatePackageEntries("react", ">16.8", "/react-jsx-runtime.js", "jsx-runtime"))), (yield generatePackageEntries("@emotion/react", ">=11", "/emotion-react.js"))),
    };
});
const memoizedGenerateImportMaps = staleMemoize(generateImportMaps, 1000 * 60 * 60 * 12);
export { memoizedGenerateImportMaps as generateImportMaps };
//# sourceMappingURL=generateImportMap.js.map