import * as tslib_1 from "tslib";
import { map, tap } from 'rxjs/operators';
import { fetchAll } from '../../../helper';
import { createLogger } from '../../../logger/logger';
import { fromDb } from '../../../mapper';
import { and } from '../../expression';
import { addExpression } from '../../expression/param-util';
import { addCondition } from '../../expression/request-expression-builder';
import { Request } from '../request.model';
var ScanRequest = /** @class */ (function (_super) {
    tslib_1.__extends(ScanRequest, _super);
    function ScanRequest(dynamoRx, modelClazz, tableName) {
        var _this = _super.call(this, dynamoRx, modelClazz, tableName) || this;
        _this.logger = createLogger('dynamo.request.ScanRequest', modelClazz);
        return _this;
    }
    ScanRequest.prototype.whereAttribute = function (attributePath) {
        return addCondition('FilterExpression', attributePath, this, this.metadata);
    };
    ScanRequest.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;
    };
    ScanRequest.prototype.execFullResponse = function () {
        var _this = this;
        delete this.params.Select;
        this.logger.debug('request', this.params);
        return this.dynamoRx.scan(this.params).pipe(tap(function (response) { return _this.logger.debug('response', response); }), map(function (queryResponse) {
            var response = tslib_1.__assign({}, queryResponse);
            if (queryResponse.Items) {
                response.Items = queryResponse.Items.map(function (item) { return fromDb(item, _this.modelClazz); });
            }
            return response;
        }), tap(function (response) { return _this.logger.debug('mapped items', response.Items); }));
    };
    ScanRequest.prototype.execNoMap = function () {
        var _this = this;
        this.logger.debug('request (noMap)', this.params);
        return this.dynamoRx.scan(this.params).pipe(tap(function (response) { return _this.logger.debug('response', response); }));
    };
    ScanRequest.prototype.exec = function () {
        var _this = this;
        delete this.params.Select;
        this.logger.debug('request', this.params);
        return this.dynamoRx.scan(this.params).pipe(tap(function (response) { return _this.logger.debug('response', response); }), map(function (response) { return (response.Items || []).map(function (item) { return fromDb(item, _this.modelClazz); }); }), tap(function (items) { return _this.logger.debug('mapped items', items); }));
    };
    ScanRequest.prototype.execSingle = function () {
        var _this = this;
        delete this.params.Select;
        this.logger.debug('single request', this.params);
        return this.dynamoRx.scan(this.params).pipe(tap(function (response) { return _this.logger.debug('response', response); }), map(function (response) {
            return response.Items && response.Items.length
                ? fromDb(response.Items[0], _this.modelClazz)
                : null;
        }), tap(function (item) { return _this.logger.debug('mapped item', item); }));
    };
    ScanRequest.prototype.execCount = function () {
        var _this = this;
        var params = tslib_1.__assign({}, this.params);
        params.Select = 'COUNT';
        this.logger.debug('count request', params);
        return this.dynamoRx.scan(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); }));
    };
    /**
     * fetches all pages. may uses all provisionedOutput, therefore for client side use cases rather use pagedDatasource (exec)
     */
    ScanRequest.prototype.execFetchAll = function () {
        return fetchAll(this);
    };
    return ScanRequest;
}(Request));
export { ScanRequest };
//# sourceMappingURL=scan.request.js.map