"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sleep = exports.partitionArray = exports.httpWithRetries = exports.http = void 0;
function partitionArray(array, partitionSize) {
    const workingCopy = [...array];
    const result = [];
    while (workingCopy.length) {
        result.push(workingCopy.splice(0, partitionSize));
    }
    return result;
}
exports.partitionArray = partitionArray;
async function httpWithRetries(request, retriesLeft = 1, timeBetweenRetries = 1000) {
    console.log('fetch with retries', request, retriesLeft, timeBetweenRetries);
    if (retriesLeft <= 0) {
        console.error('could not load cards', request);
        return null;
    }
    let result = null;
    try {
        result = await http(request);
    }
    catch (e) {
        console.warn('Exception while getting cards', e);
    }
    if (!result || result.startsWith('<')) {
        console.log('no result, sleeping');
        await sleep(timeBetweenRetries);
        console.log('retrying');
        return httpWithRetries(request, retriesLeft - 1);
    }
    return result;
}
exports.httpWithRetries = httpWithRetries;
async function http(request) {
    return new Promise((resolve) => {
        fetch(request)
            .then((response) => {
            return response.text();
        }, (error) => {
            console.warn('could not load cards', error, error.message, JSON.stringify(error));
            return null;
        })
            .then((body) => {
            resolve(body);
        });
    });
}
exports.http = http;
async function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}
exports.sleep = sleep;
//# sourceMappingURL=utils.js.map