import * as tslib_1 from "tslib";
import { map } from 'rxjs/operators';
import { Mapper } from '../../../mapper/mapper';
import { and } from '../../expression/logical-operator/and.function';
import { ParamUtil } from '../../expression/param-util';
import { RequestExpressionBuilder } from '../../expression/request-expression-builder';
import { Request } from '../request.model';
var QueryRequest = /** @class */ (function (_super) {
    tslib_1.__extends(QueryRequest, _super);
    function QueryRequest(dynamoRx, modelClazz, tableName) {
        return _super.call(this, dynamoRx, modelClazz, tableName) || this;
    }
    QueryRequest.prototype.wherePartitionKey = function (partitionKeyValue) {
        var partitionKey;
        if (this.params.IndexName) {
            var index = this.metaData.getIndex(this.params.IndexName);
            if (index) {
                partitionKey = index.partitionKey;
                if (!partitionKey) {
                    throw new Error("there is no parition key defined for index <" + this.params.IndexName + ">");
                }
            }
            else {
                throw new Error("the index <" + this.params.IndexName + "> does not exist on model " + this.modelClazz.name);
            }
        }
        else {
            partitionKey = this.metaData.getPartitionKey();
        }
        return RequestExpressionBuilder.addSortKeyCondition(partitionKey, this, this.metaData).equals(partitionKeyValue);
    };
    /**
     * used to define some condition for the sort key, use the secondary index to query based on a custom index
     * @returns {RequestConditionFunction<T>}
     */
    QueryRequest.prototype.whereSortKey = function () {
        var sortKey;
        if (this.params.IndexName) {
            var index = this.metaData.getIndex(this.params.IndexName);
            if (index) {
                if (index.sortKey) {
                    sortKey = index.sortKey;
                }
                else {
                    throw new Error("there is no sort key defined for index <" + this.params.IndexName + ">");
                }
            }
            else {
                throw new Error("the index <" + this.params.IndexName + "> does not exist on model " + this.modelClazz.name);
            }
        }
        else {
            sortKey = this.metaData.getSortKey();
        }
        if (!sortKey) {
            throw new Error('There was no sort key defined for current schema');
        }
        return RequestExpressionBuilder.addSortKeyCondition(sortKey, this);
    };
    // TODO TYPING how can we improve the typing to define the accepted value for condition function (see
    // update2.function)
    QueryRequest.prototype.whereAttribute = function (attributePath) {
        return RequestExpressionBuilder.addCondition('FilterExpression', attributePath, this, this.metaData);
    };
    QueryRequest.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);
        ParamUtil.addExpression('FilterExpression', condition, this.params);
        return this;
    };
    QueryRequest.prototype.ascending = function () {
        this.params.ScanIndexForward = true;
        return this;
    };
    QueryRequest.prototype.descending = function () {
        this.params.ScanIndexForward = false;
        return this;
    };
    QueryRequest.prototype.execCount = function () {
        var params = tslib_1.__assign({}, this.params);
        params.Select = 'COUNT';
        return this.dynamoRx.query(params).pipe(map(function (response) { return response.Count; }));
    };
    QueryRequest.prototype.execFullResponse = function () {
        var _this = this;
        return this.dynamoRx.query(this.params).pipe(map(function (queryResponse) {
            var response = tslib_1.__assign({}, queryResponse);
            response.Items = queryResponse.Items.map(function (item) { return Mapper.fromDb(item, _this.modelClazz); });
            return response;
        }));
    };
    QueryRequest.prototype.exec = function () {
        var _this = this;
        return this.dynamoRx
            .query(this.params)
            .pipe(map(function (response) { return response.Items.map(function (item) { return Mapper.fromDb(item, _this.modelClazz); }); }));
    };
    QueryRequest.prototype.execNoMap = function () {
        return this.dynamoRx.query(this.params);
    };
    QueryRequest.prototype.execSingle = function () {
        var _this = this;
        this.limit(1);
        return this.dynamoRx.query(this.params).pipe(map(function (response) {
            if (response.Count) {
                return Mapper.fromDb(response.Items[0], _this.modelClazz);
            }
            else {
                return null;
            }
        }));
    };
    return QueryRequest;
}(Request));
export { QueryRequest };
//# sourceMappingURL=query.request.js.map