"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.INITIAL_RETRY_BASE_MS = exports.INITIAL_FETCH_RETRIES = void 0;
exports.initialFetch = initialFetch;
exports.INITIAL_FETCH_RETRIES = 3;
exports.INITIAL_RETRY_BASE_MS = 2_000;
/**
 * Fetch and decrypt the artifact on startup with retry + backoff.
 *
 * Retries up to {@link INITIAL_FETCH_RETRIES} times with exponential backoff.
 * On exhaustion, throws with a descriptive error including an actionable hint
 * based on the HTTP status code (403/404) or generic advice.
 */
async function initialFetch(poller, jitMode, encryptedStore, cache, sourceDesc) {
    let lastErr;
    for (let attempt = 1; attempt <= exports.INITIAL_FETCH_RETRIES; attempt++) {
        try {
            if (jitMode) {
                await poller.fetchAndValidate();
                const artifact = encryptedStore.get();
                const { values } = await poller.getDecryptor().decrypt(artifact);
                cache.swap(values, artifact.revision);
            }
            else {
                await poller.fetchAndDecrypt();
            }
            return; // success
        }
        catch (err) {
            lastErr = err instanceof Error ? err : new Error(String(err));
            if (attempt < exports.INITIAL_FETCH_RETRIES) {
                const delayMs = exports.INITIAL_RETRY_BASE_MS * 2 ** (attempt - 1);
                console.error(`[clef-agent] initial fetch failed (attempt ${attempt}/${exports.INITIAL_FETCH_RETRIES}): ${lastErr.message}. Retrying in ${delayMs}ms...`);
                await new Promise((resolve) => setTimeout(resolve, delayMs));
            }
        }
    }
    // All retries exhausted — build a descriptive error
    const hint = lastErr?.message.includes("403")
        ? "Check that the IAM role or credentials have read access to the artifact."
        : lastErr?.message.includes("404") || lastErr?.message.includes("NoSuchKey")
            ? "The artifact does not exist yet. Has it been deployed with `clef pack`?"
            : "Check the artifact URL and network connectivity.";
    throw new Error(`Failed to fetch artifact from ${sourceDesc} after ${exports.INITIAL_FETCH_RETRIES} attempts: ${lastErr?.message}. ${hint}`);
}
//# sourceMappingURL=initial-fetch.js.map