import { of } from 'rxjs';
import { delay, map, mergeMap } from 'rxjs/operators';
export function batchGetItemsFetchAll(dynamoRx, params, backoffTimer, throttleTimeSlot) {
    return dynamoRx.batchGetItems(params)
        .pipe(mergeMap(response => {
        if (hasUnprocessedKeys(response)) {
            return of(response.UnprocessedKeys)
                .pipe(delay(backoffTimer.next().value * throttleTimeSlot), mergeMap((UnprocessedKeys) => {
                const nextParams = Object.assign({}, params, { RequestItems: UnprocessedKeys });
                return batchGetItemsFetchAll(dynamoRx, nextParams, backoffTimer, throttleTimeSlot);
            }), map(combineBatchGetResponses(response)));
        }
        return of(response);
    }));
}
export function hasUnprocessedKeys(response) {
    if (!response.UnprocessedKeys) {
        return false;
    }
    return Object.values(response.UnprocessedKeys)
        .some(t => !!t && t.Keys && t.Keys.length > 0);
}
/**
 * combines a first with a second response. ConsumedCapacity is always from the latter.
 * @param response1
 */
export function combineBatchGetResponses(response1) {
    return (response2) => {
        const tableNames = Object.keys(response1.Responses || {});
        Object.keys(response2.Responses || {})
            .filter(tn => !tableNames.includes(tn))
            .forEach(tn => tableNames.push(tn));
        const Responses = tableNames
            .reduce((u, tableName) => (Object.assign({}, u, { [tableName]: [
                ...(response1.Responses && response1.Responses[tableName] || []),
                ...(response2.Responses && response2.Responses[tableName] || []),
            ] })), {});
        return Object.assign({}, response2, { Responses });
    };
}
//# sourceMappingURL=batch-get-utils.js.map