"use strict";
/**
 * Memoized dynamic-import loader for the Midnight JS SDK.
 *
 * Background: every Midnight SDK dependency tree terminates at
 * `@midnight-ntwrk/compact-runtime`, which is pure ESM and ships no CJS export.
 * NIGHTGATE itself is CommonJS (`"type": "commonjs"`). Top-level `import` from
 * TypeScript would compile to `require()`, which fails on the SDK at runtime.
 *
 * Solution: load the SDK once via dynamic `import()`, cache the namespace
 * objects, and reuse. Call sites that need SDK exports await `loadMidnightSdk()`.
 *
 * The SDK is async to load (Promise<NamespaceObject>) but each call after the
 * first resolves synchronously from the cache.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadMidnightSdk = loadMidnightSdk;
exports.resetMidnightSdkCache = resetMidnightSdkCache;
exports.loadLedgerV8 = loadLedgerV8;
exports.loadWalletSdk = loadWalletSdk;
let cachedBundle;
let inflight;
async function loadMidnightSdk() {
    if (cachedBundle)
        return cachedBundle;
    if (inflight)
        return inflight;
    inflight = (async () => {
        const [contracts, indexer, proof, zk, level, facade] = await Promise.all([
            import('@midnight-ntwrk/midnight-js-contracts'),
            import('@midnight-ntwrk/midnight-js-indexer-public-data-provider'),
            import('@midnight-ntwrk/midnight-js-http-client-proof-provider'),
            import('@midnight-ntwrk/midnight-js-node-zk-config-provider'),
            import('@midnight-ntwrk/midnight-js-level-private-state-provider'),
            import('@midnight-ntwrk/wallet-sdk-facade')
        ]);
        const bundle = { contracts, indexer, proof, zk, level, facade };
        cachedBundle = bundle;
        return bundle;
    })();
    try {
        return await inflight;
    }
    finally {
        inflight = undefined;
    }
}
function resetMidnightSdkCache() {
    cachedBundle = undefined;
    inflight = undefined;
    cachedLedgerV8 = undefined;
    inflightLedger = undefined;
    cachedWalletSdk = undefined;
    inflightWalletSdk = undefined;
}
// ---- ledger-v8 ----
// Loaded separately because not every code path needs the rest of the SDK.
let cachedLedgerV8;
let inflightLedger;
async function loadLedgerV8() {
    if (cachedLedgerV8)
        return cachedLedgerV8;
    if (inflightLedger)
        return inflightLedger;
    inflightLedger = (async () => {
        const mod = await import('@midnight-ntwrk/ledger-v8');
        cachedLedgerV8 = mod;
        return mod;
    })();
    try {
        return await inflightLedger;
    }
    finally {
        inflightLedger = undefined;
    }
}
let cachedWalletSdk;
let inflightWalletSdk;
async function loadWalletSdk() {
    if (cachedWalletSdk)
        return cachedWalletSdk;
    if (inflightWalletSdk)
        return inflightWalletSdk;
    inflightWalletSdk = (async () => {
        const [shielded, unshielded, dust, abstractions] = await Promise.all([
            import('@midnight-ntwrk/wallet-sdk-shielded'),
            import('@midnight-ntwrk/wallet-sdk-unshielded-wallet'),
            import('@midnight-ntwrk/wallet-sdk-dust-wallet'),
            import('@midnight-ntwrk/wallet-sdk-abstractions')
        ]);
        const bundle = { shielded, unshielded, dust, abstractions };
        cachedWalletSdk = bundle;
        return bundle;
    })();
    try {
        return await inflightWalletSdk;
    }
    finally {
        inflightWalletSdk = undefined;
    }
}
