"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const operators_1 = require("rxjs/operators");
const helper_1 = require("../../helper");
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 standard_request_1 = require("./standard.request");
/**
 * Base class for query and scan request classes.
 */
class ReadManyRequest extends standard_request_1.StandardRequest {
    constructor(dynamoRx, modelClazz) {
        super(dynamoRx, modelClazz);
        this.mapFromDb = (output) => {
            const response = Object.assign({}, output);
            response.Items = (output.Items || []).map(item => mapper_1.fromDb(item, this.modelClazz));
            return response;
        };
        this.limit(ReadManyRequest.DEFAULT_LIMIT);
    }
    /**
     *
     * @param key A map representing the start id which is included in next call, if null is delivered
     * startKey will be removed from params
     * @returns {Request}
     */
    exclusiveStartKey(key) {
        if (key) {
            this.params.ExclusiveStartKey = key;
        }
        else {
            delete this.params.ExclusiveStartKey;
        }
        return this;
    }
    index(indexName) {
        const index = this.metadata.getIndex(indexName);
        if (index) {
            this.secondaryIndex = index;
            this.params.IndexName = indexName;
        }
        else {
            throw new Error(`there is no index with name <${indexName}> defined for model ${this.modelClazz.name}`);
        }
        return this;
    }
    limit(limit) {
        if (limit === ReadManyRequest.INFINITE_LIMIT) {
            delete this.params.Limit;
        }
        else {
            if (limit !== null && limit !== undefined && limit > 0) {
                this.params.Limit = limit;
            }
            else {
                throw new Error('limit must be a valid positive number');
            }
        }
        return this;
    }
    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;
    }
    execNoMap() {
        this.logger.debug('request (noMap)', this.params);
        return this.doRequest(this.params).pipe(operators_1.tap(response => this.logger.debug('response', response)));
    }
    execSingle() {
        // do not alter the params on the instance but add the additional 'Limit' param to a copy.
        // otherwise a follow-up request with the very same request-object would be wrong
        const params = Object.assign({}, this.params, { Limit: 1 });
        this.logger.debug('single request', params);
        return this.doRequest(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() {
        // do not alter the params on the instance but add the additional 'Limit' param to a copy.
        // otherwise a follow-up request with the very same request-object would be wrong
        const params = Object.assign({}, this.params, { Select: 'COUNT' });
        this.logger.debug('count request', params);
        return this.doRequest(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)));
    }
    exec() {
        this.logger.debug('request', this.params);
        return this.doRequest(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)));
    }
    execFullResponse() {
        this.logger.debug('request', this.params);
        return this.doRequest(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)));
    }
    /**
     * fetches all pages. may uses all provisionedOutput, therefore for client side use cases rather use pagedDatasource (exec)
     */
    execFetchAll() {
        return helper_1.fetchAll(this);
    }
}
ReadManyRequest.DEFAULT_LIMIT = 10;
ReadManyRequest.INFINITE_LIMIT = -1;
exports.ReadManyRequest = ReadManyRequest;
//# sourceMappingURL=read-many.request.js.map