'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';

// Backoff schedule for the post-shield refresh. `shieldToken` resolves
//  when the relay accepts the broadcast, not when the public→private
//  transition confirms on-chain. Aleo block time + RecordScanner indexer
//  lag means the immediate post-broadcast fetch usually returns the
//  pre-confirmation balance, so we re-poll for ~45s before giving up.
//
//  Shared between the auto-shield hook (background) and the widget's
//  manual Shield Manually CTA (foreground), so both flows update the
//  unshielded and shielded balances on the same cadence — previously
//  the manual flow refreshed exactly once and the unshielded row
//  stayed at its pre-broadcast value for the full RecordScanner lag,
//  making a successful shield look like a no-op.
const REFRESH_DELAYS_MS = [
    3000, 5000, 8000, 12000, 18000,
];
/**
 * After a successful shield broadcast, poll `onShielded` on a backoff
 * schedule so the consumer sees both the shielded and unshielded balances
 * update without a manual refresh click. Stops early on cancel; swallows
 * per-iteration errors (refresh is best-effort).
 *
 * `isCancelled` is a thunk so callers can hook it up to whatever liveness
 * signal they have (effect cleanup `cancelled` flag, abort signal, mount
 * ref, etc.) without this module having to know about React lifecycle.
 */
const pollOnShielded = (onShielded, isCancelled) => __awaiter(void 0, void 0, void 0, function* () {
    for (const delay of REFRESH_DELAYS_MS) {
        if (isCancelled())
            return;
        yield new Promise((resolve) => setTimeout(resolve, delay));
        if (isCancelled())
            return;
        try {
            yield onShielded();
        }
        catch (_a) {
            /* swallow — refresh is best-effort */
        }
    }
});

export { REFRESH_DELAYS_MS, pollOnShielded };
