import { map } from 'rxjs/operators';
import { metadataForClass } from '../../../decorator/metadata';
import { randomExponentialBackoffTimer } from '../../../helper';
import { createLogger } from '../../../logger/logger';
import { createToKeyFn, toDb } from '../../../mapper';
import { getTableName } from '../../get-table-name.function';
import { batchWriteItemsWriteAll } from './batch-write-utils';
const MAX_BATCH_WRITE_ITEMS = 25;
const DEFAULT_TIME_SLOT = 1000;
export class BatchWriteSingleTableRequest {
    constructor(dynamoRx, modelClazz) {
        this.createDeleteRequest = (item) => ({ DeleteRequest: { Key: this.toKey(item) } });
        this.createPutRequest = (item) => ({ PutRequest: { Item: toDb(item, this.modelClazz) } });
        this.logger = createLogger('dynamo.request.BatchWriteSingleTableRequest', modelClazz);
        this.dynamoRx = dynamoRx;
        if (modelClazz === null || modelClazz === undefined) {
            throw new Error("please provide the model clazz for the request, won't work otherwise");
        }
        this.modelClazz = modelClazz;
        this.metadata = metadataForClass(this.modelClazz);
        if (!this.metadata.modelOptions) {
            throw new Error('given ModelConstructor has no @Model decorator');
        }
        this.tableName = getTableName(this.metadata);
        this.params = {
            RequestItems: {
                [this.tableName]: [],
            },
        };
    }
    get toKey() {
        if (!this.keyFn) {
            this.keyFn = createToKeyFn(this.modelClazz);
        }
        return this.keyFn;
    }
    returnConsumedCapacity(value) {
        this.params.ReturnConsumedCapacity = value;
    }
    returnItemCollectionMetrics(value) {
        this.params.ReturnItemCollectionMetrics = value;
    }
    delete(items) {
        if (this.params.RequestItems[this.tableName].length + items.length > MAX_BATCH_WRITE_ITEMS) {
            throw new Error(`batch write takes at max ${MAX_BATCH_WRITE_ITEMS} items`);
        }
        this.params.RequestItems[this.tableName].push(...items.map(this.createDeleteRequest));
        return this;
    }
    put(items) {
        if (this.params.RequestItems[this.tableName].length + items.length > MAX_BATCH_WRITE_ITEMS) {
            throw new Error(`batch write takes at max ${MAX_BATCH_WRITE_ITEMS} items`);
        }
        this.params.RequestItems[this.tableName].push(...items.map(this.createPutRequest));
        return this;
    }
    /**
     *
     * @param backoffTimer generator for how much timeSlots should be waited before requesting next batch. only used when capacity was exceeded. default randomExponentialBackoffTimer
     * @param throttleTimeSlot defines how long one timeSlot is for throttling, default 1 second
     */
    exec(backoffTimer = randomExponentialBackoffTimer, throttleTimeSlot = DEFAULT_TIME_SLOT) {
        this.logger.debug('starting batchWriteItem');
        return this.write(backoffTimer, throttleTimeSlot).pipe(map(() => {
            return;
        }));
    }
    execFullResponse(backoffTimer = randomExponentialBackoffTimer, throttleTimeSlot = DEFAULT_TIME_SLOT) {
        return this.write(backoffTimer, throttleTimeSlot);
    }
    write(backoffTimer, throttleTimeSlot) {
        return batchWriteItemsWriteAll(this.dynamoRx, Object.assign({}, this.params), backoffTimer(), throttleTimeSlot);
    }
}
//# sourceMappingURL=batch-write-single-table.request.js.map