import { map, tap } from 'rxjs/operators';
import { fetchAll } from '../../helper';
import { fromDb } from '../../mapper';
import { and } from '../expression';
import { addExpression } from '../expression/param-util';
import { addCondition } from '../expression/request-expression-builder';
import { StandardRequest } from './standard.request';
export class ReadManyRequest extends StandardRequest {
    constructor(dynamoRx, modelClazz) {
        super(dynamoRx, modelClazz);
        this.mapFromDb = (output) => {
            const response = Object.assign({}, output);
            response.Items = (output.Items || []).map(item => 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 addCondition('FilterExpression', attributePath, this, this.metadata);
    }
    where(...conditionDefFns) {
        const condition = and(...conditionDefFns)(undefined, this.metadata);
        addExpression('FilterExpression', condition, this.params);
        return this;
    }
    execNoMap() {
        this.logger.debug('request (noMap)', this.params);
        return this.doRequest(this.params)
            .pipe(tap(response => this.logger.debug('response', response)));
    }
    execSingle() {
        const params = Object.assign({}, this.params, { Limit: 1 });
        this.logger.debug('single request', params);
        return this.doRequest(params).pipe(tap(response => this.logger.debug('response', response)), map(this.mapFromDb), map(r => r.Items && r.Items.length ? r.Items[0] : null), 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.doRequest(params).pipe(tap(response => this.logger.debug('response', response)), map(response => response.Count || 0), tap(count => this.logger.debug('count', count)));
    }
    exec() {
        this.logger.debug('request', this.params);
        return this.doRequest(this.params).pipe(tap(response => this.logger.debug('response', response)), map(this.mapFromDb), map(r => r.Items), tap(items => this.logger.debug('mapped items', items)));
    }
    execFullResponse() {
        this.logger.debug('request', this.params);
        return this.doRequest(this.params).pipe(tap(response => this.logger.debug('response', response)), map(this.mapFromDb), 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 fetchAll(this);
    }
}
ReadManyRequest.DEFAULT_LIMIT = 10;
ReadManyRequest.INFINITE_LIMIT = -1;
//# sourceMappingURL=read-many.request.js.map