import * as tslib_1 from "tslib";
import { fetchAll } from '../../helper';
import { and } from '../expression';
import { addExpression } from '../expression/param-util';
import { addCondition } from '../expression/request-expression-builder';
import { getTableName } from '../get-table-name.function';
import { BaseRequest } from './base.request';
var Request = /** @class */ (function (_super) {
    tslib_1.__extends(Request, _super);
    function Request(dynamoRx, modelClazz) {
        var _this = _super.call(this, dynamoRx, modelClazz) || this;
        _this.params = {
            TableName: getTableName(_this.metadata),
        };
        _this.limit(Request.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}
     */
    Request.prototype.exclusiveStartKey = function (key) {
        if (key) {
            this.params.ExclusiveStartKey = key;
        }
        else {
            delete this.params.ExclusiveStartKey;
        }
        return this;
    };
    Request.prototype.index = function (indexName) {
        var index = this.metadata.getIndex(indexName);
        if (index) {
            this.params.IndexName = indexName;
        }
        else {
            throw new Error("there is no index with name <" + indexName + "> defined for model " + this.modelClazz.name);
        }
        return this;
    };
    Request.prototype.limit = function (limit) {
        if (limit === Request.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;
    };
    // TODO TYPING how can we improve the typing to define the accepted value for condition function (see
    // update2.function)
    Request.prototype.whereAttribute = function (attributePath) {
        return addCondition('FilterExpression', attributePath, this, this.metadata);
    };
    Request.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;
    };
    /**
     * fetches all pages. may uses all provisionedOutput, therefore for client side use cases rather use pagedDatasource (exec)
     */
    Request.prototype.execFetchAll = function () {
        return fetchAll(this);
    };
    Request.DEFAULT_LIMIT = 10;
    Request.INFINITE_LIMIT = -1;
    return Request;
}(BaseRequest));
export { Request };
//# sourceMappingURL=request.model.js.map