import * as tslib_1 from "tslib";
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';
var ReadManyRequest = /** @class */ (function (_super) {
    tslib_1.__extends(ReadManyRequest, _super);
    function ReadManyRequest(dynamoRx, modelClazz) {
        var _this = _super.call(this, dynamoRx, modelClazz) || this;
        _this.mapFromDb = function (output) {
            var response = tslib_1.__assign({}, output);
            response.Items = (output.Items || []).map(function (item) { return fromDb(item, _this.modelClazz); });
            return response;
        };
        _this.limit(ReadManyRequest.DEFAULT_LIMIT);
        return _this;
    }
    /**
     *
     * @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}
     */
    ReadManyRequest.prototype.exclusiveStartKey = function (key) {
        if (key) {
            this.params.ExclusiveStartKey = key;
        }
        else {
            delete this.params.ExclusiveStartKey;
        }
        return this;
    };
    ReadManyRequest.prototype.index = function (indexName) {
        var 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;
    };
    ReadManyRequest.prototype.limit = function (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;
    };
    ReadManyRequest.prototype.whereAttribute = function (attributePath) {
        return addCondition('FilterExpression', attributePath, this, this.metadata);
    };
    ReadManyRequest.prototype.where = function () {
        var conditionDefFns = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            conditionDefFns[_i] = arguments[_i];
        }
        var condition = and.apply(void 0, tslib_1.__spread(conditionDefFns))(undefined, this.metadata);
        addExpression('FilterExpression', condition, this.params);
        return this;
    };
    ReadManyRequest.prototype.execNoMap = function () {
        var _this = this;
        this.logger.debug('request (noMap)', this.params);
        return this.doRequest(this.params)
            .pipe(tap(function (response) { return _this.logger.debug('response', response); }));
    };
    ReadManyRequest.prototype.execSingle = function () {
        var _this = this;
        var params = tslib_1.__assign({}, this.params, { Limit: 1 });
        this.logger.debug('single request', params);
        return this.doRequest(params).pipe(tap(function (response) { return _this.logger.debug('response', response); }), map(this.mapFromDb), map(function (r) { return r.Items && r.Items.length ? r.Items[0] : null; }), tap(function (item) { return _this.logger.debug('mapped item', item); }));
    };
    ReadManyRequest.prototype.execCount = function () {
        var _this = this;
        var params = tslib_1.__assign({}, this.params, { Select: 'COUNT' });
        this.logger.debug('count request', params);
        return this.doRequest(params).pipe(tap(function (response) { return _this.logger.debug('response', response); }), map(function (response) { return response.Count || 0; }), tap(function (count) { return _this.logger.debug('count', count); }));
    };
    ReadManyRequest.prototype.exec = function () {
        var _this = this;
        this.logger.debug('request', this.params);
        return this.doRequest(this.params).pipe(tap(function (response) { return _this.logger.debug('response', response); }), map(this.mapFromDb), map(function (r) { return r.Items; }), tap(function (items) { return _this.logger.debug('mapped items', items); }));
    };
    ReadManyRequest.prototype.execFullResponse = function () {
        var _this = this;
        this.logger.debug('request', this.params);
        return this.doRequest(this.params).pipe(tap(function (response) { return _this.logger.debug('response', response); }), map(this.mapFromDb), tap(function (response) { return _this.logger.debug('mapped items', response.Items); }));
    };
    /**
     * fetches all pages. may uses all provisionedOutput, therefore for client side use cases rather use pagedDatasource (exec)
     */
    ReadManyRequest.prototype.execFetchAll = function () {
        return fetchAll(this);
    };
    ReadManyRequest.DEFAULT_LIMIT = 10;
    ReadManyRequest.INFINITE_LIMIT = -1;
    return ReadManyRequest;
}(StandardRequest));
export { ReadManyRequest };
//# sourceMappingURL=read-many.request.js.map