"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const operators_1 = require("rxjs/operators");
const helper_1 = require("../../../helper");
const logger_1 = require("../../../logger/logger");
const mapper_1 = require("../../../mapper");
const expression_1 = require("../../expression");
const param_util_1 = require("../../expression/param-util");
const request_expression_builder_1 = require("../../expression/request-expression-builder");
const request_model_1 = require("../request.model");
class ScanRequest extends request_model_1.Request {
    constructor(dynamoRx, modelClazz, tableName) {
        super(dynamoRx, modelClazz, tableName);
        this.logger = logger_1.createLogger('dynamo.request.ScanRequest', modelClazz);
    }
    whereAttribute(attributePath) {
        return request_expression_builder_1.addCondition('FilterExpression', attributePath, this, this.metadata);
    }
    where(...conditionDefFns) {
        const condition = expression_1.and(...conditionDefFns)(undefined, this.metadata);
        param_util_1.addExpression('FilterExpression', condition, this.params);
        return this;
    }
    execFullResponse() {
        delete this.params.Select;
        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(queryResponse => {
            const response = Object.assign({}, queryResponse);
            if (queryResponse.Items) {
                response.Items = queryResponse.Items.map(item => mapper_1.fromDb(item, this.modelClazz));
            }
            return response;
        }), operators_1.tap(response => this.logger.debug('mapped items', response.Items)));
    }
    execNoMap() {
        this.logger.debug('request (noMap)', this.params);
        return this.dynamoRx.scan(this.params).pipe(operators_1.tap(response => this.logger.debug('response', response)));
    }
    exec() {
        delete this.params.Select;
        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(response => (response.Items || []).map(item => mapper_1.fromDb(item, this.modelClazz))), operators_1.tap(items => this.logger.debug('mapped items', items)));
    }
    execSingle() {
        delete this.params.Select;
        this.logger.debug('single request', this.params);
        return this.dynamoRx.scan(this.params).pipe(operators_1.tap(response => this.logger.debug('response', response)), operators_1.map(response => {
            return response.Items && response.Items.length
                ? mapper_1.fromDb(response.Items[0], this.modelClazz)
                : null;
        }), operators_1.tap(item => this.logger.debug('mapped item', item)));
    }
    execCount() {
        const params = Object.assign({}, this.params);
        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)));
    }
    /**
     * fetches all pages. may uses all provisionedOutput, therefore for client side use cases rather use pagedDatasource (exec)
     */
    execFetchAll() {
        return helper_1.fetchAll(this);
    }
}
exports.ScanRequest = ScanRequest;
//# sourceMappingURL=scan.request.js.map