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';
export class ScanRequest extends Request {
    constructor(dynamoRx, modelClazz, tableName) {
        super(dynamoRx, modelClazz, tableName);
        this.logger = createLogger('dynamo.request.ScanRequest', modelClazz);
    }
    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;
    }
    execFullResponse() {
        delete this.params.Select;
        this.logger.debug('request', this.params);
        return this.dynamoRx.scan(this.params).pipe(tap(response => this.logger.debug('response', response)), map(queryResponse => {
            const response = Object.assign({}, queryResponse);
            if (queryResponse.Items) {
                response.Items = queryResponse.Items.map(item => fromDb(item, this.modelClazz));
            }
            return response;
        }), tap(response => this.logger.debug('mapped items', response.Items)));
    }
    execNoMap() {
        this.logger.debug('request (noMap)', this.params);
        return this.dynamoRx.scan(this.params).pipe(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(tap(response => this.logger.debug('response', response)), map(response => (response.Items || []).map(item => fromDb(item, this.modelClazz))), 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(tap(response => this.logger.debug('response', response)), map(response => {
            return response.Items && response.Items.length
                ? fromDb(response.Items[0], this.modelClazz)
                : null;
        }), 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(tap(response => this.logger.debug('response', response)), map(response => response.Count || 0), 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 fetchAll(this);
    }
}
//# sourceMappingURL=scan.request.js.map