"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const operators_1 = require("rxjs/operators");
const logger_1 = require("../../../logger/logger");
const mapper_1 = require("../../../mapper");
const request_model_1 = require("../request.model");
class ScanRequest extends request_model_1.Request {
    constructor(dynamoRx, modelClazz) {
        super(dynamoRx, modelClazz);
        this.mapFromDb = (output) => {
            const response = Object.assign({}, output);
            if (output.Items) {
                response.Items = output.Items.map(item => mapper_1.fromDb(item, this.modelClazz));
            }
            return response;
        };
        this.logger = logger_1.createLogger('dynamo.request.ScanRequest', modelClazz);
    }
    execNoMap() {
        this.logger.debug('request (noMap)', this.params);
        return this.dynamoRx.scan(this.params)
            .pipe(operators_1.tap(response => this.logger.debug('response', response)));
    }
    execFullResponse() {
        this.logger.debug('request', this.params);
        return this.dynamoRx.scan(this.params).pipe(operators_1.tap(response => this.logger.debug('response', response)), operators_1.map(this.mapFromDb), operators_1.tap(response => this.logger.debug('mapped items', response.Items)));
    }
    exec() {
        this.logger.debug('request', this.params);
        return this.dynamoRx.scan(this.params).pipe(operators_1.tap(response => this.logger.debug('response', response)), operators_1.map(this.mapFromDb), operators_1.map(r => r.Items), operators_1.tap(items => this.logger.debug('mapped items', items)));
    }
    execSingle() {
        const params = Object.assign({}, this.params, { Limit: 1 });
        this.logger.debug('single request', params);
        return this.dynamoRx.scan(this.params).pipe(operators_1.tap(response => this.logger.debug('response', response)), operators_1.map(this.mapFromDb), operators_1.map(r => r.Items && r.Items.length ? r.Items[0] : null), operators_1.tap(item => this.logger.debug('mapped item', item)));
    }
    execCount() {
        const params = Object.assign({}, this.params, { Select: 'COUNT' });
        this.logger.debug('count request', params);
        return this.dynamoRx.scan(params).pipe(operators_1.tap(response => this.logger.debug('response', response)), operators_1.map(response => response.Count || 0), operators_1.tap(count => this.logger.debug('count', count)));
    }
}
exports.ScanRequest = ScanRequest;
//# sourceMappingURL=scan.request.js.map